Apply finishes all resource changes, then fails with `Failed to save state` and `AccessDenied` mentioning `kms:GenerateDataKey`.
AdvancedTroubleshootingState Locking and Backend Errors
Answer
Likely causes → The bucket uses SSE-KMS with a customer-managed key (kms_key_id in the backend or bucket default encryption). The role can read state (or the old object used a different key) but cannot encrypt new writes.
Diagnose → Look for errored.tfstate in the working directory. Check the key policy and role policy for kms:GenerateDataKey, kms:Encrypt and kms:Decrypt.
aws kms get-key-policy --key-id alias/tfstate --policy-name default
Fix → Grant the KMS permissions, then push the saved state immediately:
terraform state push errored.tfstate
Do not run apply again first; that would work from the old state and duplicate resources.
Prevent → Test backend write permissions in a sandbox stack whenever IAM or KMS policies change.
More on State Locking and Backend Errors
`terraform plan` fails with `Error acquiring the state lock` and `ConditionalCheckFailedException: The conditional request failed`.BeginnerWith the S3 backend and `use_lockfile = true`, you get `Error acquiring the state lock` with `StatusCode: 412` and `PreconditionFailed`.Intermediate`terraform force-unlock` returns `Failed to unlock state: lock ID "..." does not match existing lock`.BeginnerA teammate ran `terraform apply -lock=false` "because the lock was stuck". Now the plan shows resources you never touched being recreated.Advanced`terraform init` fails with `Error: Backend configuration changed`.Beginner`terraform init -migrate-state` asks `Do you want to copy existing state to the new backend?` and the new backend already has a state. What is the danger?Intermediate