Problem
When attempting to unlock a remote backend workspace from the Terraform CLI, the operation fails with the following error message:
Failed to unlock state: lock ID "f51f0d55-84a8-cfae-6217-e3e0046397c1" does not match existing lock ID
Prerequisites
- HCP Terraform
- Terraform Enterprise
Cause
Unlocking remote backend workspaces directly from the Terraform CLI is not a supported operation. This is the default behavior for workspaces managed by HCP Terraform or Terraform Enterprise, and the lock must be removed using the UI or API.
Solutions
There are three methods to unlock a workspace.
Solution 1: Unlock via the UI
You can unlock the workspace directly from the HCP Terraform or Terraform Enterprise web interface. This requires a user account with at least "Manage Workspaces" permissions.
- Log in to the HCP Terraform or Terraform Enterprise UI.
- Select the organization where the workspace resides.
- Click on the name of the locked workspace.
- From the Settings dropdown menu, select Locking.
- Click the Unlock button and confirm the action.
Solution 2: Unlock via the API
You can use the API to programmatically unlock the workspace.
-
Retrieve the workspace ID. For HCP Terraform, the hostname is
app.terraform.io.$ curl \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/vnd.api+json" \ https://$TFE_HOSTNAME/api/v2/organizations/$YOUR_ORG/workspaces/$YOUR_WORKSPACE | jq -r '.data.id'
-
Use the workspace ID from the previous step to unlock the workspace.
$ curl \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/vnd.api+json" \ --request POST \ https://$TFE_HOSTNAME/api/v2/workspaces/$WORKSPACE_ID/actions/unlock
Solution 3: Unlock via the Rails Console (Terraform Enterprise Only)
If you are unable to unlock the workspace using the UI or API, Terraform Enterprise administrators can use the Rails console.
- Log in to the Terraform Enterprise instance via SSH.
-
Launch the Rails console. Note that on older Terraform Enterprise installations, the container name may be
ptfe_atlasinstead oftfe-atlas.# docker exec -ti tfe-atlas /usr/bin/init.sh /app/scripts/wait-for-token -- bash -ic 'cd /app && bin/rails c'
-
Once the Rails console has loaded, run the following commands to find the workspace by name and unlock it.
ws = Workspace.find_by(name: "workspace_name") ws.unlock! ws.save
Outcome
After a successful unlock, the padlock icon next to the workspace name in the UI will disappear. You will then be able to trigger runs from the CLI for that workspace.