Melissa Jenner
Melissa Jenner

Reputation: 851

Terraform Error: Error locking state: Error acquiring the state lock: 2 errors occurred:

I tried terraform versions v0.12.26 and v0.13.3. Both failed.

terraform plan

Acquiring state lock. This may take a few moments...

Error: Error locking state: Error acquiring the state lock: 2 errors occurred: * ResourceNotFoundException: Requested resource not found * ResourceNotFoundException: Requested resource not found

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.

Related source code:

terraform {
  backend "s3" {
    encrypt        = false
    bucket         = "dev-terraform-state"
    key            = "dev/Oregon/eks/terraform.tfstate"
    region         = "us-west-2"
    dynamodb_table = "dev-lock-table"
  }
  required_version = ">= 0.12.0"
}

Upvotes: 19

Views: 39410

Answers (10)

user25741018
user25741018

Reputation: 1

Make sure the table created and region specified in below code are same and also make sure the partition key must be LockID.

Upvotes: 0

Mahesh Gaikwad
Mahesh Gaikwad

Reputation: 31

Issue: AWS S3 with Dynamodb backend IF got such error Error loading state: failed to lock s3 state: 2 errors occurred: * operation error DynamoDB: PutItem, https response error StatusCode: 400, RequestID: XXXXXXXXXXXXXXX, api error ValidationException: One or more parameter values were invalid: Missing the key LockId in the item

Error loading state: failed to lock s3 state: 2 errors occurred: * operation error DynamoDB: PutItem, https response error StatusCode: 400, RequestID: XXXXXXXXXXXXXXXXXXX, api error ValidationException: One or more parameter values were invalid: Missing the key LockId in the item

Solution is: while creation bucket i have primary LockId insteand LockID after fix such my issue resolved. Thanks

Upvotes: 0

agrawalramakant
agrawalramakant

Reputation: 170

In my case, as I took forked it from some other repo which operates in some other AWS account, I was having the wrong dynamodb_table. I updated the the dynamodb_table field in my backend config, then it worked fine.

terraform {
  backend "s3" {
    key = <VALID_KEY_NAME>
    region = 'eu-central-1'
    dynamodb_table = <A table which exists>
    encrypt = true
  }
}

This is my problem, you may check if the table exists in dynamodb already or not.

Upvotes: 0

Sasank Irugu
Sasank Irugu

Reputation: 1

Make sure you are using the particular environment Access & Security Credentials.

And also if your profile is set as default make sure you are running the terraform apply for that particular account or you might need to configure the keys again.

Upvotes: 0

Joe
Joe

Reputation: 85

I had this exact error message after I modified my backend.tfvars file and then tried to run terraform plan. To fix it, I reinitialized the IaC:

terraform init -backend-config=backend.tfvars

which generated another error message that instructed me to migrate the existing state:

terraform init -backend-config=backend.tfvars -migrate-state

Migrating the existing state allowed the backend to reconfigure successfully, and I could then run terraform plan.

Upvotes: 0

denmely
denmely

Reputation: 29

It remains to create the table to work, so it will be able to save the state.

enter image description here

IAM

enter image description here

Upvotes: 1

Rajmohan
Rajmohan

Reputation: 11

Check if you're using the same AWS account and correct region. DynamoDB Table must be in the same region as CLI/SDK configuration

Upvotes: 1

Sandeep_Jaina
Sandeep_Jaina

Reputation: 1

Make sure the table created and region specified in below code are same

terraform { backend "s3" { bucket = "" key = "" region = "us-east-1" dynamodb_table =

Upvotes: 0

Nicholas Goh
Nicholas Goh

Reputation: 11

I ran terraform force-unlock <LOCK_ID> (docs) to delete the lock.

Lots of discussion here

Upvotes: 1

Marcin
Marcin

Reputation: 238847

The error is ResourceNotFoundException, which suggests that your dev-lock-table does not exist.

Terraform does not create it. Instead it must exist before you will use it. From docs:

dynamodb_table field to an existing DynamoDB table name.

Upvotes: 26

Related Questions