Problem
When running terraform plan or terraform apply with a remote backend configured in HCP Terraform or Terraform Enterprise, the operation fails with the following error if a local terraform.tfstate file is present:
Error: Error locking destination state: Error acquiring the state lock: resource not found Terraform acquires a state lock to protect the state from being written by multiple users at the same time. Please resolve the issue above and try again. For most commands, you can disable locking with the "-lock=false" flag, but this is not recommended.
Prerequisites
- An HCP Terraform or Terraform Enterprise workspace.
- A local Terraform configuration initialized with a remote backend or HCP Terraform cloud integration.
- A non-empty
terraform.tfstatefile exists in the local configuration directory. - You are using Terraform CLI version
v1.1.6or earlier.
Cause
Terraform CLI versions v1.1.6 and earlier upload the entire configuration directory, including the local terraform.tfstate file, to the remote backend for execution. When the remote backend attempts to manage the state, it conflicts with the uploaded local state file, leading to a state lock error.
Starting with Terraform v1.1.7, the CLI automatically ignores the local terraform.tfstate file during remote operations, preventing this conflict.
Solutions
There are two primary solutions to resolve this issue. The recommended approach is to upgrade your Terraform CLI version.
Solution 1: Upgrade Terraform CLI
Upgrade your local Terraform CLI to version v1.1.7 or a later version. This is the recommended long-term solution, as these versions automatically ignore the local state file during remote operations.
Check your current Terraform version.
$ terraform version
- Follow the official documentation to upgrade the Terraform CLI to the latest version.
Solution 2: Delete the Local State File
If you cannot upgrade your Terraform CLI version, you can manually delete the local terraform.tfstate file before running remote operations.
Warning: This action is destructive. Ensure you have a backup or that the authoritative state is correctly stored in your remote backend before deleting the local file.
- Navigate to your Terraform configuration directory.
Delete the local state file.
$ rm terraform.tfstate
- Run
terraform planorterraform applyagain. The command should now succeed as there is no local state file to cause a conflict.
Additional Information
- For more details on this change, refer to the Terraform
v1.1.7changelog. - For guidance on managing remote state, refer to the official Terraform documentation on remote backends.