Interview Prep/Terraform/State Locking and Backend Errors

`Unable to list objects in S3 bucket "acme-tfstate" with prefix "env:/": operation error S3: ListObjectsV2, https response error StatusCode: 403`.

IntermediateTroubleshootingState Locking and Backend Errors
Answer

Likely causes → The role can read/write the state object but lacks s3:ListBucket on the bucket. Terraform lists env:/ to discover workspaces during init.

Diagnose →

aws s3api list-objects-v2 --bucket acme-tfstate --prefix env:/ --max-items 1
aws iam simulate-principal-policy --policy-source-arn arn:aws:iam::111122223333:role/tf-ci \
  --action-names s3:ListBucket --resource-arns arn:aws:s3:::acme-tfstate

Fix → Grant s3:ListBucket on arn:aws:s3:::acme-tfstate (optionally conditioned on s3:prefix), plus s3:GetObject/s3:PutObject on the key, and s3:DeleteObject for .tflock if using native locking.

Prevent → Keep a tested least-privilege backend policy as a module and attach it to every Terraform role.

All Terraform questions Roadmap: Terraform in production

More on State Locking and Backend Errors