terraform-provider-proxmox: Proxmox VM Creation Fails with ‘Unable to Retrieve VM Identifier’ Error When Cloning Multiple VMs Simultaneously

Describe the bug When creating two or more virtual machines (VMs) using the , an error occurs indicating a failure to retrieve the next available VM identifier.

To Reproduce Steps to reproduce the behavior:

  1. Create a Proxmox resource with the following Terraform configuration.
  2. Run terraform apply (or tofu apply).
  3. Observe that vm2 is created successfully, but vm1 fails with an error related to VM identifier retrieval.
  4. Modify the resource ‘…’
  5. Run ‘…’
  6. See error

Please also provide a minimal Terraform configuration that reproduces the issue.


# >>> terraform {
  required_providers {
    proxmox = {
      source  = "bpg/proxmox"
      version = "0.66.3"
    }
  }
}
provider "proxmox" {
  endpoint  = "https://xxx:8006/"
  api_token = "xxx"
  insecure  = true
}

resource "proxmox_virtual_environment_vm" "vm1" {
  name            = "va-vm-name1"
  node_name       = "va-dev-proxmox01"
  stop_on_destroy = true

  clone {
    vm_id     = 9001
    full      = true
    node_name = "va-dev-proxmox02"
  }
}

resource "proxmox_virtual_environment_vm" "vm2" {
  name            = "va-vm-name2"
  node_name       = "va-dev-proxmox01"
  stop_on_destroy = true

  clone {
    vm_id     = 9001
    full      = true
    node_name = "va-dev-proxmox02"
  }
} <<< #

and the output of terraform|tofu apply.

