I experienced this issue when trying to run a terraform command for a resource in Google Cloud Console within the GitHub actions workflow.
The issue started after I cancelled a running terraform apply
command.
When I tried to run other terraform commands like the terraform apply -refresh-only
command, I got the error below:
Run terraform apply -var-file env/dev-01-default.tfvars -refresh-only -auto-approve
/runner/_work/_temp/8cdffd5c-b7a1-446d-a294-c1e34b63cde4/terraform-bin apply -var-file env/dev-01-default.tfvars -refresh-only -auto-approve
╷
│ Error: Error acquiring the state lock
│
│ Error message: writing "gs://my-dev/k8s/default/dev-01.tflock" failed:
│ googleapi: Error 412: At least one of the pre-conditions you specified did
│ not hold., conditionNotMet
│ Lock Info:
│ ID: 1688478112611453
│ Path: gs://my-dev/k8s/default/dev-01.tflock
│ Operation: OperationTypeApply
│ Who: runner@pm-runners-m4h7k-gcxqk
│ Version: 1.1.8
│ Created: 2023-07-04 13:41:52.517578046 +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.
╵
Error: Terraform exited with code 1.
Error: Process completed with exit code 1.
Here's how I fixed this:
This issue created a lockfile called dev-01.tflock
in the location gs://my-dev/k8s/default/dev-01.tflock
with a lock-info-id of 1688478112611453
One way is to run the command below:
terraform force-unlock -force <lock-info-id>
Another way is to locate the location of the lockfile in the Google cloud storage account where the statefile is located. And within the same directory where the statefile is stored you will the lockfile. Go ahead to delete the lockfile.
Afterwhich you can run the terraform command you want to run and it will run fine.