$ run as a normal user# run as root (or with sudo)
The quick answer
terraform {
required_version = ">= 1.10"
backend "s3" {
bucket = "acme-tfstate-111122223333"
key = "shop/prod/terraform.tfstate"
region = "eu-west-1"
encrypt = true
use_lockfile = true
}
}terraform init -migrate-stateStep by step
- Create the bucket once (by hand or in a small "bootstrap" Terraform project) — private, versioned, encrypted:
aws s3api create-bucket --bucket acme-tfstate-111122223333 --region eu-west-1 --create-bucket-configuration LocationConstraint=eu-west-1
aws s3api put-bucket-versioning --bucket acme-tfstate-111122223333 --versioning-configuration Status=Enabled
aws s3api put-public-access-block --bucket acme-tfstate-111122223333 --public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true
aws s3api put-bucket-encryption --bucket acme-tfstate-111122223333 --server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"aws:kms"}}]}'Versioning is your undo button: every state write keeps the old version.
- One key per project and environment — never one state for everything:
| Key | Holds |
|---|---|
network/prod/terraform.tfstate | VPC, subnets of prod. |
shop/prod/terraform.tfstate | The shop app in prod. |
shop/dev/terraform.tfstate | The shop app in dev. |
- Move the existing local state to S3:
terraform init -migrate-state
Do you want to copy existing state to the new backend? yes
Successfully configured the backend "s3"!
terraform state list | head- How the lock works: during
plan/applyTerraform writesshop/prod/terraform.tfstate.tflocknext to the state. A second run waits or stops:
terraform apply -lock-timeout=5m
Error acquiring the state lock
Lock Info:
ID: 7b3c1f0e-1a2b-4c3d-9e8f-0a1b2c3d4e5f
Who: alice@build-01- The IAM rights the pipeline role needs (only on its own keys):
s3:ListBucketon the bucket, ands3:GetObject,s3:PutObject,s3:DeleteObjectonshop/prod/terraform.tfstateandshop/prod/terraform.tfstate.tflock, pluskms:Encrypt/kms:Decrypton the key. - Protect the state like a password store — it can contain secrets (database passwords, keys) in plain text: few people may read the bucket, CloudTrail on, no public access, no copy on laptops.
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
Error acquiring the state lock | Another run is busy — or a crashed run left the lock. | Wait. Only if you are sure no run is active: terraform force-unlock LOCK_ID. |
AccessDenied on the .tflock object | The IAM policy allows only the state key. | Add the .tflock key to the policy. |
Backend configuration changed | You changed the backend block. | terraform init -reconfigure (new place, no copy) or -migrate-state (copy). |
Unsupported argument "use_lockfile" | Terraform older than 1.10. | Update Terraform (or keep the DynamoDB lock table on old versions). |
| Wrong state was overwritten | Two projects used the same key. | Restore the old version from S3 versioning; use unique keys. |
| Secrets visible in the state | Terraform stores resource attributes. | Restrict bucket access; use ephemeral values / secret managers where you can. |
Go further
- Command pages: terraform, aws.
- Next how-tos: Plan and apply Terraform safely, Import an existing resource, Separate environments.
- Learn it: Terraform — Remote state, Terraform — Locking, Terraform — Secrets in state.
- Full guides: Install Terraform, AWS CLI, OpenTofu.
- Practise: Lab: work with state — inspect, move, import, drift.
1Since which Terraform version can the S3 backend lock without DynamoDB?
1.10 — with use_lockfile = true.
2Why turn on versioning on the state bucket?
Every state write keeps the old version, so a broken or overwritten state can be restored.