State Locking
State locking stops two Terraform runs from writing to the same state file together. One run holds the lock while the other waits or fails.
Name the lock table
The backend block gains one line for the table.
terraform {
backend "s3" {
bucket = "terraform-state-demo"
key = "demo/terraform.tfstate"
region = "us-east-1"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
use_path_style = true
dynamodb_table = "terraform-locks"
use_lockfile = true
}
}Create the lock table first
Like the bucket, the table is bootstrapped ahead of time. Add the DynamoDB endpoint to the bootstrap provider first, so the table is created in the emulator and not in a real account.
resource "aws_dynamodb_table" "locks" {
name = "terraform-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}What the second run sees
Run a second apply while the first is still running. Terraform stops with a lock error that names the run holding the lock.
terraform apply -auto-approveExample
# Step 1: bootstrap the bucket and the lock table (in a folder named bootstrap).
# The bucket uses the configuration from the last lesson, with the dynamodb endpoint added to the provider.
# The lock table uses the resource from the second snippet above.
# Run terraform init and terraform apply -auto-approve there.
# Step 2: main configuration (in a folder named demo).
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
backend "s3" {
bucket = "terraform-state-demo"
key = "demo/terraform.tfstate"
region = "us-east-1"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
use_path_style = true
dynamodb_table = "terraform-locks"
use_lockfile = true
endpoints = {
s3 = "http://localhost:5000"
dynamodb = "http://localhost:5000"
}
}
}
provider "aws" {
region = "us-east-1"
s3_use_path_style = true
endpoints {
s3 = "http://localhost:5000"
dynamodb = "http://localhost:5000"
}
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
}
resource "aws_s3_bucket" "demo" {
bucket = "demo-state-locking"
}
# commands (with dummy credentials for the emulator)
export AWS_ACCESS_KEY_ID="test"
export AWS_SECRET_ACCESS_KEY="test"
terraform init -migrate-state
terraform apply -auto-approve
terraform destroy -auto-approve
# Then remove the bootstrap bucket and lock table too, from the bootstrap folder.Key points
- Two applies at once can corrupt state. Each run writes over the other.
- A lock lets one run through. The second run stops with an error.
- A DynamoDB table holds the lock for the S3 backend. The backend names the table.
- The S3 backend also supports a native lockfile. Set use_lockfile to true.
- A lock error names the run that holds the lock. Wait for it, or check that it is really stuck.
The backend now names a lock table, so a second apply while this one runs would stop with a lock error. The table and the bucket were bootstrapped first, because a backend cannot create its own lock table. The bucket and table in this lesson are mocks on localhost, not real AWS resources.
Note
force-unlock is a last resort. Use it only when the other run is certainly dead.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What happens when a second apply starts while one holds the lock?