proxmox_virtual_environment_vm.vm2: Creating...
proxmox_virtual_environment_vm.vm2: Still creating... [10s elapsed]
proxmox_virtual_environment_vm.vm2: Creation complete after 18s [id=115]
╷
│ Error: unable to retrieve the next available VM identifier: context deadline exceeded
│
│   with proxmox_virtual_environment_vm.vm1,
│   on main.tf line 16, in resource "proxmox_virtual_environment_vm" "vm1":
│   16: resource "proxmox_virtual_environment_vm" "vm1" {

Expected behavior Both vm1 and vm2 should be created successfully

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

  • Single or clustered Proxmox: Clustered
  • Proxmox version: 8.2.7
  • Provider version (ideally it should be the latest version): 0.66.3
  • Terraform/OpenTofu version: 1.8.7
  • OS (where you run Terraform/OpenTofu from): macos sonoma
  • Debug logs (TF_LOG=DEBUG terraform apply):

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 22 (9 by maintainers)

Most upvoted comments

Ok, so here is the use case that doesn’t work well.

Precondition: there are a lot of VMs / containers on PVE, but there is a gap in ID allocation closer to the beginning of the range. For example: 100, 101, 102, 105, 106, <continue without gaps>, 150 (technically the tail of continuous IDs after the gap should be at least 25)

Now imagine we need to provision 4 VMs form a single config. By default, TF parallelism is 4, so those VMs will start provision simultaneously in separate threads. They all ask for “get next id” from PVE at the same time, and all receive “here is: 103!” response from the PVE.

Obviously, that won’t work. The provider has a locking mechanism that prevents parallel executions to use the same ID, so a thread that got into this condition uses a simple “+1” logic to try the next one. And this won’t work in all cases, especially if the “gap” of unused IDs is smaller than the number of concurrently provisioning VMs at the moment. So the provider will keep enumerating ID up to find available until it times out (currently 5 sec, with 200ms delay between iterations).

It seems logical to use the “get next id” API call again instead of “+1”, however, it is not guarantee to return the actually available ID, as the could be in-flight “create VM” call for e.g. ID 103, but it is not committed by PVE yet, so it may happily return 103 again.

I think a few retries should solve that. I’ll also add an acceptance test to verify this scenario.

Hey @maskudo 👋🏼

This error could also be a symptom of connectivity issues between your machine and PVE. Can you apply any config at all? Like this one:

data "proxmox_virtual_environment_time" "node_time" {
  node_name = var.proxmox_host
}

We are fighting with this issue for some time, very randomly it appears and currently breaks our deployment. According to our tests:

Testing with the newly released version 0.37.0 we were able to get some more logs regarding this issue. What immediately caught our attention was that IDs that are in use, are requested using the api call GET /api2/json/cluster/nextid?vmid=<UNAVAILABLE_ID>. In the logs we could not find a single call to GET /api2/json/cluster/nextid without the vmid query parameter, which would have returned an available ID. As seen in the Proxmox logs the HTTP error code 400 is returned, which indicates that the requested ID is not available.

Proxmox logs:

REDACTED [20/Nov/2024:11:24:50.279] pve~ pve/REDACTED 0/0/4/3/7 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=190 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:50.486] pve~ pve/REDACTED 0/0/3/3/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=191 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:50.693] pve~ pve/REDACTED 0/0/5/4/9 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=192 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:50.904] pve~ pve/REDACTED 0/0/0/3/3 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=193 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:51.109] pve~ pve/REDACTED 0/0/0/3/3 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=194 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:51.312] pve~ pve/REDACTED 0/0/3/3/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=195 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:51.519] pve~ pve/REDACTED 0/0/5/2/7 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=196 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:51.732] pve~ pve/REDACTED 0/0/9/3/12 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=197 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:51.947] pve~ pve/REDACTED 0/0/3/3/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=198 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:52.155] pve~ pve/REDACTED 0/0/3/2/5 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=199 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:52.361] pve~ pve/REDACTED 0/0/2/2/4 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=200 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:52.567] pve~ pve/REDACTED 0/0/3/2/5 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=201 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:52.774] pve~ pve/REDACTED 0/0/5/5/10 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=202 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:52.985] pve~ pve/REDACTED 0/0/4/5/9 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=203 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:53.197] pve~ pve/REDACTED 0/0/3/3/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=204 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:53.404] pve~ pve/REDACTED 0/0/3/3/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=205 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:53.611] pve~ pve/REDACTED 0/0/4/3/7 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=206 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:53.819] pve~ pve/REDACTED 0/0/3/3/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=207 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:54.028] pve~ pve/REDACTED 0/0/4/4/8 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=208 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:54.236] pve~ pve/REDACTED 0/0/3/3/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=209 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:54.443] pve~ pve/REDACTED 0/0/3/6/9 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=210 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:54.654] pve~ pve/REDACTED 0/0/3/2/5 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=211 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:54.861] pve~ pve/REDACTED 0/0/4/2/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=212 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:55.070] pve~ pve/REDACTED 0/0/3/3/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=213 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:55.276] pve~ pve/REDACTED 0/0/3/3/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=214 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:55.483] pve~ pve/REDACTED 0/0/4/2/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=215 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:55.691] pve~ pve/REDACTED 0/0/3/3/6 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=216 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:55.898] pve~ pve/REDACTED 0/0/5/3/8 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=217 HTTP/1.1"
REDACTED [20/Nov/2024:11:24:56.108] pve~ pve/REDACTED 0/0/4/3/7 400 304 - - ---- 2/2/0/0/0 0/0 "GET /api2/json/cluster/nextid?vmid=218 HTTP/1.1"

Additionally we can provide a relevant snippet from our tofu apply logs.

tofu apply logs:

2024-11-20T11:24:31.356Z [DEBUG] provider.terraform-provider-proxmox_v0.67.0: Sending HTTP Request: @caller=/home/runner/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.35.0/helper/logging/logging_http_transport.go:162 Host=REDACTED User-Agent=Go-http-client/1.1 tf_http_req_body="" tf_mux_provider=tf5to6server.v5tov6Server Accept=application/json tf_http_req_method=GET tf_provider_addr=registry.terraform.io/bpg/proxmox tf_req_id=19938013-de2b-01e4-2f70-fea77f785e61 Authorization="PVEAPIToken=REDACTED" tf_http_op_type=request tf_http_req_uri=/api2/json/cluster/nextid?vmid=218 tf_rpc=ApplyResourceChange @module=proxmox Accept-Encoding=gzip tf_http_req_version=HTTP/1.1 tf_http_trans_id=49601e70-85f3-f9b7-65b0-553a9a5f3c3f tf_resource_type=proxmox_virtual_environment_vm timestamp=2024-11-20T11:24:31.355Z

