Unlocking State Locks
Overview
Occasionally, OpenTofu/Terraform state locks may remain in place if the process holding the lock does not release it properly. This can occur due to unexpected failures, such as crashes or premature termination of jobs.
When this occurs, jobs will fail after a timeout with Error: Error acquiring the state lock, and will not succeed until the lock is manually removed.
You might have a default lock timeout in your root.hcl file that looks like this:
terraform {
extra_arguments "retry_lock" {
commands = get_terraform_commands_that_need_locking()
arguments = ["-lock-timeout=10m"]
}
}
Unlocking Unit State
When a unit lock is held, the lock can be manually removed with the unlock unit workflow. This is a convenience workflow for running the OpenTofu/Terraform force-unlock command. The workflow requires the following inputs:
Lock ID
The Lock ID can be found in the logs where a job has failed to acquire the state lock.
Lock ID
In this example the ID is ca9c97f1-6315-c0d1-56ef-efb8c8996e8c.
Unit Path
The Unit Path is the relative path to the directory containing your terragrunt.hcl file where the lock is being held.
In the above example the Unit Path is acme/us-east-1/storage/s3bucket1.
Stack Path
Optional: When a Unit within a Stack is locked, Pipelines requires the Stack Path to generate the stack before running force-unlock in the Unit.
- GitHub
- GitLab
- From the Actions Tab, select Pipelines Unlock from the list of workflows on the left.
- Select the Run workflow dropdown on the right
- Enter the Lock ID and Unit Path values into the dropdown
- Select Run workflow
GitHub Unlock Unit Workflow
- From the Pipelines tab select New Pipeline
- Select the
pipelines_workflowInput and change the Value tounlock-unitfrom the dropdown - Enter the Lock ID and Unit Path into the
pipelines_unlock_unit_lock_idandpipelines_unlock_unit_pathinputs - Select New pipeline
GitLab Unlock Unit Workflow
Unlocking All State
In the event that many locks are being held, and it is difficult to obtain the Lock IDs, an Unlock All workflow exists to forcibly remove all locks. Run this workflow with caution.
Unlock All removes every lock, including locks held by jobs that are still running. Only run it when no pipelines are in flight.
Unlock All checks every unit that isn't in your ignore list. It decides which cloud a unit uses from the type of block inside the authentication block of the unit's matching environment: aws_oidc, gcp_oidc or azure_oidc.
What Unlock All removes depends on that cloud:
- AWS: deletes the DynamoDB
terraform-lockstable in every region, then re-runsinitin one unit per account to recreate it. If your lock tables have a different name, enable PIPELINES_FEATURE_EXPERIMENT_UNLOCK_ALL_LOCK_TABLE_FROM_REMOTE_STATE to use thedynamodb_tableset in each unit'sremote_state. - GCP: deletes each unit's lock object (
<prefix>/default.tflock) from its GCS state bucket, read from the unit'sremote_stateblock. No re-init is needed. This requires Terragrunt 1.0 or later, andpipelines-workflowsv4.34.0 or later on GitHub, or v2.29.0 or later on GitLab. Units it can't unlock are listed in a warning: those that fail to render, have nogcsremote_state, or setstorage_custom_endpointoruniverse_domain. - Azure: not supported yet. Azure units are skipped with a warning.
- Custom authentication: not supported. Unlock All can't tell which cloud a
customblock authenticates to, so it skips these units. A skipped unit only gets an info-level log line,Skipping unit - "<unit path>" no AWS account found, and the workflow still succeeds. To remove a lock in one of these units, use Unlocking Unit State.
- GitHub
- GitLab
- From the Actions Tab, select Pipelines Unlock from the list of workflows on the left.
- Select the Run workflow dropdown on the right
- Tick the checkbox to forcibly reset all locks
- Select Run workflow
GitHub Unlock All Workflow
- From the Pipelines tab select New Pipeline
- Select the
pipelines_workflowInput and change the Value tounlock-allfrom the dropdown - Select New pipeline
GitLab Unlock All Workflow