Learn Build5Nines Forum

Find answers, ask questions, and connect with our
global community of IT professionals.

Forums HashiCorp Terraform Troubleshooting Terraform State Lock Error: ConditionalCheckFailedException

  • Troubleshooting Terraform State Lock Error: ConditionalCheckFailedException

    Posted by Joshua Falken on September 22, 2023 at 11:21 am

    I encountered the following error while running a Terraform plan in my pipeline:

    Error: Error locking state: Error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed

    Lock Info:

    ID: 00000000-0000-0000-0000-000000000000

    Path: ...

    Operation: OperationTypePlan

    Who: ...

    Version: 0.12.25

    Created: 2023-05-29 12:52:25.690864752 +0000 UTC


    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.

    Despite being certain that there are no concurrent plans running, this error occurred. Is there a way to address this issue? How can I release or remove this state lock?

    Joshua Falken replied 2 months, 2 weeks ago 1 Member · 1 Reply
  • 1 Reply
  • Joshua Falken

    September 22, 2023 at 11:21 am

    The error message you’ve encountered, “Error acquiring the state lock: ConditionalCheckFailedException,” relates to Terraform’s state locking mechanism. This mechanism is crucial for ensuring that only one process or user can modify the Terraform state at any given time, thereby preventing conflicts and data corruption. When Terraform performs actions like terraform apply, terraform plan, or terraform refresh, it attempts to acquire a lock on the state file. If it fails to acquire the lock, it indicates that another process is already working with the state file.

    To troubleshoot and address this issue, consider the following steps:

    1. Identify the Source of the Conflict:

      • Even if you are confident that there are no concurrent plans running, it’s essential to determine if any other process or user might be working with the same state file. Ensure effective communication within your team to avoid potential conflicts.
    2. Check for Lingering Operations:

      • Sometimes, a previous Terraform operation (e.g., terraform apply or terraform plan) might be stuck or taking longer to complete than expected. Ensure that there are no long-running or stuck Terraform processes that might be causing lock contention.
    3. Use -lock=false with Caution:

      • As mentioned in the error message, you can disable locking using the -lock=false flag for most Terraform commands. However, this approach should be used sparingly and as a last resort. It is not recommended in multi-user environments, as it can lead to conflicts and data inconsistencies.
    4. Review and Optimize State Backends:

      • If you are using a remote state backend (e.g., S3, Azure Blob Storage, Google Cloud Storage), ensure that it is correctly configured. Verify that there are no issues with the backend configuration that might be contributing to lock contention.
    5. Retry the Terraform Command:

      • Once you have identified and resolved any potential sources of conflict, retry the Terraform command that triggered the error. This should allow Terraform to acquire the state lock successfully.

    By following these steps and maintaining good coordination within your team, you should be able to resolve the “Error acquiring the state lock: ConditionalCheckFailedException” issue and continue working effectively with Terraform.

Log in to reply.