Terraform-provider-aws: Dynamodb: error locking state error acquiring the state lock

Created on 23 May 2018  ·  7Comments  ·  Source: hashicorp/terraform-provider-aws

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform Version

Affected Resource(s)

Terraform v0.11.7

  • provider.aws v1.19.0
  • provider.random v0.1.0
  • provider.template v0.1.1
  • aws_XXXXX

Terraform Configuration Files

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file. For
# security, you can also encrypt the files using our GPG public key: https://keybase.io/hashicorp

/*
terraform
*/

terraform {
required_version = ">= 0.11.7"

backend "s3" {
bucket = "terraform-state-non-prd"
key = "nonprd/com/api.tfstate"
region = "us-east-1"
encrypt = "true"
acl = "bucket-owner-full-control"
dynamodb_table = "terraform-state-lock-non-prd"
}
}

/*
provider aws
*/

provider "aws" {
version = "~>1.19.0"
region = "${var.aws_region}"
}

/*
provider random
*/
provider "random" {
version = "~>0.1.0"
}

/*
provider template
*/
provider "template" {
version = "~>0.1.1"
}

Debug Output

Panic Output

Expected Behavior

Actual Behavior

Initializing the backend...
Backend configuration changed!

Terraform has detected that the configuration specified for the backend
has changed. Terraform will now check for existing state in the backends.

Acquiring state lock. This may take a few moments...
Error locking source state: Error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed
status code: 400, request id: 038OGQSKTQS56R8UUREQ1H1RTBVV4KQNSO5AEMVJF6
6Q9ASUAAJG
Lock Info:
ID: 12103daf-700f-da6d-b772-8daecd96eefc
Path: terraform-state-non-prd/nonprd/com/api.tfstate
Operation: OperationTypePlan
Who: COMPANYuser_id
Version: 0.11.7
Created: 2018-05-23 18:26:41.9308391 +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.

//aws dynamodb table

Item{2}

InfoString: {"ID":"df9e88a3-3c6e-61f6-5aac-aca87bbc2601","Operation":"OperationTypePlan","Info":"","Who":"COMPANY\\user@id","Version":"0.11.7","Created":"2018-05-23T16:29:20.0607225Z","Path":"terraform-state-non-prd/nonprd/com/api.tfstate"}

LockIDString: terraform-state-non-prd/nonprd/com/out.tfstate

Steps to Reproduce

  1. terraform apply
    terraform init
    terraform plan

    Important Factoids

References

  • #0000
question servicdynamodb upstream-terraform

Most helpful comment

Hi @tonygyerr 👋 Did this happen under some repeatable circumstance? Are you able to get out of the situation by using the lock ID with terraform force-unlock?

Please see the state locking documentation for more information.

All 7 comments

Hi @tonygyerr 👋 Did this happen under some repeatable circumstance? Are you able to get out of the situation by using the lock ID with terraform force-unlock?

Please see the state locking documentation for more information.

I also tried using force unlock it is still returning the same error message:
$ terraform force-unlock 12103daf-700f-da6d-b772-8daecd96eefc

message
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.

$ terraform plan

- runs for 3 seconds and returns nothing

i believe this may possibly happen because it is running the dynamodb

aws dynamodb create-table
--region us-east-1
--table-name api-nonprd-statelock
--attribute-definitions AttributeName=LockID,AttributeType=S
--key-schema AttributeName=LockID,KeyType=HASH

--provisioned-throughput ReadCapacityUnits=20,WriteCapacityUnits=20

info string: {"ID":"12103daf-700f-da6d-b772-8daecd96eefc","Operation":"OperationTypePlan","Info":"","Who":"COMPANY\\user@id","Version":"0.11.7","Created":"2018-05-23T18:26:41.9308391Z","Path":"terraform-state-non-prd/nonprd/com/api.tfstate"}

lockID: LockID string: terraform-state-non-prd/nonprd/com/api.tfstate

@tonygyerr Something like that happened to me because I messed up with dynamodb permissions. I fixed the permissions, delete and recreate the table and all was ok.

rename your terraform.tfstate file. Looks like it got corrupted
I got an alike problem and I renamed the file

Error: Error locking state: Error acquiring the state lock: open terraform.tfstate: permission denied

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.

aryan@aryan-VirtualBox:~/docker$ ls

main.tf terraform.tfstate

aryan@aryan-VirtualBox:~/docker$ ll

total 20

drwxr-xr-x 3 aryan aryan 4096 May 7 14:07 ./

drwxr-xr-x 21 aryan aryan 4096 May 7 13:57 ../

-rw-r--r-- 1 aryan aryan 279 May 7 13:57 main.tf

drwxr-xr-x 3 aryan aryan 4096 May 7 12:26 .terraform/

-rw-r--r-- 1 root root 1161 May 7 12:37 terraform.tfstate

aryan@aryan-VirtualBox:~/docker$ ls .terraform/

plugins

aryan@aryan-VirtualBox:~/docker$ less terraform.tfstate

aryan@aryan-VirtualBox:~/docker$ mv terraform.tfstate old_terraform.tfstate

What helped me today for this problem is that removing the entry of the lock acquisition in DynamoDB manually. (I kept the LockID entry as it is.)
Post that, Terraform didn't complain at all.
Thanks all.

Thank you for using Terraform and for opening up this question, @tonygyerr! Issues on GitHub are intended to be related to bugs or feature requests with the provider codebase. It looks like you've gotten some great feedback on this question. If needed, please use https://discuss.hashicorp.com/c/terraform-providers for additional feedback, community discussions, and questions around Terraform.

If you believe that your issue was miscategorized as a question or closed in error, please create a new issue using one of the following provided templates: bug report or feature request. Please make sure to provide us with the appropriate information so we can best determine how to assist with the given issue.

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings