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:
- Create a Proxmox resource with the following Terraform configuration.
- Run terraform apply (or tofu apply).
- Observe that vm2 is created successfully, but vm1 fails with an error related to VM identifier retrieval.
- Modify the resource ‘…’
- Run ‘…’
- 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)
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:
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 toGET /api2/json/cluster/nextidwithout thevmidquery parameter, which would have returned an available ID. As seen in the Proxmox logs the HTTP error code400is returned, which indicates that the requested ID is not available.Proxmox logs:
Additionally we can provide a relevant snippet from our tofu apply logs.
tofu apply logs:
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:
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.