Interview Prep/Terraform/State Locking and Backend Errors

`terraform init` fails on the backend with `No valid credential sources found`, but `aws s3 ls` works in the same shell.

IntermediateTroubleshootingState Locking and Backend Errors
Answer

Likely causes → The backend uses its own credential settings: profile or assume_role in the backend block refers to a profile that does not exist on this machine; AWS_PROFILE differs; the SSO session for that profile expired.

Diagnose →

grep -A8 'backend "s3"' *.tf; cat backends/prod.hcl
aws sts get-caller-identity --profile <profile-from-backend>
TF_LOG=DEBUG terraform init 2>&1 | grep -i -E 'credential|profile|assume'

Fix → Align the backend profile/assume_role with what exists, or remove profile and rely on environment credentials. Run aws sso login --profile ... if the session expired.

Prevent → Keep the backend free of personal profile names. Prefer assume_role { role_arn = ... } with environment base credentials so laptops and CI behave the same.

All Terraform questions Roadmap: Terraform in production

More on State Locking and Backend Errors