2024-11-20T11:24:31.364Z [DEBUG] provider.terraform-provider-proxmox_v0.67.0: Received HTTP Response: tf_http_res_status_code=400 tf_http_res_version=HTTP/1.1 tf_http_trans_id=49601e70-85f3-f9b7-65b0-553a9a5f3c3f tf_resource_type=proxmox_virtual_environment_vm Server=pve-api-daemon/3.0 tf_http_op_type=response tf_http_res_body="{\"errors\":{\"vmid\":\"VM 218 already exists\"},\"data\":null}" tf_req_id=19938013-de2b-01e4-2f70-fea77f785e61 @caller=/home/runner/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.35.0/helper/logging/logging_http_transport.go:162 @module=proxmox Cache-Control=max-age=0 Pragma=no-cache tf_http_res_status_reason="400 Parameter verification failed." tf_mux_provider=tf5to6server.v5tov6Server tf_rpc=ApplyResourceChange Content-Length=55 tf_provider_addr=registry.terraform.io/bpg/proxmox Content-Type=application/json;charset=UTF-8 Date="Wed, 20 Nov 2024 11:24:31 GMT" Expires="Wed, 20 Nov 2024 11:24:31 GMT" timestamp=2024-11-20T11:24:31.364Z

2024-11-20T11:24:31.523Z [ERROR] provider.terraform-provider-proxmox_v0.67.0: Response contains error diagnostic: diagnostic_summary="unable to retrieve the next available VM identifier: context deadline exceeded" tf_rpc=ApplyResourceChange diagnostic_severity=ERROR tf_provider_addr=registry.terraform.io/bpg/proxmox tf_req_id=19938013-de2b-01e4-2f70-fea77f785e61 @module=sdk.proto diagnostic_detail="" @caller=/home/runner/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.25.0/tfprotov6/internal/diag/diagnostics.go:58 tf_proto_version=6.7 tf_resource_type=proxmox_virtual_environment_vm timestamp=2024-11-20T11:24:31.522Z

2024-11-20T11:24:31.540Z [DEBUG] State storage *remote.State declined to persist a state snapshot

2024-11-20T11:24:31.540Z [ERROR] vertex "REDACTED.proxmox_virtual_environment_vm.user_vm" error: unable to retrieve the next available VM identifier: context deadline exceeded

2024-11-20T11:24:31.556Z [WARN]  provider.terraform-provider-proxmox_v0.67.0: unable to require attribute replacement: error="ForceNew: No changes for vm_id" tf_attribute_path=vm_id tf_req_id=b25857c0-a85e-1517-1667-fe4e5bf1f4ec @caller=/home/runner/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.35.0/helper/customdiff/force_new.go:32 tf_mux_provider=tf5to6server.v5tov6Server tf_provider_addr=registry.terraform.io/bpg/proxmox tf_resource_type=proxmox_virtual_environment_vm tf_rpc=PlanResourceChange @module=sdk.helper_schema timestamp=2024-11-20T11:24:31.556Z

We hope, that the provided insight helps to fix the underlying problem. Should more information be required, don’t hesitate to reach out to us.

Returned back to 0.66.3 to check random vmid feature - it does work as declared, all 9 VMs were created successfully. Added these to ‘provider’ block:

provider "proxmox" {
...
  random_vm_ids      = true
  random_vm_id_start = 90000
  random_vm_id_end   = 90999
...
}

So here’s 2 ways to deal with the issue as of now, hopefully @bpg will be able to tinker with that stuff a bit more. For now i’ll stay on 0.66.3 and will see how random vmid feature behaves. As i understand it should help prevent possible conflicts with vmid collision on allocation with parallel execution (for example a few pipelines and manual execution from workstation simultaneously on the same cluster) unlike pre-0.66.0 way.