Skip to content

Troubleshooting

Overview

Error acquiring the state lock

State Lock is a special case when.

The error will like this

$ terraform apply -auto-approve
# Acquiring state lock. This may take a few moments...
# ╷
# │ Error: Error acquiring the state lock
# │
# │ Error message: writing "gs://<STATE_BUCKET>/terraform/state/default.tflock" failed: googleapi: Error 412: At least one of the pre-conditions you specified did not hold., conditionNotMet
# │ Lock Info:
# │   ID:        1682427451294142
# │   Path:      gs://<STATE_BUCKET>/terraform/state/default.tflock
# │   Operation: OperationTypeApply
# │   Who:       <USER>
# │   Version:   1.2.5
# │   Created:   2023-04-24 06:24:11.0927623 +0000 UTC
# │   Info:
# │
# │
# │ 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.

so what is happend

Happended when:

Cause of Error This error usually appears when one process fails running terraform plan or terraform apply. For example if your network connection interrupts or the process is terminated before finishing. Then Terraform "thinks" that this process is still working on the infrastructure and blocks other processes from working with the same infrastructure and state at the same time in order to avoid conflicts.

As stated in the error message, you should make sure that there is really no other process still running (e.g. from another developer or from some build-automation). If you force-unlock in such a situation you might screw up your terraform state, making it hard to recover.

Solution If there is no other process still running: run this command

https://stackoverflow.com/questions/62189825/terraform-error-acquiring-the-state-lock-conditionalcheckfailedexception

terraform force-unlock 9db590f1-b6fe-c5f2-2678-8804f089deba

How to fix problem

  1. Using -lock=false

  2. Using force-unlock

$ terraform force-unlock
Expected a single argument: LOCK_ID
Usage: terraform [global options] force-unlock LOCK_ID

  Manually unlock the state for the defined configuration.

  This will not modify your infrastructure. This command removes the lock on the
  state for the current workspace. The behavior of this lock is dependent
  on the backend being used. Local state files cannot be unlocked by another
  process.

Options:

  -force                 Don't ask for input for unlock confirmation.

but where is LOCK_ID?

LOCK_ID is in the lock,you can re-run the command then a

as

</LOCK_ID> 9db590f1-b6fe-c5f2-2678-8804f089deba
$ terraform force-unlock <LOCK_ID>
Do you really want to force-unlock?
  Terraform will remove the lock on the remote state.
  This will allow local Terraform commands to modify this state, even though it
  may be still be in use. Only 'yes' will be accepted to confirm.

  Enter a value: yes

Terraform state has been successfully unlocked!

The state has been unlocked, and Terraform commands should now be able to
obtain a new lock on the remote state.

You must promt "yes" and make sure no other process are running, such as any CICD pipeline

See: https://stackoverflow.com/questions/62189825/terraform-error-acquiring-the-state-lock-conditionalcheckfailedexception

Error containing objects without force_destroy set to true

When meet this

│ Error: Error trying to delete bucket [$BUCKET_NAME] containing objects without force_destroy set to true

Reason:

When this has been config force_destroy

Handle

  • [1] Remove resource by state

  • [2] Delete resource by UI

  • [3] Re-run terraform plan

Rename resource name without revalidate element

$ terraform state mv google_project_iam_member.sa_meepo google_project_iam_member.deploy_internal_docs Move "google_project_iam_member.sa_meepo" to "google_project_iam_member.deploy_internal_docs" Successfully moved 1 object(s).

Index brackets must contain either a literal number or a literal string

When reference index

terraform state list  google_storage_bucket.gcs_projects
terraform state rm google_storage_bucket.gcs_projects["processor"]
% %  Error: Index value required
% %    on  line 1:
%    (source code not available)
% %  Index brackets must contain either a literal number or a literal string.
% 

Solutuon:

terraform state rm google_storage_bucket.gcs_projects[\"processor\"]

See: https://github.com/hashicorp/terraform-provider-aws/issues/11591