How to Fix “Failed” in terraform apply (2026 Guide)
The Short Answer
To fix the “Failed” error in terraform apply, advanced users can try releasing the state lock by running terraform force-unlock with the lock ID, which reduces the sync time from 15 minutes to 30 seconds. Alternatively, verify that the backend configuration is correctly set up to use a state lock, such as backend "s3" { bucket = "my-bucket" key = "terraform.tfstate" region = "us-west-2" }.
Why This Error Happens
- Reason 1: The most common cause of the “Failed” error in terraform apply is a state lock issue, where another process or user is holding a lock on the Terraform state file, preventing the current operation from proceeding. For example, if a previous terraform apply command was interrupted or failed, it may have left a lock on the state file.
- Reason 2: An edge case cause of this error is a misconfigured backend, such as an incorrect bucket name or region in the S3 backend configuration, which can lead to Terraform being unable to acquire a state lock. This can happen when the Terraform configuration is updated to use a new backend, but the old backend is still being used by another process.
- Impact: This error can have a significant impact on Infrastructure as Code (IaC) workflows, as it can prevent deployments and updates from being applied, leading to delays and downtime. In a real-world scenario, this error can occur when multiple teams are working on the same Terraform configuration, and one team’s changes are blocked by a state lock held by another team.
Step-by-Step Solutions
Method 1: The Quick Fix
- Go to the Terraform working directory and run
terraform initto re-initialize the Terraform configuration. - Run
terraform refreshto update the Terraform state file. - Run
terraform applyagain to retry the failed operation.
Method 2: The Command Line/Advanced Fix
To release a stuck state lock, run the following command:
| |
Replace <lock-id> with the actual lock ID, which can be found in the Terraform error message. For example:
| |
This will release the state lock and allow the terraform apply command to proceed.
Prevention: How to Stop This Coming Back
To prevent state lock issues from occurring in the future, follow these best practices:
- Use a consistent backend configuration across all Terraform configurations.
- Ensure that all Terraform operations are properly terminated, such as by running
terraform destroywhen finished with a deployment. - Monitor Terraform state locks using tools like
terraform state lockto detect and release stuck locks. - Consider using a state lock timeout, such as
lock_timeout = "1h"in the S3 backend configuration, to automatically release locks after a certain period of inactivity.
If You Can’t Fix It…
[!WARNING] If terraform apply keeps crashing due to state lock issues, consider switching to AWS Cloud Development Kit (CDK), which handles state locks natively without these errors. CDK provides a more robust and scalable solution for IaC workflows, with built-in support for state management and locking.
FAQ
Q: Will I lose data fixing this?
A: Releasing a state lock using terraform force-unlock will not delete any data, but it may cause inconsistencies in the Terraform state file if the lock was held by a process that was making changes to the infrastructure. To minimize risks, ensure that all Terraform operations are properly terminated before releasing a state lock.
Q: Is this a bug in terraform apply? A: The state lock issue is not a bug in terraform apply, but rather a feature that prevents concurrent modifications to the Terraform state file. However, the error message and handling of state locks have been improved in recent versions of Terraform, such as Terraform 1.2.5, which provides more detailed error messages and better support for state lock management.
📚 Continue Learning
Check out our guides on terraform apply and Failed.