Troubleshooting
Real Terraform failures with the exact error text: state locks, lost state, imports, cycles, provider conflicts, bad plans, AWS auth, modules, workspaces and CI
State Locking and Backend Errors
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q1 terraform plan fails with Error acquiring the state lock and ConditionalCheckFailedException: The conditional request failed.
Beginner
terraform plan fails with Error acquiring the state lock and ConditionalCheckFailedException: The conditional request failed.Likely causes โ Another plan/apply holds the lock right now; a previous run crashed (CI runner killed, laptop closed, Ctrl-C twice) and left the lock item in DynamoDB.
Diagnose โ The error prints Lock Info with ID, Who, Operation and Created. Check whether that person or pipeline is still running.
aws dynamodb scan --table-name terraform-locks \
--query 'Items[].{id:LockID.S,info:Info.S}'Fix โ If a run is active, wait. If the holder is confirmed dead, release it with the lock ID from the message:
terraform force-unlock 6f1c2a9e-3b1d-4c7a-9a0e-2d5f8f1b7c11Prevent โ Serialize pipelines per stack (CI concurrency groups), give CI jobs a graceful timeout so Terraform can release the lock, and never use -lock=false on shared state.
Q2 With the S3 backend and use_lockfile = true, you get Error acquiring the state lock with StatusCode: 412 and PreconditionFailed.
Intermediate
use_lockfile = true, you get Error acquiring the state lock with StatusCode: 412 and PreconditionFailed.Likely causes โ Terraform 1.10+ S3-native locking writes a <key>.tflock object with a conditional If-None-Match PUT. A 412 means that lock object already exists: another run holds it or a dead run left it behind.
Diagnose โ
aws s3api head-object --bucket acme-tfstate --key prod/network/terraform.tfstate.tflock
aws s3 cp s3://acme-tfstate/prod/network/terraform.tfstate.tflock - # shows Who / CreatedFix โ Confirm no run is active, then terraform force-unlock <ID>. Deleting the .tflock object by hand works too but is the last resort.
Prevent โ Same as DynamoDB locking: one pipeline per state key at a time, graceful job cancellation. Make sure the bucket policy allows s3:PutObject and s3:DeleteObject on *.tflock, otherwise unlocks silently fail.
Q3 terraform force-unlock returns Failed to unlock state: lock ID "..." does not match existing lock.
Beginner
terraform force-unlock returns Failed to unlock state: lock ID "..." does not match existing lock.Likely causes โ You copied an old lock ID; the original lock was already released and a new run has taken a fresh lock; or you are pointed at a different workspace/key.
Diagnose โ Re-run terraform plan to see the current Lock Info, and check terraform workspace show and the backend key.
Fix โ Use the ID from the latest error message. If the new lock belongs to an active run, do not unlock it.
Prevent โ Treat force-unlock as a manual, logged action. Paste the exact Lock Info into the incident/PR before unlocking.
Q4 A teammate ran terraform apply -lock=false "because the lock was stuck". Now the plan shows resources you never touched being recreated.
Advanced
terraform apply -lock=false "because the lock was stuck". Now the plan shows resources you never touched being recreated.Likely causes โ Two applies wrote state concurrently. The last writer won, so the state no longer records objects the other run created. Terraform now plans to create (duplicate) or destroy things based on an incomplete state.
Diagnose โ
aws s3api list-object-versions --bucket acme-tfstate --prefix prod/app/terraform.tfstate \
--query 'Versions[].{v:VersionId,t:LastModified,size:Size}'
terraform state list | sort > now.txtDownload the previous versions and diff terraform state list output between them.
Fix โ Freeze applies. Either restore the most complete state version and terraform import what the other run created, or keep the current one and import the missing objects. Re-plan until it shows only intended changes.
Prevent โ Remove the habit: block -lock=false in CI wrappers, and teach force-unlock with verification instead.
Q5 terraform init fails with Error: Backend configuration changed.
Beginner
terraform init fails with Error: Backend configuration changed.Likely causes โ You edited the backend block (bucket, key, region, role) or passed different -backend-config values than the ones cached in .terraform/terraform.tfstate.
Diagnose โ
cat .terraform/terraform.tfstate | jq '.backend.config | {bucket,key,region}'Compare with the backend block and your -backend-config file.
Fix โ
- You want to move state to the new location:
terraform init -migrate-state. - The state is already at the new location, or you only switched env config locally:
terraform init -reconfigure.
Prevent โ In CI, always run terraform init -reconfigure -backend-config=env/prod.hcl in a clean workspace so no stale .terraform is reused.
Q6 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
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?Likely causes โ The target key already contains state, often because someone already migrated, or two stacks were pointed at the same key by mistake.
Diagnose โ
terraform state pull > local-copy.json
aws s3 cp s3://acme-tfstate/prod/app/terraform.tfstate remote.json
jq '.lineage, .serial, (.resources|length)' local-copy.json remote.jsonDifferent lineage means they are unrelated states.
Fix โ Answer no until you know which state is authoritative. If both are real, use different keys. If one is a stale copy, keep the newer/complete one.
Prevent โ Use one unique key per stack (<env>/<component>/terraform.tfstate) and enable bucket versioning so an overwrite is recoverable.
Q7 Error: Backend initialization required, please run "terraform init" with Reason: Initial configuration of the requested backend "s3".
Beginner
Error: Backend initialization required, please run "terraform init" with Reason: Initial configuration of the requested backend "s3".Likely causes โ Fresh clone or new CI runner with no .terraform/ directory; you added a backend block to a stack that used local state.
Diagnose โ ls -la .terraform/ โ it is missing or has no terraform.tfstate.
Fix โ Run terraform init (with -backend-config if you use partial config). If you are converting from local state, terraform init -migrate-state and confirm the copy.
Prevent โ Make init the first step of every pipeline job, including apply jobs that consume a saved plan.
Q8 terraform init fails with Error: Variables not allowed pointing at the backend "s3" block.
Beginner
terraform init fails with Error: Variables not allowed pointing at the backend "s3" block.Likely causes โ You wrote bucket = var.state_bucket or key = "${var.env}/terraform.tfstate". The backend is initialized before variables are evaluated, so no variables, locals or functions are allowed there.
Diagnose โ Read the line number in the error; it points into the terraform { backend ... } block.
Fix โ Use partial configuration:
terraform {
backend "s3" {}
}terraform init -backend-config=backends/prod.hclPrevent โ Keep a backends/<env>.hcl per environment, or let Terragrunt generate the backend block.
Q9 In CI, terraform init hangs forever after printing key The path to the state file inside the bucket Enter a value:.
Intermediate
terraform init hangs forever after printing key The path to the state file inside the bucket Enter a value:.Likely causes โ Partial backend config with a required setting missing (the -backend-config file path is wrong, or the file lacks key). Terraform falls back to an interactive prompt.
Diagnose โ Check the job's working directory and that the backend file exists: ls backends/ && cat backends/prod.hcl.
Fix โ Pass the correct file and disable prompts:
terraform init -input=false -backend-config=backends/prod.hclPrevent โ Always use -input=false in automation and set TF_IN_AUTOMATION=1. A missing value then fails fast instead of hanging until the job timeout.
Q10 Error: Failed to get existing workspaces: S3 bucket "acme-tfstate" does not exist.
Beginner
Error: Failed to get existing workspaces: S3 bucket "acme-tfstate" does not exist.Likely causes โ Typo in the bucket name; the bootstrap stack that creates the state bucket was never applied; you are using credentials for a different AWS account.
Diagnose โ
aws sts get-caller-identity
aws s3api head-bucket --bucket acme-tfstateFix โ Fix the name or switch to the correct account/profile. If it truly doesn't exist, apply the bootstrap stack (local state, then migrate it into the bucket).
Prevent โ Keep backend config in versioned backends/*.hcl files and print aws sts get-caller-identity at the start of every pipeline.
Q11 Unable to list objects in S3 bucket "acme-tfstate" with prefix "env:/": operation error S3: ListObjectsV2, https response error StatusCode: 403.
Intermediate
Unable to list objects in S3 bucket "acme-tfstate" with prefix "env:/": operation error S3: ListObjectsV2, https response error StatusCode: 403.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-tfstateFix โ 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.
Q12 Apply finishes all resource changes, then fails with Failed to save state and AccessDenied mentioning kms:GenerateDataKey.
Advanced
Failed to save state and AccessDenied mentioning kms:GenerateDataKey.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 defaultFix โ Grant the KMS permissions, then push the saved state immediately:
terraform state push errored.tfstateDo 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.
Q13 Error: Failed to persist state to backend and Terraform says it wrote errored.tfstate. The CI job ended. What now?
Advanced
Error: Failed to persist state to backend and Terraform says it wrote errored.tfstate. The CI job ended. What now?Likely causes โ Network drop, expired credentials or a permission issue at the moment of writing state. The cloud resources were changed, but the remote state was not updated.
Diagnose โ Find the errored.tfstate file. In CI it is lost unless you upload the working directory on failure. Check what the run created from the apply log.
Fix โ
- If you have the file: fix credentials, then
terraform state push errored.tfstate. - If the file is gone: the state is behind reality. Use the apply log to list created resource IDs and
terraform importthem (or useimportblocks), then re-plan.
Prevent โ In pipelines, upload errored.tfstate as an artifact on failure (if: failure()), and use credentials whose session lasts longer than the longest apply.
Q14 terraform state push fails with cannot import state with lineage "a1..." over unrelated state with lineage "b2...".
Intermediate
terraform state push fails with cannot import state with lineage "a1..." over unrelated state with lineage "b2...".Likely causes โ The file you push came from a different stack or a re-created state (new lineage). Terraform protects you from overwriting an unrelated state.
Diagnose โ
terraform state pull | jq '.lineage, .serial'
jq '.lineage, .serial' errored.tfstateFix โ Make sure you are in the right directory/workspace/backend key. Only if you are certain the file is the correct one, use terraform state push -force file.tfstate, after taking a state pull backup.
Prevent โ Never copy state files between stacks. Use terraform state mv -state-out or removed + import for moving resources.
Q15 Error refreshing state: state data in S3 does not have the expected content. The checksum calculated for the state stored in S3 does not match the checksum stored in DynamoDB.
Advanced
Error refreshing state: state data in S3 does not have the expected content. The checksum calculated for the state stored in S3 does not match the checksum stored in DynamoDB.Likely causes โ With the legacy DynamoDB lock table, Terraform also stores an MD5 digest (<bucket>/<key>-md5 item). Someone uploaded or restored state directly in S3, or a previous write failed halfway.
Diagnose โ Check S3 object versions and the -md5 item in the table:
aws dynamodb get-item --table-name terraform-locks \
--key '{"LockID":{"S":"acme-tfstate/prod/app/terraform.tfstate-md5"}}'Fix โ Wait a minute and retry (it can be a delay). If it persists and the S3 object is the version you want, update the Digest to the "Calculated checksum" shown in the error, or delete the -md5 item.
Prevent โ Restore state through terraform state push, not aws s3 cp. Moving to use_lockfile = true (Terraform 1.10+) removes the DynamoDB digest entirely.
Q16 After upgrading to Terraform 1.11 you see Warning: Deprecated Parameter for dynamodb_table. On an older laptop use_lockfile gives An argument named "use_lockfile" is not expected here.
Intermediate
Warning: Deprecated Parameter for dynamodb_table. On an older laptop use_lockfile gives An argument named "use_lockfile" is not expected here.Likely causes โ DynamoDB locking is deprecated in favour of S3-native locking. use_lockfile was added in 1.10, so older binaries reject it.
Diagnose โ terraform version on each machine and CI image; check required_version.
Fix โ Standardize on 1.10+ everywhere, then migrate in two steps: set both use_lockfile = true and dynamodb_table (both locks are taken), roll out, then remove dynamodb_table.
Prevent โ Pin required_version = ">= 1.10, < 2.0" and the CI image version, and use tfenv/mise with a .terraform-version file.
Q17 Error: Failed to get existing workspaces: ... StatusCode: 301 ... requested bucket from "us-east-1", actual location "ap-south-1".
Beginner
Error: Failed to get existing workspaces: ... StatusCode: 301 ... requested bucket from "us-east-1", actual location "ap-south-1".Likely causes โ The backend region doesn't match the bucket's region. The backend region is independent of the provider region.
Diagnose โ aws s3api get-bucket-location --bucket acme-tfstate
Fix โ Set region = "ap-south-1" in the backend config and run terraform init -reconfigure.
Prevent โ Keep the state bucket region in the shared backends/*.hcl files, not inferred from AWS_REGION.
Q18 terraform init fails on the backend with No valid credential sources found, but aws s3 ls works in the same shell.
Intermediate
terraform init fails on the backend with No valid credential sources found, but aws s3 ls works in the same shell.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.
Q19 Two pipelines for different stacks keep blocking each other with Error acquiring the state lock, though they manage unrelated resources.
Intermediate
Error acquiring the state lock, though they manage unrelated resources.Likely causes โ Both stacks use the same backend key (copy-pasted backend.tf), so they share one state and one lock. That's worse than locking: each apply wants to destroy the other's resources.
Diagnose โ
grep -rn 'key' --include='*.tf' --include='*.hcl' . | grep tfstate
terraform state list | headIf one stack's state list shows the other's resources, they share state.
Fix โ Give each stack its own key. Split the shared state with terraform state mv -state-out= or removed/import blocks into the new key.
Prevent โ Derive keys from directory path (Terragrunt path_relative_to_include()), or add a CI check that no two stacks use the same key.
Q20 Error: Error acquiring the state lock with AccessDeniedException: User: arn:aws:sts::...:assumed-role/tf-ci/... is not authorized to perform: dynamodb:PutItem.
Beginner
Error: Error acquiring the state lock with AccessDeniedException: User: arn:aws:sts::...:assumed-role/tf-ci/... is not authorized to perform: dynamodb:PutItem.Likely causes โ The role has S3 access but no DynamoDB permissions on the lock table, or the table is in another region/account.
Diagnose โ aws dynamodb describe-table --table-name terraform-locks --region <backend-region>
Fix โ Grant dynamodb:GetItem, dynamodb:PutItem, dynamodb:DeleteItem (and DescribeTable) on the table ARN. Or switch to use_lockfile = true and drop DynamoDB.
Prevent โ Ship backend permissions as one reusable IAM policy so S3, KMS and locking are granted together.
Q21 terraform plan takes 10 minutes and then fails with Error acquiring the state lock and context deadline exceeded, only from the corporate network.
Intermediate
terraform plan takes 10 minutes and then fails with Error acquiring the state lock and context deadline exceeded, only from the corporate network.Likely causes โ A proxy or firewall blocks or slows the DynamoDB/S3 endpoint; HTTPS_PROXY is set but NO_PROXY routes VPC endpoints incorrectly; DNS resolves S3 to a private endpoint you can't reach.
Diagnose โ
env | grep -i proxy
TF_LOG=DEBUG terraform plan 2>&1 | grep -E 'dynamodb|s3\.|timeout'
curl -sv https://dynamodb.ap-south-1.amazonaws.com 2>&1 | headFix โ Configure the proxy correctly (and trust its CA via the OS store or AWS_CA_BUNDLE), or run Terraform from a runner inside the VPC.
Prevent โ Run shared-state Terraform from controlled runners, not from laptops on varied networks. -lock-timeout=5m helps only with genuine contention, not network issues.
Q22 Apply was cancelled in GitHub Actions. The next run fails with the state lock held by Who: runner@fv-az123 and the plan shows half the resources changed.
Advanced
Who: runner@fv-az123 and the plan shows half the resources changed.Likely causes โ Cancelling a job sends SIGINT then kills the process after a short grace period. Terraform could not finish writing state or releasing the lock.
Diagnose โ Check the cancelled run's log for the last Creation complete / Modifications complete lines and whether errored.tfstate was uploaded. Confirm no other runner is active.
Fix โ terraform force-unlock <ID>, then terraform plan. Resources finished before the kill are usually in state (Terraform persists state periodically during apply). Import anything the log shows as created but the plan wants to create again.
Prevent โ Don't cancel applies; use concurrency: { group: tf-prod, cancel-in-progress: false }. Set timeout-minutes generously and upload errored.tfstate on failure.
Q23 HCP Terraform: terraform plan from a laptop says Error: Required token could not be found for app.terraform.io.
Beginner
terraform plan from a laptop says Error: Required token could not be found for app.terraform.io.Likely causes โ The cloud {} block (or remote backend) needs an API token; you haven't logged in, or the token in ~/.terraform.d/credentials.tfrc.json expired or was revoked.
Diagnose โ cat ~/.terraform.d/credentials.tfrc.json | jq 'keys'; check TF_TOKEN_app_terraform_io in CI.
Fix โ terraform login on a laptop. In CI set TF_TOKEN_app_terraform_io to a team or user token from a secret.
Prevent โ Use team tokens for automation with an expiry, rotated through your secret manager.
Q24 HCP Terraform: the run sits in Pending or Plan queued for a long time and nothing happens.
Intermediate
Pending or Plan queued for a long time and nothing happens.Likely causes โ Another run in that workspace is waiting for confirmation (Needs confirmation), so later runs queue behind it; the workspace is locked manually; agent-pool workspaces have no healthy agent; the organization hit its concurrency limit.
Diagnose โ Open the workspace's Runs list and the agent pool status. Check Settings > Locking.
Fix โ Apply or discard the run that awaits confirmation, unlock the workspace, or restart/scale the agents.
Prevent โ Auto-discard stale speculative runs, keep agent pools sized with health checks, and alert on runs pending longer than N minutes.
Q25 terraform init warns Warning: Missing backend configuration and creates a local terraform.tfstate in CI. Later, the next pipeline wants to create everything again.
Advanced
terraform init warns Warning: Missing backend configuration and creates a local terraform.tfstate in CI. Later, the next pipeline wants to create everything again.Likely causes โ -backend-config was passed but there is no backend block in the code (so Terraform ignores it and uses local state), or the backend block lives in a file excluded from the job. Each CI run starts with an empty local state.
Diagnose โ
grep -rn 'backend\s*"' *.tf
ls terraform.tfstate .terraform/terraform.tfstateFix โ Add the backend block. If a CI run already created resources with local state that was thrown away, import them into the remote state or delete the orphans manually.
Prevent โ Fail the pipeline if terraform.tfstate exists in the working directory after apply, and enforce a backend via policy or a wrapper script.
Lost, Corrupted and Drifted State
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q26 Someone deleted the state object from the S3 bucket. terraform plan now wants to create 140 resources that already exist.
Intermediate
terraform plan now wants to create 140 resources that already exist.Likely causes โ The object was deleted (by hand, by a lifecycle rule or by a cleanup script). With versioning on, S3 only added a delete marker.
Diagnose โ
aws s3api list-object-versions --bucket acme-tfstate --prefix prod/app/terraform.tfstate \
--query '{versions:Versions[].[VersionId,LastModified,Size],markers:DeleteMarkers[].[VersionId,LastModified]}'Fix โ Do not apply. Remove the delete marker (restores the latest version) or copy a known-good version back:
aws s3api delete-object --bucket acme-tfstate --key prod/app/terraform.tfstate --version-id <delete-marker-id>
terraform plan # should show no or small changesPrevent โ Versioning + MFA delete or a bucket policy denying s3:DeleteObject on *.tfstate for humans, and a noncurrent-version retention lifecycle rule (for example 90 days).
Q27 State is gone and the bucket had no versioning. How do you recover a production stack?
Advanced
Likely causes โ Unversioned bucket, local state on a lost laptop, or a CI workspace that used local state.
Diagnose โ Search for any copy first: terraform.tfstate.backup files, CI artifacts, old laptops, HCP state history. Inventory real resources by tag:
aws resourcegroupstaggingapi get-resources --tag-filters Key=stack,Values=prod-app \
--query 'ResourceTagMappingList[].ResourceARN'Fix โ Rebuild state with import blocks (Terraform 1.5+), one per resource, in a fresh empty state:
import {
to = aws_vpc.main
id = "vpc-0a1b2c3d4e5f67890"
}Iterate terraform plan until it shows 0 to add, 0 to change, 0 to destroy, fixing code where it differs.
Prevent โ Versioned, KMS-encrypted remote state with restricted delete, plus consistent tagging (stack, env, managed-by=terraform) so an inventory is possible.
Q28 Error: Failed to load state followed by a JSON error like invalid character '}' looking for beginning of object key string.
Intermediate
Error: Failed to load state followed by a JSON error like invalid character '}' looking for beginning of object key string.Likely causes โ Someone edited state by hand (trailing comma, bad merge) and uploaded it, or a truncated write from an interrupted upload.
Diagnose โ
terraform state pull > broken.json 2>/dev/null || aws s3 cp s3://acme-tfstate/prod/app/terraform.tfstate broken.json
jq . broken.json > /dev/null # shows the line of the syntax errorFix โ Restore the previous S3 version. If no good version exists, fix the JSON carefully, bump serial by one, and terraform state push.
Prevent โ Never hand-edit state. Use terraform state mv/rm, moved, removed and import blocks; they validate and back up.
Q29 The plan prints Note: Objects have changed outside of Terraform and lists a security group with an extra ingress rule.
Beginner
Note: Objects have changed outside of Terraform and lists a security group with an extra ingress rule.Likely causes โ Drift: someone added a rule in the console or CLI during an incident.
Diagnose โ
terraform plan -refresh-only
aws ec2 describe-security-groups --group-ids sg-0abc123 --query 'SecurityGroups[0].IpPermissions'CloudTrail (AuthorizeSecurityGroupIngress) tells you who and when.
Fix โ Decide: if the rule is wanted, add it to code. If not, a normal terraform apply removes it. Never just apply blindly during an incident; ask first.
Prevent โ Read-only console access in prod, break-glass roles with alerts, and nightly terraform plan -detailed-exitcode drift checks.
Q30 Every plan wants to change desired_capacity on an Auto Scaling group back to 2, though autoscaling has raised it to 6.
Beginner
desired_capacity on an Auto Scaling group back to 2, though autoscaling has raised it to 6.Likely causes โ Terraform manages an attribute that something else (scaling policies, Karpenter, a human) also manages. That's expected drift, not a Terraform bug.
Diagnose โ terraform plan shows ~ desired_capacity = 6 -> 2.
Fix โ
resource "aws_autoscaling_group" "web" {
# ...
lifecycle {
ignore_changes = [desired_capacity]
}
}Or don't set desired_capacity at all.
Prevent โ For every attribute, pick one owner. Same pattern for ECS desired_count with service autoscaling.
Q31 A resource was deleted manually in the console. The plan wants to create it, and an apply fails because a dependent resource still references the old ID.
Intermediate
Likely causes โ Refresh noticed the object is gone and planned a create, but dependents (route table association, target group attachment) were not updated in the same plan because their attributes didn't appear to change.
Diagnose โ terraform plan shows + create for the deleted resource, and dependents show no change or known after apply IDs.
Fix โ Usually one apply resolves it; if dependents still point at the old ID, force them to be recreated:
terraform apply -replace='aws_route_table_association.private["a"]'Prevent โ Restrict delete permissions in prod; use prevent_destroy on critical resources so even Terraform won't remove them without a code change.
Q32 terraform plan -refresh-only shows drift, and you apply it. The next normal plan still wants to revert the change. Why?
Intermediate
terraform plan -refresh-only shows drift, and you apply it. The next normal plan still wants to revert the change. Why?Likely causes โ apply -refresh-only updates state to match reality but doesn't change code. The code still says the old value, so the next plan reverts.
Diagnose โ Compare the value in code with terraform state show <addr>.
Fix โ Update the code to the new value (or add ignore_changes), then plan should be clean.
Prevent โ Use refresh-only only to record accepted drift, and always follow up with a code change in the same PR.
Q33 You inherited a stack whose state lives in a committed terraform.tfstate in Git. Three engineers have different versions locally.
Advanced
terraform.tfstate in Git. Three engineers have different versions locally.Likely causes โ No remote backend; each engineer applied from their own copy, so the copies forked.
Diagnose โ
git log --oneline -- terraform.tfstate
for f in a.tfstate b.tfstate c.tfstate; do jq -r '"\(.serial) \(.lineage) \(.resources|length)"' $f; doneAlso scan the state for secrets: it's in Git history.
Fix โ Pick the most complete copy (highest serial, same lineage), import anything others created, migrate to S3 with terraform init -migrate-state, delete the file from the repo and add *.tfstate* to .gitignore. Rotate any secrets found in history.
Prevent โ Remote backend with locking from day one; pre-commit hook blocking *.tfstate.
Q34 A plan shows an RDS instance's engine_version going from 15.8 back to 15.4. Nobody changed it.
Intermediate
engine_version going from 15.8 back to 15.4. Nobody changed it.Likely causes โ auto_minor_version_upgrade = true let AWS upgrade the engine during a maintenance window; your code still pins 15.4. Downgrading isn't possible, so the apply would fail.
Diagnose โ aws rds describe-db-instances --db-instance-identifier prod-db --query 'DBInstances[0].EngineVersion'
Fix โ Pin only the major version (engine_version = "15") so minor upgrades don't show as drift, or update code to 15.8. Alternatively ignore_changes = [engine_version].
Prevent โ Decide who owns upgrades: either AWS (major-only pin) or Terraform (auto minor off, explicit bumps via PRs).
Q35 Plans always show ~ tags_all changes on hundreds of resources after adding default_tags to the provider.
Intermediate
~ tags_all changes on hundreds of resources after adding default_tags to the provider.Likely causes โ Normal on the first apply after adding default_tags. Persisting changes come from a resource also setting the same tag key in tags with a different value, or a tag value computed at apply time (for example timestamp()).
Diagnose โ terraform plan | grep -B3 -A6 tags_all to see which keys flip.
Fix โ Apply once. Remove duplicated keys from resource-level tags, and never use timestamp()/uuid() in tags.
Prevent โ Put static org tags only in default_tags; keep resource tags for resource-specific keys.
Q36 An IAM policy shows changes every plan even though the JSON is "the same".
Intermediate
Likely causes โ A heredoc JSON string differs from how AWS normalizes it (statement order, single-string vs list Action, whitespace), or principals were given as account IDs that AWS rewrites to ARNs.
Diagnose โ In the plan diff, look for Action = "s3:GetObject" vs Action = ["s3:GetObject"] or reordered keys.
Fix โ Generate policies with data "aws_iam_policy_document" or jsonencode() and match AWS's canonical form (full ARNs for principals, lists where AWS uses lists).
Prevent โ Ban raw heredoc JSON policies in code review; use policy documents everywhere.
Q37 After an incident, someone changed an EC2 instance type in the console. terraform plan now wants to stop and resize it back during business hours.
Beginner
terraform plan now wants to stop and resize it back during business hours.Likely causes โ Drift on instance_type; the AWS provider changes it in place by stopping and starting the instance.
Diagnose โ terraform plan shows ~ instance_type = "m6i.xlarge" -> "m6i.large".
Fix โ If the bigger size is now correct, update code and apply (no-op). If not, schedule the resize in a maintenance window.
Prevent โ Incident runbooks must include "open a PR to codify any manual change within 24 hours".
Q38 Terraform state shows an S3 bucket, but aws s3api head-bucket returns 404, and the plan still shows no changes.
Advanced
aws s3api head-bucket returns 404, and the plan still shows no changes.Likely causes โ You ran terraform plan -refresh=false or a CI wrapper disables refresh; the provider is pointed at a different region/account from where the bucket was; a provider alias mismatch makes the refresh query the wrong endpoint.
Diagnose โ
terraform state show aws_s3_bucket.logs | grep -E 'region|arn'
aws sts get-caller-identity
grep -rn 'refresh=false' .github/ scripts/Fix โ Plan with refresh enabled using the correct provider configuration. Terraform will then notice the bucket is gone and plan a create.
Prevent โ Only use -refresh=false for speed on very large stacks when you also run scheduled full-refresh drift checks.
Q39 terraform state list shows aws_instance.web but the plan says aws_instance.web will be destroyed with (because aws_instance.web is not in configuration).
Beginner
terraform state list shows aws_instance.web but the plan says aws_instance.web will be destroyed with (because aws_instance.web is not in configuration).Likely causes โ The block was deleted or renamed in code, or it lives in a file that isn't in this directory (moved to a module, or a file renamed to .tf.bak).
Diagnose โ grep -rn 'resource "aws_instance" "web"' .
Fix โ If renamed or moved into a module, add a moved block. If intentionally removed from management without destroying it, use a removed block with lifecycle { destroy = false }. If it should be destroyed, apply.
Prevent โ Treat any unexpected destroy in plan as a blocker in review; add a CI check that fails on destroys unless a label is set.
Q40 Plans for the network stack are clean, but the app stack reading it via terraform_remote_state fails with This object does not have an attribute named "private_subnet_ids".
Intermediate
network stack are clean, but the app stack reading it via terraform_remote_state fails with This object does not have an attribute named "private_subnet_ids".Likely causes โ The output was added to the network code but not applied yet, so it's not in its state; or the app reads the wrong key/workspace.
Diagnose โ
cd network && terraform output
terraform state pull | jq '.outputs | keys'Fix โ Apply the network stack so the output is written to state, or fix the config.key/workspace in the terraform_remote_state block.
Prevent โ Apply producer stacks before consumers in the pipeline. Prefer SSM parameters or data sources with tags for cross-stack contracts.
Q41 Error: Unable to find remote state from a terraform_remote_state data source: No stored state was found for the given workspace in the given backend.
Beginner
Error: Unable to find remote state from a terraform_remote_state data source: No stored state was found for the given workspace in the given backend.Likely causes โ Wrong bucket/key/region in config; the producer uses workspaces (env:/staging/...) but the consumer reads the default workspace; the producer has never been applied.
Diagnose โ aws s3 ls s3://acme-tfstate/ --recursive | grep network
Fix โ Match the exact key, and set workspace = "staging" in the data source if the producer uses workspaces.
Prevent โ Document every stack's state key in one place, or publish shared values to SSM Parameter Store instead.
Q42 An EKS managed node group shows drift on scaling_config[0].desired_size and launch template version after every cluster upgrade.
Advanced
scaling_config[0].desired_size and launch template version after every cluster upgrade.Likely causes โ Cluster Autoscaler/Karpenter changes desired_size; console or eksctl upgrades bump the launch template version and AMI release outside Terraform.
Diagnose โ aws eks describe-nodegroup --cluster-name prod --nodegroup-name general --query 'nodegroup.[scalingConfig,launchTemplate,releaseVersion]'
Fix โ ignore_changes = [scaling_config[0].desired_size], and do upgrades through Terraform (version, release_version or the launch template) instead of the console.
Prevent โ One upgrade path only. Block eks:UpdateNodegroupVersion for humans in prod.
Q43 terraform show displays a resource with status: tainted and the plan says is tainted, so must be replaced. Nobody ran terraform taint.
Beginner
terraform show displays a resource with status: tainted and the plan says is tainted, so must be replaced. Nobody ran terraform taint.Likely causes โ A previous apply created the resource but a later step on it failed (provisioner error, health wait timeout). Terraform marks it tainted because it may be half-configured.
Diagnose โ Check the earlier apply log for the failure on that resource; check the real object's health.
Fix โ If the object is actually fine, terraform untaint aws_instance.web. Otherwise let the plan replace it.
Prevent โ Avoid provisioners; use user_data/cloud-init or configuration management. Set realistic timeouts {} for slow resources.
Q44 Drift detection in CI fails every night on a Lambda function's source_code_hash, though no one deployed.
Intermediate
source_code_hash, though no one deployed.Likely causes โ The ZIP is built in CI and contains timestamps or file permission differences, so its hash changes every build; or the app team deploys Lambda code with another tool.
Diagnose โ Build the archive twice and compare sha256sum; check CloudTrail for UpdateFunctionCode from non-Terraform principals.
Fix โ Make builds reproducible (archive_file from a fixed source dir, stable file modes), or split ownership: Terraform manages the function config and ignore_changes = [source_code_hash, s3_key], CI deploys the code.
Prevent โ Agree which tool owns application code vs infrastructure.
Q45 A plan wants to destroy and recreate a whole module after a teammate restored state from a backup taken before a big refactor.
Advanced
Likely causes โ The backup predates moved/state mv operations, so the restored state uses the old addresses while code uses new ones. If the moved blocks were deleted already, Terraform can't map them.
Diagnose โ terraform state list | head and compare to code addresses (module.network.aws_vpc.this vs aws_vpc.main).
Fix โ Re-add the moved blocks (or run the same terraform state mv commands) and re-plan until the destroys disappear. Then import anything created after the backup.
Prevent โ Keep moved blocks in code for a long time (several releases), and take a state pull backup right before every refactor, not days earlier.
Q46 Plan output shows # (12 unchanged attributes hidden) and you can't see why a resource changes.
Beginner
# (12 unchanged attributes hidden) and you can't see why a resource changes.Likely causes โ Terraform hides unchanged attributes by default. The change may be in a nested block or a sensitive value.
Diagnose โ
terraform plan -out=tfplan
terraform show -json tfplan | jq '.resource_changes[] | select(.address=="aws_lb.main") | .change | {before, after}'Fix โ Use the JSON output to see full before/after values and which attributes are after_unknown.
Prevent โ In CI, publish the JSON plan as an artifact and render it with a diff tool for reviewers.
Q47 Every plan shows an update for aws_security_group and aws_security_group_rule alternately adding and removing the same rule.
Intermediate
aws_security_group and aws_security_group_rule alternately adding and removing the same rule.Likely causes โ Inline ingress {} blocks in aws_security_group and separate rule resources manage the same group. Each one thinks the other's rules are drift.
Diagnose โ grep -rn 'security_group_id\s*=\s*aws_security_group.web' . alongside ingress { in the group.
Fix โ Choose one style. Current best practice: no inline rules, use aws_vpc_security_group_ingress_rule / aws_vpc_security_group_egress_rule. Move rules with removed/import to avoid gaps.
Prevent โ Same rule for route tables (route {} vs aws_route) and IAM (inline policy vs attachment resources).
Q48 A new engineer's terraform plan shows 50 changes, while CI shows none for the same commit.
Intermediate
terraform plan shows 50 changes, while CI shows none for the same commit.Likely causes โ Different workspace, different var files or TF_VAR_* environment variables, a different provider version (old .terraform dir with an unlocked provider), or different credentials pointing at another account.
Diagnose โ
terraform workspace show; env | grep ^TF_; aws sts get-caller-identity
terraform version; cat .terraform.lock.hcl | grep -A1 'provider "registry'Fix โ Align workspace, var files and credentials; rm -rf .terraform && terraform init.
Prevent โ Wrap commands in a Makefile/script that selects workspace and var file, and print account ID and workspace at the top.
Q49 terraform plan shows aws_route53_record will be created, but apply fails with Tried to create resource record set ... but it already exists.
Intermediate
terraform plan shows aws_route53_record will be created, but apply fails with Tried to create resource record set ... but it already exists.Likely causes โ The record was created manually or by another stack (or ExternalDNS in Kubernetes). Terraform doesn't know it exists.
Diagnose โ aws route53 list-resource-record-sets --hosted-zone-id Z123 --query "ResourceRecordSets[?Name=='api.example.com.']"
Fix โ Import it (id = "Z123_api.example.com_A"), or set allow_overwrite = true if Terraform should take ownership.
Prevent โ Split DNS ownership clearly (for example, ExternalDNS owns *.apps, Terraform owns everything else).
Q50 A nightly drift job reports drift in the morning that disappears by itself in the afternoon.
Advanced
Likely causes โ Scheduled automation changes resources (instance scheduler stopping dev EC2 at night, scaling schedules, Lambda rotating something), or eventual consistency in APIs read during refresh.
Diagnose โ Correlate drift timestamps with CloudTrail events and EventBridge schedules:
aws cloudtrail lookup-events --lookup-attributes AttributeKey=ResourceName,AttributeValue=i-0abc123 \
--start-time 2026-11-08T00:00:00Z --max-results 20Fix โ ignore_changes on attributes owned by the automation, or move the drift job to a time when the environment is stable.
Prevent โ Inventory every non-Terraform actor that writes to Terraform-managed resources and document ownership per attribute.
Import, Already Exists and State Surgery
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q51 Apply fails with EntityAlreadyExists: Role with name app-role already exists.
Beginner
EntityAlreadyExists: Role with name app-role already exists.Likely causes โ The role was created manually, by another stack, or by a previous run whose state was lost. IAM names are unique per account.
Diagnose โ aws iam get-role --role-name app-role --query 'Role.[Arn,CreateDate,Tags]' and check tags to find the owner.
Fix โ If this stack should own it, import:
import {
to = aws_iam_role.app
id = "app-role"
}If another stack owns it, rename yours or reference it with a data "aws_iam_role".
Prevent โ Prefix names with stack/env (${var.env}-app-role) or use name_prefix so stacks can't collide.
Q52 terraform apply on a new bucket fails with BucketAlreadyExists. You have never created that bucket.
Beginner
terraform apply on a new bucket fails with BucketAlreadyExists. You have never created that bucket.Likely causes โ S3 bucket names are global across all AWS accounts; someone else owns the name. (BucketAlreadyOwnedByYou means it's in your account.)
Diagnose โ aws s3api head-bucket --bucket logs โ 403 means it exists elsewhere, 404 means free.
Fix โ Choose a unique name, for example acme-prod-logs-${data.aws_caller_identity.current.account_id}, or use bucket_prefix.
Prevent โ A naming module that always appends account ID and region to bucket names.
Q53 Error: Resource already managed by Terraform when running terraform import aws_s3_bucket.logs acme-logs.
Beginner
Error: Resource already managed by Terraform when running terraform import aws_s3_bucket.logs acme-logs.Likely causes โ That address already has an object in state. Terraform refuses to import over it.
Diagnose โ terraform state show aws_s3_bucket.logs โ is it the same bucket or a different one?
Fix โ If it's the same bucket, nothing to do. If the state points to a wrong/deleted object, terraform state rm aws_s3_bucket.logs first, then import.
Prevent โ Use import blocks in PRs so the plan shows what will be imported before anything changes.
Q54 Error: Cannot import non-existent remote object for a resource you can see in the console.
Intermediate
Error: Cannot import non-existent remote object for a resource you can see in the console.Likely causes โ The provider is configured for a different region or account than the console; the ID format is wrong (name vs ARN vs ID); the import targets a resource using a provider alias you didn't specify.
Diagnose โ
aws sts get-caller-identity; echo $AWS_REGION
terraform providersCheck the resource's "Import" section in the provider docs for the expected ID.
Fix โ Use the correct ID and the matching provider. With import blocks, set provider = aws.west when needed.
Prevent โ Keep a team snippet of ID formats for frequently imported types.
Q55 Error: Configuration for import target does not exist when running terraform plan with an import block.
Beginner
Error: Configuration for import target does not exist when running terraform plan with an import block.Likely causes โ The to address has no matching resource block (typo, different name, missing module path or instance key).
Diagnose โ Compare to = aws_instance.app with grep -rn 'resource "aws_instance"' .
Fix โ Write the resource block, or let Terraform generate it:
terraform plan -generate-config-out=generated.tfThen clean up generated.tf and move it into proper files.
Prevent โ Review generated config before merging; it often contains defaults and conflicting arguments.
Q56 terraform plan -generate-config-out=generated.tf fails with Conflicting configuration arguments in the generated file.
Intermediate
terraform plan -generate-config-out=generated.tf fails with Conflicting configuration arguments in the generated file.Likely causes โ The generator writes every attribute it reads, including mutually exclusive ones (for example ipv6_cidr_block and ipv6_ipam_pool_id, or both name and name_prefix).
Diagnose โ Read each error's attribute pair.
Fix โ Delete the conflicting/computed arguments from generated.tf (keep the one you actually set), then run terraform plan again. The file remains; re-running with the same -generate-config-out path errors because the file exists.
Prevent โ Treat generated config as a draft. Refactor it into variables and modules before merge.
Q57 After importing an S3 bucket, the plan still shows + force_destroy = false as an update.
Beginner
+ force_destroy = false as an update.Likely causes โ force_destroy is a Terraform-only argument; it doesn't exist in AWS, so it can't be imported. The first apply just writes it to state.
Diagnose โ The diff touches only Terraform-only arguments; no API call changes the bucket.
Fix โ Apply; it's a state-only update. Same for things like skip_final_snapshot on RDS.
Prevent โ Know which arguments are provider-side only and explain them in the import PR.
Q58 You imported an IAM role and the plan wants to remove two managed policy attachments that prod depends on.
Intermediate
Likely causes โ The code uses managed_policy_arns (exclusive management) or aws_iam_role_policy_attachments_exclusive, so Terraform removes attachments not listed in code.
Diagnose โ aws iam list-attached-role-policies --role-name app-role
Fix โ Add all real attachments to code (or import the existing aws_iam_role_policy_attachment resources) before applying. Note managed_policy_arns is deprecated in AWS provider v5; prefer separate attachment resources.
Prevent โ After every import, require a plan with zero changes before merging.
Q59 terraform import 'aws_instance.web["api"]' i-0abc fails in bash with Error: Index value required or the address looks mangled.
Beginner
terraform import 'aws_instance.web["api"]' i-0abc fails in bash with Error: Index value required or the address looks mangled.Likely causes โ Shell quoting. Without single quotes, bash strips the inner double quotes, so Terraform receives aws_instance.web[api].
Diagnose โ echo aws_instance.web["api"] prints the mangled address.
Fix โ
terraform import 'aws_instance.web["api"]' i-0abc123 # bash/zsh
terraform import 'aws_instance.web[\"api\"]' i-0abc123 # PowerShellOr avoid quoting entirely with an import block in code.
Prevent โ Use import blocks and moved blocks rather than CLI addresses.
Q60 You ran terraform state rm aws_db_instance.main to "fix" a plan error. The next apply fails with DBInstanceAlreadyExists.
Intermediate
terraform state rm aws_db_instance.main to "fix" a plan error. The next apply fails with DBInstanceAlreadyExists.Likely causes โ state rm only forgets the object; the database still exists. The code still declares it, so Terraform tries to create a new one with the same identifier.
Diagnose โ aws rds describe-db-instances --db-instance-identifier prod-db
Fix โ Re-import it:
terraform import aws_db_instance.main prod-db
terraform planPrevent โ Use state rm only when you also remove the block (or use a removed block, which does both in one reviewed change).
Q61 After terraform state mv aws_instance.web module.app.aws_instance.web, the plan wants to destroy the instance and create a new one.
Intermediate
terraform state mv aws_instance.web module.app.aws_instance.web, the plan wants to destroy the instance and create a new one.Likely causes โ The destination address doesn't match the code (module is called module.application, or the resource uses count so it needs [0]); the state now has the object at an address the code doesn't declare.
Diagnose โ terraform state list | grep aws_instance vs the code address shown in the plan's + create.
Fix โ Move again to the exact address in the plan:
terraform state mv 'module.app.aws_instance.web' 'module.application.aws_instance.web[0]'Prevent โ Prefer moved blocks; terraform plan shows has moved to and you see mistakes before state changes.
Q62 Error: Moved object still exists after adding a moved block.
Intermediate
Error: Moved object still exists after adding a moved block.Likely causes โ The from address is still declared in code, so it isn't actually moved (you copied the block instead of renaming it).
Diagnose โ grep -rn 'resource "aws_s3_bucket" "logs"' . finds both old and new blocks.
Fix โ Delete or rename the old resource block so only the to address exists.
Prevent โ Rename and add the moved block in the same commit; review that the old address disappears.
Q63 A moved block from null_resource.bootstrap to terraform_data.bootstrap fails with a resource type mismatch on an old Terraform version.
Advanced
moved block from null_resource.bootstrap to terraform_data.bootstrap fails with a resource type mismatch on an old Terraform version.Likely causes โ Cross-type moves need Terraform 1.8+ and provider support. null_resource โ terraform_data is supported from 1.9; older versions reject any move between different types.
Diagnose โ terraform version locally and in CI.
Fix โ Upgrade to 1.9+ everywhere and keep the moved block. On older versions, use removed { lifecycle { destroy = false } } plus a new terraform_data (which will re-run its provisioner) or accept the replacement.
Prevent โ Check the minimum Terraform version for refactor features and bump required_version in the same PR.
Q64 A moved block inside your module referencing a resource in a registry module fails with Cross-package move statement.
Advanced
moved block inside your module referencing a resource in a registry module fails with Cross-package move statement.Likely causes โ moved can only refer to objects within your module package; you can't move into or out of a separately installed module's internals from inside it.
Diagnose โ The error names the external package source.
Fix โ Put the moved block in the calling module, using full module paths:
moved {
from = aws_vpc.main
to = module.vpc.aws_vpc.this[0]
}Prevent โ Do refactors that cross module boundaries in the root module, and look up the registry module's real resource names and instance keys first.
Q65 You need to move a resource from stack A's state to stack B's state. terraform state mv with -state-out isn't working with the S3 backend.
Advanced
terraform state mv with -state-out isn't working with the S3 backend.Likely causes โ -state / -state-out work on local files only; with remote backends you must use pull/push or a declarative approach.
Diagnose โ The command complains about local state options or updates only a local file.
Fix โ The safest approach in 1.7+: in stack A add removed { from = aws_sqs_queue.jobs lifecycle { destroy = false } } and apply; in stack B add the resource plus an import block and apply. No hand-edited state, and both steps are reviewed plans.
Prevent โ Plan stack boundaries early; when moving, always use the removed + import pattern.
Q66 removed block plan fails with Error: Removed resource block still exists.
Beginner
removed block plan fails with Error: Removed resource block still exists.Likely causes โ The resource block is still in code; a removed block requires the block to be deleted.
Diagnose โ grep -rn 'aws_sqs_queue" "jobs"' .
Fix โ Delete the resource block, keep only:
removed {
from = aws_sqs_queue.jobs
lifecycle {
destroy = false
}
}Prevent โ Remember removed needs Terraform 1.7+; check CI's version too.
Q67 Importing an aws_iam_role_policy_attachment fails with unexpected format for ID.
Intermediate
aws_iam_role_policy_attachment fails with unexpected format for ID.Likely causes โ Many "association" resources use composite IDs, not a single AWS ID. For this one it's role-name/policy-arn.
Diagnose โ Check the "Import" section at the bottom of the resource's provider documentation page.
Fix โ
import {
to = aws_iam_role_policy_attachment.ssm
id = "app-role/arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}Other examples: aws_route_table_association uses subnet-id/rtb-id; aws_security_group_rule has a long composite ID.
Prevent โ Keep a cheatsheet of composite IDs for commonly imported resources.
Q68 You need to import 60 SQS queues created by hand. Writing 60 import blocks is error-prone.
Intermediate
Likely causes โ Not an error, but a common place to make address mistakes.
Diagnose โ List the real queues: aws sqs list-queues --queue-name-prefix orders- --query QueueUrls
Fix โ Use for_each on import blocks (Terraform 1.7+), driven by the same map as the resource:
locals {
queues = toset(["orders-created", "orders-paid", "orders-shipped"])
}
resource "aws_sqs_queue" "q" {
for_each = local.queues
name = each.key
}
import {
for_each = local.queues
to = aws_sqs_queue.q[each.key]
id = "https://sqs.ap-south-1.amazonaws.com/111122223333/${each.key}"
}Prevent โ Delete import blocks after a clean apply, or leave them; they're no-ops once imported.
Q69 Import finished, but now terraform plan shows a forced replacement on the imported resource.
Advanced
terraform plan shows a forced replacement on the imported resource.Likely causes โ An argument in code that forces replacement differs from reality: availability_zone, subnet_id, name, engine, kms_key_id, or user_data with user_data_replace_on_change = true.
Diagnose โ terraform plan | grep -B2 '# forces replacement', then terraform state show <addr> to see real values.
Fix โ Change code to match the imported values exactly. Never apply an import PR that shows a replace.
Prevent โ Add a CI rule: PRs containing import blocks must show zero delete and zero update actions.
Q70 terraform state rm module.legacy was run in the wrong workspace (prod instead of staging).
Advanced
terraform state rm module.legacy was run in the wrong workspace (prod instead of staging).Likely causes โ Wrong workspace selected, or TF_WORKSPACE set in the shell.
Diagnose โ terraform workspace show; terraform plan in prod now shows module.legacy resources to be created.
Fix โ Do not apply. Restore the previous state version from S3 versioning (env:/prod/... key), or re-import every removed resource. With a backup:
terraform state push -force prod-before-rm.jsonPrevent โ Take terraform state pull > backup-$(date +%s).json before any state command, and show the workspace in your shell prompt.
Q71 terraform state mv returns Error: Invalid target address when moving between count and for_each instances.
Beginner
terraform state mv returns Error: Invalid target address when moving between count and for_each instances.Likely causes โ Missing quotes around string keys, or using [0] where the code now uses for_each keys.
Diagnose โ terraform state list | grep aws_subnet
Fix โ
terraform state mv 'aws_subnet.private[0]' 'aws_subnet.private["ap-south-1a"]'Better: a moved block per instance, which can be reviewed in a plan.
Prevent โ When converting count โ for_each, generate moved blocks from a list in one PR.
Q72 You imported a resource into the default workspace instead of staging. Now default has a stray object.
Intermediate
staging. Now default has a stray object.Likely causes โ Forgot terraform workspace select staging before importing.
Diagnose โ terraform workspace list, then terraform state list in each.
Fix โ In default: terraform state rm <addr> (only forgets, doesn't delete). In staging: import it again.
Prevent โ Use separate directories/backends per environment instead of workspaces for long-lived envs, or a wrapper that requires an explicit ENV= argument.
Q73 Error: resource aws_xyz doesn't support import.
Intermediate
Error: resource aws_xyz doesn't support import.Likely causes โ Some resources have no read API or the provider never implemented import (common for "action" resources and some older ones).
Diagnose โ Check the provider docs and changelog; a newer provider version may add import support.
Fix โ Upgrade the provider if support was added. Otherwise recreate under Terraform (planned window), or manage it outside Terraform and reference it via a data source.
Prevent โ Prefer resources that support import when choosing between equivalent resource types.
Q74 After importing an EC2 instance, every plan shows user_data changes or a replacement.
Intermediate
user_data changes or a replacement.Likely causes โ The provider stores a hash/base64 of user data; your code's template renders differently (trailing newline, different variables). With user_data_replace_on_change = true this forces replacement.
Diagnose โ
aws ec2 describe-instance-attribute --instance-id i-0abc123 --attribute userData \
--query 'UserData.Value' --output text | base64 -d > real.sh
diff real.sh <(terraform console <<< 'local.user_data')Fix โ Make the template output byte-identical, or ignore_changes = [user_data] for instances where user data only matters at first boot.
Prevent โ For long-lived instances, keep user data minimal and push config via SSM/Ansible.
Q75 A teammate edited the state JSON to change a resource ID, pushed it, and now refresh fails for that resource with a provider error.
Advanced
Likely causes โ Only id was changed; other attributes (ARN, nested IDs) still reference the old object, or schema_version / private provider data no longer match, so the provider can't read it.
Diagnose โ terraform state show <addr>; compare id with arn and other identifiers.
Fix โ Remove and re-import cleanly:
terraform state rm aws_lb.main
terraform import aws_lb.main arn:aws:elasticloadbalancing:ap-south-1:111122223333:loadbalancer/app/prod/abc123Prevent โ No manual state edits; reviewers should reject PRs or runbooks that suggest them.
Dependencies, Cycles and Providers
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q76 Error: Cycle: aws_security_group.app, aws_security_group.db.
Beginner
Error: Cycle: aws_security_group.app, aws_security_group.db.Likely causes โ Each group's inline ingress references the other group's ID, so neither can be created first.
Diagnose โ
terraform graph -type=plan | dot -Tsvg > graph.svgLook for the two nodes pointing at each other.
Fix โ Create both groups without inline rules, then add rules as separate resources:
resource "aws_vpc_security_group_ingress_rule" "db_from_app" {
security_group_id = aws_security_group.db.id
referenced_security_group_id = aws_security_group.app.id
from_port = 5432
to_port = 5432
ip_protocol = "tcp"
}Prevent โ Standardize on standalone rule resources for all security groups.
Q77 Error: Cycle: local.name_prefix, local.tags.
Beginner
Error: Cycle: local.name_prefix, local.tags.Likely causes โ Two locals reference each other (directly or through a third).
Diagnose โ Read both definitions; the cycle list tells you every node involved.
Fix โ Break the loop by extracting the shared part into a third local that depends on neither.
Prevent โ Keep locals layered: inputs โ derived names โ tags/maps, never back-references.
Q78 A cycle appears only after adding depends_on = [module.app] to a module. The error lists dozens of nodes.
Intermediate
depends_on = [module.app] to a module. The error lists dozens of nodes.Likely causes โ Module-level depends_on makes every resource and data source in the module depend on everything in the other module. If any value flows back (an output of this module is used by module.app), you get a cycle.
Diagnose โ The cycle list contains nodes from both modules; check which outputs cross between them.
Fix โ Remove the module depends_on and pass the actual values (IDs, ARNs) as inputs; Terraform then infers precise dependencies.
Prevent โ Treat module depends_on as a last resort; it also delays data sources and causes "known after apply" noise.
Q79 Adding create_before_destroy = true to a launch template causes Error: Cycle: ... (destroy deposed ...).
Advanced
create_before_destroy = true to a launch template causes Error: Cycle: ... (destroy deposed ...).Likely causes โ create_before_destroy propagates to dependencies: a resource it depends on doesn't have it, so Terraform must destroy the dependency before creating the new object and destroy the dependent first. The graph conflicts.
Diagnose โ terraform graph -type=apply and look for (destroy deposed) nodes in the cycle.
Fix โ Add create_before_destroy = true to the dependencies in the chain too (and use name_prefix so old and new can coexist).
Prevent โ Design replaceable chains (LT โ ASG โ target group attachments) with consistent lifecycle settings from the start.
Q80 terraform init fails: Could not retrieve the list of available versions for provider hashicorp/aws: no available releases match the given constraints ~> 4.0, >= 5.30.0.
Intermediate
terraform init fails: Could not retrieve the list of available versions for provider hashicorp/aws: no available releases match the given constraints ~> 4.0, >= 5.30.0.Likely causes โ Root requires AWS provider 5.x, but a child module pins ~> 4.0. Terraform needs one version satisfying every module.
Diagnose โ
terraform providersThe tree shows which module declares which constraint.
Fix โ Upgrade the module to a release that supports AWS provider 5.x (version = "~> 5.0" on the registry module), or fork and relax its constraint.
Prevent โ Modules should use loose lower bounds (>= 5.0); only root modules pin tightly. Bump modules and providers together with Renovate.
Q81 Error: Inconsistent dependency lock file with locked version selection 5.31.0 doesn't match the updated version constraints ">= 5.40.0".
Beginner
Error: Inconsistent dependency lock file with locked version selection 5.31.0 doesn't match the updated version constraints ">= 5.40.0".Likely causes โ You raised the provider constraint in required_providers, but .terraform.lock.hcl still records the old version.
Diagnose โ grep -A3 'hashicorp/aws' .terraform.lock.hcl
Fix โ
terraform init -upgrade
git add .terraform.lock.hclPrevent โ Always commit the lock file in the same PR as constraint changes.
Q82 Error: Inconsistent dependency lock file with provider registry.terraform.io/hashicorp/random: required by this configuration but no version is selected.
Beginner
Error: Inconsistent dependency lock file with provider registry.terraform.io/hashicorp/random: required by this configuration but no version is selected.Likely causes โ A new provider was introduced (directly or by a new module) and terraform init wasn't re-run, or the lock file in CI is older than the code.
Diagnose โ terraform providers shows random but the lock file lacks it.
Fix โ terraform init locally (adds the entry), commit .terraform.lock.hcl.
Prevent โ CI should run terraform init -lockfile=readonly so missing entries fail early in PR checks, not at apply time.
Q83 CI on Linux fails: the current package for registry.terraform.io/hashicorp/aws 5.72.1 doesn't match any of the checksums previously recorded in the dependency lock file. Works on Mac laptops.
Intermediate
the current package for registry.terraform.io/hashicorp/aws 5.72.1 doesn't match any of the checksums previously recorded in the dependency lock file. Works on Mac laptops.Likely causes โ The lock file was generated on darwin_arm64 and only contains h1: hashes for that platform (when installed from a mirror/cache); CI downloads linux_amd64.
Diagnose โ Count hashes: grep -A20 'hashicorp/aws' .terraform.lock.hcl.
Fix โ
terraform providers lock -platform=linux_amd64 -platform=darwin_arm64 -platform=darwin_amd64 -platform=windows_amd64Commit the updated lock file.
Prevent โ Make the multi-platform providers lock command part of your upgrade script or Renovate post-upgrade task.
Q84 On an Apple Silicon Mac: Provider registry.terraform.io/hashicorp/template v2.2.0 does not have a package available for your current platform, darwin_arm64.
Beginner
Provider registry.terraform.io/hashicorp/template v2.2.0 does not have a package available for your current platform, darwin_arm64.Likely causes โ The template provider is archived and was never built for darwin_arm64. An old module still uses data "template_file".
Diagnose โ terraform providers shows which module requires hashicorp/template.
Fix โ Replace template_file with the built-in templatefile() function, and upgrade/fork the module that uses it.
Prevent โ Remove archived providers (template, old community ones) during regular dependency hygiene.
Q85 provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/kubectl.
Beginner
provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/kubectl.Likely causes โ A non-HashiCorp provider is used without a source in required_providers, so Terraform assumes hashicorp/<name>. Often happens in a child module that forgot to declare it.
Diagnose โ The error says Did you intend to use ...? and names the module requiring it.
Fix โ In every module that uses it:
terraform {
required_providers {
kubectl = {
source = "alekc/kubectl"
version = "~> 2.0"
}
}
}Prevent โ Every module declares all its providers with source; tflint catches missing ones.
Q86 Error: Provider configuration not present โ To work with module.dr.aws_s3_bucket.replica (orphan) its original provider configuration ... is required, but it has been removed.
Advanced
Error: Provider configuration not present โ To work with module.dr.aws_s3_bucket.replica (orphan) its original provider configuration ... is required, but it has been removed.Likely causes โ You removed a module call (or a provider alias like aws.dr) while its resources are still in state. Terraform needs that provider configuration to destroy them.
Diagnose โ terraform state list | grep module.dr
Fix โ Two-step removal: put the provider block (and module call, if removed) back, apply with the module removed so resources are destroyed, then delete the provider alias in a follow-up PR. If resources should survive, use removed blocks first.
Prevent โ Never delete a provider configuration in the same change that removes the resources using it.
Q87 Error: Module is incompatible with count, for_each, and depends_on โ The module at module.legacy is a legacy module which contains its own local provider configurations.
Intermediate
Error: Module is incompatible with count, for_each, and depends_on โ The module at module.legacy is a legacy module which contains its own local provider configurations.Likely causes โ The child module has provider "aws" { ... } blocks inside it. Modules with their own provider blocks can't be used with count/for_each/depends_on.
Diagnose โ grep -rn '^provider ' modules/legacy/
Fix โ Move provider configuration to the root, declare configuration_aliases in the module if it needs multiple, and pass providers in:
module "legacy" {
source = "./modules/legacy"
for_each = var.regions
providers = { aws = aws.mumbai }
}Note: you still can't choose a provider per for_each instance dynamically; one module call per provider is needed.
Prevent โ Module guideline: no provider blocks in reusable modules, only required_providers.
Q88 Creating an EKS cluster and Kubernetes resources in one stack: Error: Get "http://localhost/api/v1/namespaces/app": dial tcp [::1]:80: connect: connection refused.
Advanced
Error: Get "http://localhost/api/v1/namespaces/app": dial tcp [::1]:80: connect: connection refused.Likely causes โ The kubernetes provider was configured from values unknown at plan time (cluster not created yet, or being replaced), so it fell back to defaults (localhost). Also happens when the cluster is replaced in the same plan.
Diagnose โ Is the cluster being created/replaced in this plan? Does the provider use module.eks.cluster_endpoint?
Fix โ Apply in two stages (cluster stack first, workloads stack second), and configure the provider with exec auth:
provider "kubernetes" {
host = data.aws_eks_cluster.this.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.this.certificate_authority[0].data)
exec {
api_version = "client.authentication.k8s.io/v1beta1"
command = "aws"
args = ["eks", "get-token", "--cluster-name", "prod"]
}
}Prevent โ Keep cluster infrastructure and in-cluster resources in separate states.
Q89 Long applies against EKS fail midway with Error: Unauthorized from the Kubernetes/Helm provider.
Intermediate
Error: Unauthorized from the Kubernetes/Helm provider.Likely causes โ The provider uses data "aws_eks_cluster_auth" token, which expires after about 15 minutes. Long applies outlive it.
Diagnose โ The failure appears roughly 15 minutes into the apply; earlier Kubernetes resources succeeded.
Fix โ Switch the kubernetes and helm providers to the exec block (aws eks get-token) so a fresh token is fetched as needed.
Prevent โ Use exec auth as the default pattern in all modules and examples.
Q90 Error: Kubernetes cluster unreachable: invalid configuration: no configuration has been provided from the Helm provider.
Beginner
Error: Kubernetes cluster unreachable: invalid configuration: no configuration has been provided from the Helm provider.Likely causes โ The helm provider block has no kubernetes {} config (in Helm provider v3 it's a kubernetes = { ... } attribute instead of a block), no KUBECONFIG, or the config path points to a missing file in CI.
Diagnose โ grep -A10 'provider "helm"' *.tf; check provider major version in the lock file.
Fix โ Configure it explicitly with host/CA/exec using the syntax matching your Helm provider major version.
Prevent โ Pin provider major versions and read upgrade guides for major bumps (Helm provider 3.x changed block syntax).
Q91 Provider "registry.terraform.io/hashicorp/aws" requires explicit configuration / The argument "region" is required, but was not set.
Beginner
Provider "registry.terraform.io/hashicorp/aws" requires explicit configuration / The argument "region" is required, but was not set.Likely causes โ No region in the provider block and no AWS_REGION/AWS_DEFAULT_REGION or profile region in this environment (common in fresh CI runners).
Diagnose โ env | grep -i region; aws configure get region
Fix โ Set region = var.region in the provider block, or export AWS_REGION in the pipeline.
Prevent โ Always set region explicitly in code; don't depend on per-machine defaults.
Q92 A data source reads the wrong AMI/VPC: data "aws_vpc" returns a VPC from us-east-1 though resources go to ap-south-1.
Intermediate
data "aws_vpc" returns a VPC from us-east-1 though resources go to ap-south-1.Likely causes โ Multiple provider configurations; the data source uses the default provider while resources use provider = aws.mumbai.
Diagnose โ terraform providers and grep -n 'provider\s*=' *.tf
Fix โ Set provider = aws.mumbai on the data source too.
Prevent โ In multi-region stacks, avoid an unaliased default provider, so every resource and data source must pick one explicitly.
Q93 A module uses provider = aws.us_east_1 for an ACM certificate (CloudFront) and fails: the module doesn't receive that provider.
Intermediate
provider = aws.us_east_1 for an ACM certificate (CloudFront) and fails: the module doesn't receive that provider.Likely causes โ The module didn't declare the alias in configuration_aliases, or the caller didn't pass it in providers.
Diagnose โ Check the module's required_providers and the call's providers = {} map.
Fix โ
# in the module
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
configuration_aliases = [aws.us_east_1]
}
}
}
# in the root
module "cdn" {
source = "./modules/cdn"
providers = { aws = aws, aws.us_east_1 = aws.us_east_1 }
}Prevent โ Document required provider aliases in each module's README.
Q94 terraform init fails behind a firewall: could not connect to registry.terraform.io: failed to request discovery document.
Intermediate
terraform init fails behind a firewall: could not connect to registry.terraform.io: failed to request discovery document.Likely causes โ No outbound internet, a proxy without its CA trusted, or a blocked releases.hashicorp.com (providers download from there after registry lookup).
Diagnose โ
curl -sv https://registry.terraform.io/.well-known/terraform.json
curl -sI https://releases.hashicorp.com/Fix โ Configure HTTPS_PROXY and trust the proxy CA, or use a provider mirror:
# ~/.terraformrc
provider_installation {
network_mirror {
url = "https://tf-mirror.internal.example.com/"
}
}Prevent โ For air-gapped runners, prepare mirrors with terraform providers mirror ./mirror and bake them into the runner image.
Q95 Parallel CI jobs sharing TF_PLUGIN_CACHE_DIR fail intermittently with text file busy or corrupt provider zips.
Advanced
TF_PLUGIN_CACHE_DIR fail intermittently with text file busy or corrupt provider zips.Likely causes โ The plugin cache is not safe for concurrent writes; two terraform init runs write the same provider at once.
Diagnose โ Failures only when jobs run in parallel on the same host.
Fix โ Give each job its own cache dir, or pre-populate the cache in the image (read-only use is safe). Use CI cache keyed on .terraform.lock.hcl hash.
Prevent โ Treat the plugin cache as per-job or pre-baked, never shared-writable across concurrent jobs.
Q96 Error: Required plugins are not installed โ there is no package for registry.terraform.io/hashicorp/aws 5.72.1 cached in .terraform/providers.
Beginner
Error: Required plugins are not installed โ there is no package for registry.terraform.io/hashicorp/aws 5.72.1 cached in .terraform/providers.Likely causes โ The lock file was updated (pulled new commits) but .terraform/providers still has the old version; or the CI job restored a stale .terraform cache.
Diagnose โ ls .terraform/providers/registry.terraform.io/hashicorp/aws/
Fix โ terraform init (not -upgrade; the lock file already chooses the version).
Prevent โ Cache providers by lock-file hash, and don't cache the whole .terraform directory.
Q97 depends_on on a data source makes every plan show (known after apply) for things that never change.
Advanced
depends_on on a data source makes every plan show (known after apply) for things that never change.Likely causes โ A data source with depends_on (or inside a module with module-level depends_on) is deferred until apply whenever the dependency has any pending change. All values derived from it become unknown, causing noisy diffs and sometimes replacements.
Diagnose โ In the plan, look for <= data "..." ... (depends on a resource or a module with changes pending) lines.
Fix โ Remove depends_on and make the data source reference a specific attribute (for example vpc_id = aws_vpc.main.id) so the dependency is precise.
Prevent โ Ban module-level depends_on in review unless documented.
Q98 Error: Invalid provider configuration reference or Error: Duplicate provider configuration after copying files between stacks.
Beginner
Error: Invalid provider configuration reference or Error: Duplicate provider configuration after copying files between stacks.Likely causes โ Two provider "aws" blocks without aliases, or a resource refers to aws.west that doesn't exist in this stack.
Diagnose โ grep -rn 'provider "aws"' *.tf and grep -rn 'provider = aws\.' *.tf
Fix โ Keep one unaliased default; give extras alias = "west", and fix references.
Prevent โ Keep provider blocks in a single providers.tf per root module.
Q99 A provider minor upgrade (5.60 โ 5.70) causes plans to show unexpected attribute changes on many resources.
Intermediate
Likely causes โ New defaults or new computed attributes; a bug fix changed how a value is normalized; deprecated arguments changed behaviour.
Diagnose โ Read the provider CHANGELOG.md entries for the jump; run the plan with old and new versions to isolate.
Fix โ If harmless (state-only), apply. If behaviour changed, set the argument explicitly or pin the previous minor and open an upstream issue.
Prevent โ Upgrade providers in small steps through Renovate PRs, with plan output for every stack attached.
Q100 After upgrading the AWS provider to 6.x, plans fail with Unsupported argument on arguments that worked before.
Advanced
Unsupported argument on arguments that worked before.Likely causes โ Major provider versions remove deprecated arguments and resources. Code used things marked deprecated in 5.x.
Diagnose โ Read the provider's version 6 upgrade guide; before upgrading, terraform validate on 5.x shows Warning: Argument is deprecated for everything that will break.
Fix โ Pin back to ~> 5.0 to unblock, fix the deprecations, then upgrade one stack at a time.
Prevent โ Treat deprecation warnings as tech debt with an owner; pin majors in root modules (~> 5.0) so upgrades are deliberate.
Plan Errors and Unexpected Replacements
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q101 Error: Invalid for_each argument โ The "for_each" map includes keys derived from resource attributes that cannot be determined until apply.
Intermediate
Error: Invalid for_each argument โ The "for_each" map includes keys derived from resource attributes that cannot be determined until apply.Likely causes โ Keys come from something created in the same run, such as for_each = toset(aws_subnet.private[*].id) or { for i in aws_instance.web : i.id => i }.
Diagnose โ Find where the key is computed; any resource attribute marked (known after apply) can't be a key.
Fix โ Use static keys you control and put apply-time values in the map values:
resource "aws_route_table_association" "private" {
for_each = aws_subnet.private # keys are var-driven AZ names
subnet_id = each.value.id
route_table_id = aws_route_table.private.id
}-target to create dependencies first works but is a workaround, not a design.
Prevent โ Rule of thumb: for_each keys come from variables/locals, never from resource IDs.
Q102 The given "for_each" argument value is unsuitable: the "for_each" argument must be a map, or set of strings, and you have provided a value of type list of string.
Beginner
The given "for_each" argument value is unsuitable: the "for_each" argument must be a map, or set of strings, and you have provided a value of type list of string.Likely causes โ You passed a list (var.users) directly.
Diagnose โ terraform console โ type(var.users).
Fix โ for_each = toset(var.users), or build a map keyed by a stable field: { for u in var.users : u.name => u }.
Prevent โ Declare variables as set(string) or map(object(...)) when they drive for_each.
Q103 Error: Invalid for_each argument โ Sensitive values, or values derived from sensitive values, cannot be used as for_each arguments.
Intermediate
Error: Invalid for_each argument โ Sensitive values, or values derived from sensitive values, cannot be used as for_each arguments.Likely causes โ The map came from a sensitive = true variable or from a secret data source; even the keys become sensitive.
Diagnose โ Trace the value back to a sensitive variable/output.
Fix โ Iterate over non-secret keys only:
for_each = nonsensitive(toset(keys(var.db_users)))
# use var.db_users[each.key] inside for the secret valuesOnly use nonsensitive() when the keys are truly not secret.
Prevent โ Split structures: a non-sensitive map of names/config and a separate sensitive map of secrets.
Q104 Error: Invalid count argument โ The "count" value depends on resource attributes that cannot be determined until apply.
Intermediate
Error: Invalid count argument โ The "count" value depends on resource attributes that cannot be determined until apply.Likely causes โ count = length(aws_instance.web[*].id) or count = var.enabled && aws_vpc.main.id != "" ? 1 : 0 uses an unknown value.
Diagnose โ Find the unknown input in the count expression.
Fix โ Drive count from a known value: count = var.create_alarm ? 1 : 0 or length(var.instance_names).
Prevent โ Never compare resource attributes to decide whether to create something; use explicit boolean flags.
Q105 Error: Unsupported attribute โ This object has no argument, nested block, or exported attribute named "public_ip_address".
Beginner
Error: Unsupported attribute โ This object has no argument, nested block, or exported attribute named "public_ip_address".Likely causes โ Wrong attribute name (Azure-ism in AWS code; the AWS name is public_ip), or an attribute removed in a provider upgrade.
Diagnose โ Check the resource's "Attribute Reference" docs, or terraform state show aws_instance.web.
Fix โ Use the correct attribute: aws_instance.web.public_ip.
Prevent โ Rely on editor tooling (terraform-ls) and terraform validate in pre-commit.
Q106 Error: Unsupported attribute on module.vpc.private_subnets โ This object does not have an attribute named "private_subnets".
Beginner
Error: Unsupported attribute on module.vpc.private_subnets โ This object does not have an attribute named "private_subnets".Likely causes โ The module doesn't declare that output, or it's named differently (private_subnet_ids), or you upgraded the module and the output was renamed.
Diagnose โ grep -rn '^output' .terraform/modules/vpc/outputs.tf (for registry modules) or the module's README.
Fix โ Use the correct output name, or add the output to your module.
Prevent โ Treat module outputs as an API; changing them is a breaking change with a major version bump.
Q107 Error: Missing resource instance key โ Because aws_instance.web has "count" set, its attributes must be accessed on specific instances.
Beginner
Error: Missing resource instance key โ Because aws_instance.web has "count" set, its attributes must be accessed on specific instances.Likely causes โ You added count (or for_each) to a resource but references still use aws_instance.web.id.
Diagnose โ grep -rn 'aws_instance.web\.' .
Fix โ Use an index or splat: aws_instance.web[0].id, aws_instance.web[*].id, or values(aws_instance.web)[*].id for for_each. Add a moved block from aws_instance.web to aws_instance.web[0] so it isn't replaced.
Prevent โ When introducing count/for_each, update references and add moved blocks in the same PR.
Q108 Error: Reference to undeclared resource โ A managed resource "aws_vpc" "main" has not been declared in the root module.
Beginner
Error: Reference to undeclared resource โ A managed resource "aws_vpc" "main" has not been declared in the root module.Likely causes โ The VPC is defined inside a module (so you need module.network.vpc_id), the resource name differs (aws_vpc.this), or the file defining it was deleted/renamed with a non-.tf extension.
Diagnose โ grep -rn 'resource "aws_vpc"' .
Fix โ Reference the right address or module output; if the VPC exists outside this stack, use data "aws_vpc".
Prevent โ Run terraform validate in pre-commit and PR checks.
Q109 Plan shows # aws_instance.web must be replaced with ~ ami = "ami-0aaa" -> "ami-0bbb" # forces replacement, but nobody changed the AMI.
Intermediate
# aws_instance.web must be replaced with ~ ami = "ami-0aaa" -> "ami-0bbb" # forces replacement, but nobody changed the AMI.Likely causes โ data "aws_ami" with most_recent = true picked up a newly published image.
Diagnose โ terraform state show data.aws_ami.al2023 | grep -E 'id|creation_date'
Fix โ Pin the AMI ID in a variable updated by a deliberate PR, or lifecycle { ignore_changes = [ami] } and roll instances via ASG instance refresh instead.
Prevent โ For fleets, use launch templates + ASGs so a new AMI rolls gradually instead of replacing instances during an apply.
Q110 Removing one item from the middle of a list makes the plan destroy and recreate every subsequent resource.
Intermediate
Likely causes โ count with a list: instances are addressed by index, so removing [1] shifts [2] โ [1] and so on. Terraform sees changed arguments at each index.
Diagnose โ The plan shows aws_iam_user.u[1], [2], [3] being changed/replaced.
Fix โ Switch to for_each = toset(var.users) with moved blocks from each index to its key, then remove the item.
Prevent โ Use count only for "0 or 1" or truly identical copies; use for_each for collections.
Q111 Renaming a security group's name forces replacement, and the apply fails with InvalidGroup.Duplicate or hangs on DependencyViolation.
Advanced
name forces replacement, and the apply fails with InvalidGroup.Duplicate or hangs on DependencyViolation.Likely causes โ name forces a new group. Default order is destroy-then-create, but the old group is attached to ENIs and can't be deleted. With create_before_destroy, the same name conflicts.
Diagnose โ
aws ec2 describe-network-interfaces --filters Name=group-id,Values=sg-0abc123 \
--query 'NetworkInterfaces[].[NetworkInterfaceId,Description]'Fix โ Use name_prefix plus create_before_destroy = true so a new group is created, attached, then the old one deleted. Or revert the rename (description/tags may be enough).
Prevent โ Security groups should use name_prefix and lifecycle CBD from day one.
Q112 Plan says aws_db_instance.main must be replaced after changing storage_encrypted = true.
Advanced
aws_db_instance.main must be replaced after changing storage_encrypted = true.Likely causes โ Encryption at rest can't be enabled in place on RDS; the provider marks it forces replacement. Applying would create a new empty DB and delete the old one.
Diagnose โ terraform plan | grep -B2 'forces replacement'
Fix โ Revert the change. Migrate deliberately: snapshot โ copy snapshot with KMS encryption โ restore to a new instance (in code, snapshot_identifier) โ switch the app โ remove the old one.
Prevent โ lifecycle { prevent_destroy = true } and deletion_protection = true on stateful resources, and a CI gate that blocks any delete on RDS/S3/EFS/DynamoDB.
Q113 Error: Instance cannot be destroyed โ Resource aws_s3_bucket.state has lifecycle.prevent_destroy set, but the plan calls for this resource to be destroyed.
Beginner
Error: Instance cannot be destroyed โ Resource aws_s3_bucket.state has lifecycle.prevent_destroy set, but the plan calls for this resource to be destroyed.Likely causes โ The guard is working: your change causes a destroy or replace (rename, for_each key change, a forcing argument).
Diagnose โ Find what triggers the destroy in the plan.
Fix โ Fix the code (add moved, revert the forcing change). Only remove prevent_destroy if destroying is truly intended and approved.
Prevent โ Keep prevent_destroy on state buckets, databases, KMS keys and DNS zones.
Q114 Every plan shows ~ tags = { "LastUpdated" = "2026-11-08T10:00:00Z" -> (known after apply) }.
Beginner
~ tags = { "LastUpdated" = "2026-11-08T10:00:00Z" -> (known after apply) }.Likely causes โ timestamp() (or uuid()) in tags or names; it's evaluated each run, so there's always a diff.
Diagnose โ grep -rn -E 'timestamp\(\)|uuid\(\)' .
Fix โ Remove it. If you need a creation time, use time_static from the time provider, which is stored once in state.
Prevent โ Lint against non-deterministic functions in resource arguments.
Q115 Error: Inconsistent conditional result types โ The true and false result expressions must have consistent types.
Intermediate
Error: Inconsistent conditional result types โ The true and false result expressions must have consistent types.Likely causes โ var.enabled ? { a = 1, b = "x" } : {} or two objects with different attributes; Terraform can't unify the types.
Diagnose โ Read which attribute is absent in one branch.
Fix โ Make both branches the same shape, or use merge()/tomap():
locals {
extra = var.enabled ? { backup = "daily" } : tomap({})
}Prevent โ Use typed variables (map(string)) so values are converted consistently.
Q116 Error: Invalid index โ The given key does not identify an element in this collection value: the collection has no elements.
Intermediate
Error: Invalid index โ The given key does not identify an element in this collection value: the collection has no elements.Likely causes โ Accessing [0] on an empty list: a data source with no matches (data.aws_subnets.private.ids[0]), or a resource with count = 0.
Diagnose โ terraform console โ data.aws_subnets.private.ids and check filters/tags.
Fix โ Fix the filter, or guard: one(aws_eip.nat[*].public_ip) (null when empty) or try(x[0], null). For required inputs, add a postcondition with a clear message.
Prevent โ Add lifecycle { postcondition { condition = length(self.ids) > 0 ... } } on critical data sources.
Q117 Error: Invalid function argument โ no file exists at "./scripts/bootstrap.sh"; this function works only with files that are distributed as part of the configuration source code.
Beginner
Error: Invalid function argument โ no file exists at "./scripts/bootstrap.sh"; this function works only with files that are distributed as part of the configuration source code.Likely causes โ Paths are relative to the working directory, not the module. Inside a module, ./scripts/... points to the root's folder.
Diagnose โ Check where the file lives and where terraform runs.
Fix โ file("${path.module}/scripts/bootstrap.sh") or templatefile("${path.module}/templates/user_data.sh.tftpl", {...}).
Prevent โ Always prefix file paths with path.module in modules.
Q118 A plan wants to replace an EKS cluster after changing subnet_ids.
Advanced
subnet_ids.Likely causes โ Depending on provider and API support, some vpc_config changes require a new cluster; a module upgrade may also change subnet ordering or the cluster name.
Diagnose โ terraform plan | grep -B3 'forces replacement' inside module.eks.
Fix โ Stop. Revert the change, or check whether AWS now allows updating it via aws eks update-cluster-config and wait for provider support. For real migrations, build a second cluster and migrate workloads (blue/green).
Prevent โ Gate any plan containing delete on aws_eks_cluster, RDS, or other stateful/critical resources.
Q119 A for_each resource is replaced after changing a key from "web-1" to "web_1" for naming consistency.
Beginner
for_each resource is replaced after changing a key from "web-1" to "web_1" for naming consistency.Likely causes โ for_each keys are part of the resource address; a new key means a new object.
Diagnose โ The plan shows aws_instance.app["web-1"] destroyed and ["web_1"] created.
Fix โ
moved {
from = aws_instance.app["web-1"]
to = aws_instance.app["web_1"]
}Prevent โ Choose stable keys early; never derive keys from mutable display names.
Q120 Error: Provider produced inconsistent final plan โ .tags_all: new element "Owner" has appeared.
Advanced
Error: Provider produced inconsistent final plan โ .tags_all: new element "Owner" has appeared.Likely causes โ A tag value was unknown at plan time (comes from another resource) and got combined with default_tags, so the provider's final plan didn't match the first plan. Known AWS provider edge case.
Diagnose โ Find tags whose values are (known after apply) in the plan.
Fix โ Re-run the apply; usually the second run succeeds since values are now known. Better: avoid tags computed from resources created in the same run.
Prevent โ Keep tag values static (variables/locals); report persistent cases to the provider issue tracker.
Q121 Error: Provider produced inconsistent result after apply โ Root object was present, but now absent.
Advanced
Error: Provider produced inconsistent result after apply โ Root object was present, but now absent.Likely causes โ The provider created the object, but the read right after returned "not found" due to eventual consistency, or something deleted it immediately (an SCP or a cleanup Lambda). It's labelled a provider bug.
Diagnose โ Check whether the object exists now; check CloudTrail for create and delete events.
Fix โ If it exists, terraform import it (it isn't in state). If not, re-apply. Upgrade the provider if a fix exists.
Prevent โ Keep providers current, and avoid automation that deletes resources right after creation.
Q122 Plans show ~ policy changes for an S3 bucket policy built with jsonencode() because principal values flip between "123456789012" and "arn:aws:iam::123456789012:root".
Intermediate
~ policy changes for an S3 bucket policy built with jsonencode() because principal values flip between "123456789012" and "arn:aws:iam::123456789012:root".Likely causes โ AWS stores principals in normalized ARN form; your code uses a bare account ID.
Diagnose โ Compare aws s3api get-bucket-policy output with your code.
Fix โ Write principals in full ARN form, or build the policy with data "aws_iam_policy_document", which the provider compares semantically.
Prevent โ Use policy document data sources for every IAM/S3/KMS policy.
Q123 Error: Unsupported block type โ Blocks of type "ingres" are not expected here. Did you mean "ingress"? and similar validate errors after a provider upgrade.
Intermediate
Error: Unsupported block type โ Blocks of type "ingres" are not expected here. Did you mean "ingress"? and similar validate errors after a provider upgrade.Likely causes โ Typos are easy, but after upgrades, blocks may have become attributes (or were removed), so the old block syntax is no longer valid.
Diagnose โ terraform validate and the provider upgrade guide.
Fix โ Correct the typo or convert syntax (for example, block x { } โ attribute x = { } where the provider changed the schema).
Prevent โ Run terraform validate and tflint in pre-commit.
Q124 A plan replaces an ALB target group after changing port, and the apply fails with ResourceInUse: Target group is currently in use by a listener or a rule.
Advanced
port, and the apply fails with ResourceInUse: Target group is currently in use by a listener or a rule.Likely causes โ port/protocol/name force replacement. Destroy-before-create can't delete a target group still referenced by a listener.
Diagnose โ aws elbv2 describe-target-groups --names app-tg --query 'TargetGroups[0].LoadBalancerArns'
Fix โ Use name_prefix and create_before_destroy = true on the target group so the listener moves to the new one first.
Prevent โ Make replaceable resources CBD-friendly (name_prefix, no hardcoded names).
Q125 The plan shows known after apply on a module output, and every consumer resource now shows "must be replaced".
Intermediate
known after apply on a module output, and every consumer resource now shows "must be replaced".Likely causes โ A change inside the module (or module depends_on) makes an output unknown; consumers using it in forcing arguments (like subnet_id) are planned for replacement because the value might change.
Diagnose โ Trace which output is unknown and why: terraform show -json tfplan | jq '.resource_changes[] | select(.change.actions|index("delete")) | .address'.
Fix โ Remove the cause of the unknown (often a needless depends_on, or a replacing resource upstream); apply the upstream change first with a targeted apply if needed.
Prevent โ Keep module outputs derived from stable attributes, and avoid module-level depends_on.
Authentication and Cloud API Errors
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q126 Error: No valid credential sources found followed by no EC2 IMDS role found ... context deadline exceeded.
Beginner
Error: No valid credential sources found followed by no EC2 IMDS role found ... context deadline exceeded.Likely causes โ No credentials in env vars, no profile, no SSO session; the provider fell back to the EC2 metadata service, which doesn't exist on your laptop or runner.
Diagnose โ
env | grep -E '^AWS_(PROFILE|ACCESS_KEY_ID|REGION|ROLE_ARN|WEB_IDENTITY)'
aws sts get-caller-identityFix โ export AWS_PROFILE=prod-admin && aws sso login, or in CI configure OIDC credentials before Terraform runs.
Prevent โ Print aws sts get-caller-identity as the first step of every pipeline.
Q127 Error: Cannot assume IAM Role โ api error AccessDenied: User: arn:aws:iam::111111111111:user/ci is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::222222222222:role/terraform.
Intermediate
Error: Cannot assume IAM Role โ api error AccessDenied: User: arn:aws:iam::111111111111:user/ci is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::222222222222:role/terraform.Likely causes โ The target role's trust policy doesn't trust the caller, the caller's identity policy lacks sts:AssumeRole on that ARN, a required ExternalId is missing, or an SCP denies it.
Diagnose โ
aws iam get-role --role-name terraform --query 'Role.AssumeRolePolicyDocument' # run in account 222
aws sts assume-role --role-arn arn:aws:iam::222222222222:role/terraform --role-session-name testFix โ Add the caller as Principal in the trust policy and sts:AssumeRole in the caller's policy; pass external_id in assume_role {} if the trust policy requires it.
Prevent โ Manage trust relationships in Terraform in a central IAM stack, tested by a pipeline smoke test.
Q128 GitHub Actions: Could not assume role with OIDC: Not authorized to perform sts:AssumeRoleWithWebIdentity.
Intermediate
Could not assume role with OIDC: Not authorized to perform sts:AssumeRoleWithWebIdentity.Likely causes โ The trust policy's token.actions.githubusercontent.com:sub condition doesn't match (different repo, branch, environment or PR context); wrong aud; the OIDC provider isn't created in that account.
Diagnose โ Common sub values: repo:acme/infra:ref:refs/heads/main, repo:acme/infra:environment:prod, repo:acme/infra:pull_request. Compare with the trust policy.
Fix โ
"Condition": {
"StringEquals": { "token.actions.githubusercontent.com:aud": "sts.amazonaws.com" },
"StringLike": { "token.actions.githubusercontent.com:sub": "repo:acme/infra:environment:prod" }
}Prevent โ Separate roles for plan (PRs, read-only) and apply (protected environment only).
Q129 GitHub Actions log says It looks like you might be trying to authenticate with OIDC. Did you mean to set the id-token permission?
Beginner
It looks like you might be trying to authenticate with OIDC. Did you mean to set the id-token permission?Likely causes โ The workflow lacks permissions: id-token: write, so no OIDC token can be requested.
Diagnose โ Check the job/workflow permissions: block.
Fix โ
permissions:
id-token: write
contents: readPrevent โ Keep a reusable Terraform workflow with the right permissions instead of copy-pasting YAML.
Q130 Applies that run longer than an hour fail with ExpiredToken: The security token included in the request is expired.
Intermediate
ExpiredToken: The security token included in the request is expired.Likely causes โ Assumed-role sessions default to 1 hour; long applies (EKS, RDS, CloudFront) outlive them. Role chaining is capped at 1 hour.
Diagnose โ The error appears about 60 minutes into the apply.
Fix โ Increase the role's MaxSessionDuration and request more (for example role-duration-seconds: 7200 in the GitHub action, or duration = "2h" in the provider's assume_role). Avoid chaining roles when long sessions are needed.
Prevent โ Split slow resources into their own stacks, and set session durations deliberately.
Q131 InvalidClientTokenId: The security token included in the request is invalid.
Beginner
InvalidClientTokenId: The security token included in the request is invalid.Likely causes โ Deleted/rotated access keys still in env vars or ~/.aws/credentials; a session token missing (AWS_SESSION_TOKEN not set for temporary creds); using credentials in an opt-in region not enabled for the account.
Diagnose โ aws configure list shows which source is in use.
Fix โ Remove stale AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY, set all three values for temporary creds, or re-login via SSO.
Prevent โ Prefer SSO and OIDC over long-lived keys.
Q132 failed to refresh cached credentials, refresh cached SSO token failed, unable to refresh SSO token ... InvalidGrantException.
Beginner
failed to refresh cached credentials, refresh cached SSO token failed, unable to refresh SSO token ... InvalidGrantException.Likely causes โ The IAM Identity Center session expired.
Diagnose โ aws sts get-caller-identity --profile prod-admin fails the same way.
Fix โ aws sso login --profile prod-admin (or --sso-session name), then rerun.
Prevent โ Use sso_session profiles so refresh tokens work, and set a session length that fits your working day.
Q133 UnauthorizedOperation: You are not authorized to perform this operation. Encoded authorization failure message: ...
Intermediate
UnauthorizedOperation: You are not authorized to perform this operation. Encoded authorization failure message: ...Likely causes โ Missing EC2 permission, an explicit deny (SCP, permission boundary), or a condition such as required tags or allowed regions.
Diagnose โ
aws sts decode-authorization-message --encoded-message '<message>' \
--query DecodedMessage --output text | jq .It shows the action, resource and which policy type denied it.
Fix โ Add the permission, or adjust code to satisfy the condition (add required tags, use allowed regions/instance types).
Prevent โ Keep the Terraform role's permissions in code and test new resource types in a sandbox account first.
Q134 AccessDenied ... with an explicit deny in a service control policy.
Intermediate
AccessDenied ... with an explicit deny in a service control policy.Likely causes โ An Organizations SCP blocks that action/region/resource (for example, deny outside approved regions, deny creating IAM users, deny without tags).
Diagnose โ The error names the SCP deny; ask the platform team for the SCP, or check aws organizations list-policies-for-target with access to the management account.
Fix โ Change your code to comply (region, tags) or request an approved exception. You can't bypass an SCP from the account.
Prevent โ Publish SCP guardrails to teams and encode them as policy-as-code checks (OPA/Sentinel/Checkov) that fail in PR.
Q135 SignatureDoesNotMatch / Signature expired: ... is now earlier than ... on a VM-based runner.
Beginner
SignatureDoesNotMatch / Signature expired: ... is now earlier than ... on a VM-based runner.Likely causes โ Clock skew: the runner's clock is off by more than 5 minutes; or a secret key with copy-paste whitespace.
Diagnose โ date -u vs real time; timedatectl status.
Fix โ Enable NTP (timedatectl set-ntp true, chrony), or fix the secret value.
Prevent โ Use OIDC instead of static keys, and keep runner images with time sync enabled.
Q136 Terraform deploys to the wrong account: resources appear in the sandbox account although you targeted prod.
Advanced
Likely causes โ AWS_PROFILE/env credentials override what you expected; assume_role missing in the provider; static keys from a previous job lingering on a self-hosted runner.
Diagnose โ aws sts get-caller-identity; terraform state show an affected resource to see the ARN's account ID.
Fix โ Destroy the misplaced resources (or state rm and clean up manually), then fix credentials and apply to prod.
Prevent โ Add allowed_account_ids to the provider; Terraform refuses to run against any other account:
provider "aws" {
region = "ap-south-1"
allowed_account_ids = ["222222222222"]
} Q137 Lambda create fails with InvalidParameterValueException: The role defined for the function cannot be assumed by Lambda. right after Terraform created the role.
Intermediate
InvalidParameterValueException: The role defined for the function cannot be assumed by Lambda. right after Terraform created the role.Likely causes โ IAM eventual consistency: the role exists but hasn't propagated to Lambda yet. Or the trust policy doesn't list lambda.amazonaws.com.
Diagnose โ Retry after a minute; if it still fails, check the trust policy.
Fix โ Re-run apply (usually succeeds). The AWS provider retries for a while; if not enough, add a short time_sleep depending on the role and policy attachments.
Prevent โ Make the function depend on the policy attachments too (depends_on = [aws_iam_role_policy_attachment.logs]), and keep providers updated for better retry logic.
Q138 EC2 launch fails with Invalid IAM Instance Profile name though the instance profile was created seconds before.
Beginner
Invalid IAM Instance Profile name though the instance profile was created seconds before.Likely causes โ Same eventual consistency for instance profiles; propagation can take several seconds.
Diagnose โ aws iam get-instance-profile --instance-profile-name app works when you check later.
Fix โ Re-apply. If it recurs, add a time_sleep of 10โ15 seconds after the instance profile.
Prevent โ Create IAM in a separate, earlier stack for things that are reused.
Q139 Error: waiting for EKS Node Group (prod:general) create: unexpected state 'CREATE_FAILED' after 20+ minutes.
Advanced
Error: waiting for EKS Node Group (prod:general) create: unexpected state 'CREATE_FAILED' after 20+ minutes.Likely causes โ Nodes couldn't join: no route to the EKS endpoint/ECR (private subnets without NAT or VPC endpoints), node role missing AmazonEKSWorkerNodePolicy/AmazonEC2ContainerRegistryReadOnly/CNI policy, insufficient capacity for the instance type, or access entries missing.
Diagnose โ
aws eks describe-nodegroup --cluster-name prod --nodegroup-name general \
--query 'nodegroup.health.issues'Issues like NodeCreationFailure or AsgInstanceLaunchFailures point to the cause.
Fix โ Fix networking/IAM/capacity, then re-apply. The failed node group is in state as tainted and will be replaced.
Prevent โ Use a known-good VPC module with endpoints/NAT, and test node group config in a dev cluster.
Q140 Destroying a VPC hangs, then fails with DependencyViolation: The subnet 'subnet-0abc' has dependencies and cannot be deleted.
Intermediate
DependencyViolation: The subnet 'subnet-0abc' has dependencies and cannot be deleted.Likely causes โ Resources not managed by this state still use the subnet: ENIs from Lambda in VPC, load balancers created by the Kubernetes controller, EKS nodes, VPC endpoints from another stack.
Diagnose โ
aws ec2 describe-network-interfaces --filters Name=subnet-id,Values=subnet-0abc \
--query 'NetworkInterfaces[].[NetworkInterfaceId,InterfaceType,Description]' --output tableFix โ Delete the dependents first (Kubernetes Service type=LoadBalancer/Ingress before cluster teardown; wait for Lambda hyperplane ENIs to be released, which can take up to around 40 minutes), then re-run destroy.
Prevent โ Destroy in reverse stack order (workloads โ cluster โ network) and tag controller-created resources.
Q141 Detaching an internet gateway fails: Network vpc-0abc has some mapped public address(es). Please unmap those public address(es) before detaching the gateway.
Intermediate
Network vpc-0abc has some mapped public address(es). Please unmap those public address(es) before detaching the gateway.Likely causes โ Elastic IPs or public IPs (NAT gateways, instances, load balancers) are still in the VPC, possibly not managed by this stack.
Diagnose โ aws ec2 describe-addresses --filters Name=domain,Values=vpc and check NAT gateways in the VPC.
Fix โ Delete NAT gateways/EIPs/instances first, then destroy again. Make the IGW explicitly depend on nothing that outlives it.
Prevent โ Keep the network stack's dependents in other stacks destroyed first.
Q142 BucketNotEmpty: The bucket you tried to delete is not empty during terraform destroy.
Beginner
BucketNotEmpty: The bucket you tried to delete is not empty during terraform destroy.Likely causes โ Objects (or object versions/delete markers in a versioned bucket) remain.
Diagnose โ aws s3api list-object-versions --bucket logs --max-items 5
Fix โ For disposable buckets set force_destroy = true, apply, then destroy. For important buckets, empty them deliberately or keep them.
Prevent โ force_destroy = true only in ephemeral environments.
Q143 Applies fail randomly with ThrottlingException: Rate exceeded or RequestLimitExceeded in large stacks.
Intermediate
ThrottlingException: Rate exceeded or RequestLimitExceeded in large stacks.Likely causes โ Many resources refreshed/created in parallel (default 10), plus other tools hitting the same API.
Diagnose โ TF_LOG=DEBUG shows retries; errors cluster on one service (Route 53, IAM, API Gateway have low limits).
Fix โ Lower parallelism and raise retries:
terraform apply -parallelism=4provider "aws" {
max_retries = 25
}Prevent โ Split huge states; Route 53 and IAM-heavy stacks especially.
Q144 Error: creating EC2 Instance: VcpuLimitExceeded: You have requested more vCPU capacity than your current vCPU limit.
Beginner
Error: creating EC2 Instance: VcpuLimitExceeded: You have requested more vCPU capacity than your current vCPU limit.Likely causes โ Service quota for the instance family (On-Demand Standard instances counted in vCPUs) is reached; new accounts start with low limits.
Diagnose โ
aws service-quotas get-service-quota --service-code ec2 --quota-code L-1216C47AFix โ Request an increase (Service Quotas console or request-service-quota-increase), or use smaller/fewer instances.
Prevent โ Manage quota requests per account in your account-vending process.
Q145 Unsupported: Your requested instance type (t3.micro) is not supported in your requested Availability Zone (us-east-1e).
Intermediate
Unsupported: Your requested instance type (t3.micro) is not supported in your requested Availability Zone (us-east-1e).Likely causes โ Hardcoded AZs or data "aws_availability_zones" returning AZs without that instance type.
Diagnose โ
aws ec2 describe-instance-type-offerings --location-type availability-zone \
--filters Name=instance-type,Values=t3.micro --query 'InstanceTypeOfferings[].Location'Fix โ Use data "aws_ec2_instance_type_offerings" to pick supported AZs, or exclude the bad AZ.
Prevent โ Don't hardcode AZ names; AZ letters differ per account.
Q146 InvalidAMIID.NotFound when deploying the same code to a new region.
Beginner
InvalidAMIID.NotFound when deploying the same code to a new region.Likely causes โ AMI IDs are region-specific; a hardcoded ami- from another region.
Diagnose โ aws ec2 describe-images --image-ids ami-0abc --region ap-south-1
Fix โ Look up the AMI per region with data "aws_ami" or an SSM public parameter (/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64).
Prevent โ No hardcoded AMI IDs in reusable modules.
Q147 An ACM certificate with DNS validation causes the apply to hang on aws_acm_certificate_validation until timeout.
Advanced
aws_acm_certificate_validation until timeout.Likely causes โ Validation CNAMEs weren't created, were created in the wrong hosted zone (private zone, or a zone not delegated from the registrar), or CAA records block Amazon.
Diagnose โ
aws acm describe-certificate --certificate-arn <arn> --query 'Certificate.DomainValidationOptions'
dig +short _abc123.api.example.com CNAME
dig +short NS example.comFix โ Create the records in the public authoritative zone (for_each over domain_validation_options), fix delegation/CAA, then re-apply.
Prevent โ Keep certificate + validation records in one module, and remember CloudFront certificates must be in us-east-1.
Q148 ResourceConflictException: The operation cannot be performed at this time. An update is in progress for resource on Lambda updates.
Intermediate
ResourceConflictException: The operation cannot be performed at this time. An update is in progress for resource on Lambda updates.Likely causes โ Two updates to the same function (code and configuration) in quick succession; Lambda only allows one at a time while LastUpdateStatus is InProgress.
Diagnose โ aws lambda get-function-configuration --function-name api --query '[State,LastUpdateStatus]'
Fix โ Re-run apply. Upgrade the AWS provider (newer versions wait for update completion). Avoid other tools updating the same function concurrently.
Prevent โ One owner for function updates (Terraform or the app deploy pipeline, not both).
Q149 MalformedPolicyDocument: Has prohibited field Resource when creating an IAM role.
Intermediate
MalformedPolicyDocument: Has prohibited field Resource when creating an IAM role.Likely causes โ You put an identity policy (with Resource) in assume_role_policy. Trust policies must have Principal and Action: sts:AssumeRole, no Resource.
Diagnose โ Inspect what's passed to assume_role_policy.
Fix โ Separate them: trust policy via data "aws_iam_policy_document" with principals {}; permissions via aws_iam_role_policy or attachments.
Prevent โ Name documents clearly (*_trust, *_permissions) in code.
Q150 A KMS-encrypted resource fails with AccessDeniedException ... is not authorized to perform: kms:CreateGrant, though the role has kms:* in its IAM policy.
Advanced
AccessDeniedException ... is not authorized to perform: kms:CreateGrant, though the role has kms:* in its IAM policy.Likely causes โ KMS needs both the key policy and IAM policy to allow access (unless the key policy delegates to IAM with the account root principal). Cross-account keys need the key policy to name your account.
Diagnose โ
aws kms get-key-policy --key-id <key-id> --policy-name default --output text | jq .Look for the root principal statement or your role.
Fix โ Add the Terraform role (or root delegation) to the key policy, including kms:CreateGrant with kms:GrantIsForAWSResource for services like EBS/RDS.
Prevent โ Manage key policies in Terraform with the consumers declared explicitly, and test with a sandbox key.
Modules, Variables, Sensitive Values and Workspaces
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q151 Error: Module not installed โ This module is not yet installed. Run "terraform init" to install all modules required by this configuration.
Beginner
Error: Module not installed โ This module is not yet installed. Run "terraform init" to install all modules required by this configuration.Likely causes โ A new module block was added or its source/version changed since the last init.
Diagnose โ cat .terraform/modules/modules.json | jq '.Modules[] | {Key, Source, Version}'
Fix โ terraform init (or terraform get for modules only).
Prevent โ Every CI job runs init first; don't cache .terraform/modules across commits.
Q152 Error: Failed to download module โ git::https://github.com/acme/tf-modules.git?ref=v2.3.0 ... exit status 128 in CI, works locally.
Intermediate
Error: Failed to download module โ git::https://github.com/acme/tf-modules.git?ref=v2.3.0 ... exit status 128 in CI, works locally.Likely causes โ Private repo and the CI runner has no Git credentials (your laptop uses your SSH key or credential helper); the tag doesn't exist; SSH host key not trusted.
Diagnose โ
git ls-remote https://github.com/acme/tf-modules.git v2.3.0
GIT_TERMINAL_PROMPT=0 git clone https://github.com/acme/tf-modules.git /tmp/mFix โ Give the job a token via Git config:
git config --global url."https://x-access-token:${GH_TOKEN}@github.com/".insteadOf "https://github.com/"or an SSH deploy key with known_hosts. Push the missing tag if needed.
Prevent โ Publish internal modules to a private registry (HCP Terraform, Artifactory, GitLab) with versioned releases.
Q153 Error: Invalid module source address for source = "github.com/acme/modules//vpc?ref=v1.0" with a version = "1.0.0" argument.
Beginner
Error: Invalid module source address for source = "github.com/acme/modules//vpc?ref=v1.0" with a version = "1.0.0" argument.Likely causes โ version only works with registry sources; Git sources pin with ?ref=. Also typos in the //subdir syntax.
Diagnose โ Read the source string carefully: registry format is namespace/name/provider.
Fix โ Remove version for Git sources; keep ?ref=v1.0.0. For registry: source = "terraform-aws-modules/vpc/aws" and version = "~> 5.0".
Prevent โ Use registry sources where possible and pin versions.
Q154 After bumping a registry module from 4.x to 5.x, plan shows dozens of resources destroyed and recreated.
Advanced
Likely causes โ Major module versions rename internal resources, switch count to for_each, or change defaults. Without moved blocks in the module (some include them), addresses no longer match.
Diagnose โ Read the module's upgrade guide/CHANGELOG; list destroyed vs created addresses from terraform show -json tfplan.
Fix โ Revert the bump. Add moved blocks in your root mapping old โ new addresses, set any changed defaults explicitly, and re-plan until only expected in-place updates remain.
Prevent โ Pin modules with ~> 4.2 (no surprise majors), and upgrade majors in a dedicated PR per stack.
Q155 Error: No value for required variable โ The root module input variable "db_password" is not set, and has no default value. in CI.
Beginner
Error: No value for required variable โ The root module input variable "db_password" is not set, and has no default value. in CI.Likely causes โ The local terraform.tfvars (gitignored) or your shell's TF_VAR_db_password isn't present in CI.
Diagnose โ env | grep TF_VAR_ in the job (masked); check which -var-file is passed.
Fix โ Provide it from the CI secret store as TF_VAR_db_password, or better, read the secret inside Terraform from Secrets Manager (or use an ephemeral resource/write-only argument where supported).
Prevent โ Commit non-secret env/*.tfvars; keep secrets out of variables entirely where possible.
Q156 Error: Invalid value for variable โ This was checked by the validation rule at variables.tf:8,3-13.
Beginner
Error: Invalid value for variable โ This was checked by the validation rule at variables.tf:8,3-13.Likely causes โ The value fails a validation block (wrong environment name, CIDR format, instance type not in the allowed list).
Diagnose โ The error prints the rule's error_message; check the value from tfvars/env.
Fix โ Correct the input. If the rule is too strict, update the validation in a reviewed PR.
Prevent โ Write clear error_message text that says what's allowed.
Q157 Warning: Value for undeclared variable โ The root module does not declare a variable named "regoin" but a value was found in file "terraform.tfvars". and the region silently defaults.
Intermediate
Warning: Value for undeclared variable โ The root module does not declare a variable named "regoin" but a value was found in file "terraform.tfvars". and the region silently defaults.Likely causes โ Typo in tfvars; Terraform only warns for undeclared vars in files (it errors for -var on the CLI). The real variable keeps its default.
Diagnose โ Read warnings, not just errors; terraform console โ var.region.
Fix โ Fix the typo. Consider removing defaults for important variables so a missing value fails.
Prevent โ Fail CI on warnings for undeclared variables (grep the plan output), and use tflint rules.
Q158 Error: Output refers to sensitive values โ Terraform requires that any root module output containing sensitive data be explicitly marked as sensitive.
Beginner
Error: Output refers to sensitive values โ Terraform requires that any root module output containing sensitive data be explicitly marked as sensitive.Likely causes โ The output uses a sensitive variable or a sensitive resource attribute (like aws_db_instance.main.password, or a module output marked sensitive).
Diagnose โ Trace the value; check for sensitive = true upstream.
Fix โ Add sensitive = true to the output. Read it with terraform output -raw db_password only when needed. Or don't output secrets at all.
Prevent โ Prefer outputting secret ARNs/names (Secrets Manager) instead of secret values.
Q159 A security review finds plaintext database passwords in the S3 state file, although variables are marked sensitive = true.
Advanced
sensitive = true.Likely causes โ sensitive only hides values in CLI output; state stores all arguments and attributes in plaintext JSON.
Diagnose โ terraform state pull | jq '.. | .password? // empty'
Fix โ Rotate the exposed credentials. Move to patterns that keep secrets out of state: manage_master_user_password = true on RDS (Secrets Manager), ephemeral resources (1.10+) and write-only arguments like password_wo (1.11+, provider support required).
Prevent โ Encrypt state with KMS, restrict s3:GetObject on state to the pipeline role, and treat state as secret.
Q160 Plan shows (sensitive value) for a whole user_data or tags block, and reviewers can't see what changes.
Intermediate
(sensitive value) for a whole user_data or tags block, and reviewers can't see what changes.Likely causes โ A single sensitive input (for example a token inside a template) marks the whole derived value as sensitive.
Diagnose โ Find which input in the template is sensitive.
Fix โ Don't embed secrets in user data; fetch them at boot from SSM/Secrets Manager using the instance role. Only use nonsensitive() if the value truly isn't secret.
Prevent โ Keep secrets out of templates so plans stay reviewable.
Q161 terraform apply in a terminal ran against prod because terraform workspace show said prod, though you selected dev earlier.
Intermediate
terraform apply in a terminal ran against prod because terraform workspace show said prod, though you selected dev earlier.Likely causes โ TF_WORKSPACE=prod is exported in the shell (overrides workspace select), or the .terraform/environment file was changed by another command in the same directory.
Diagnose โ echo $TF_WORKSPACE; cat .terraform/environment
Fix โ unset TF_WORKSPACE; re-select dev. Review what was applied to prod and revert via a proper plan if needed.
Prevent โ Show workspace and AWS account in your prompt; prefer separate directories/backends per environment for prod; protect prod with separate credentials that a dev shell doesn't have.
Q162 Workspace "staging" doesn't exist. You can create this workspace with the "new" subcommand. in CI.
Beginner
Workspace "staging" doesn't exist. You can create this workspace with the "new" subcommand. in CI.Likely causes โ Fresh backend key, wrong backend config, or a typo in the workspace name.
Diagnose โ terraform workspace list
Fix โ terraform workspace select -or-create staging (Terraform 1.4+), or check the backend first if it should already exist.
Prevent โ Use select -or-create only for ephemeral environments; for long-lived ones, a missing workspace should fail loudly (it often means the wrong backend).
Q163 A count = terraform.workspace == "prod" ? 3 : 1 setup created 3 NAT gateways in staging.
Intermediate
count = terraform.workspace == "prod" ? 3 : 1 setup created 3 NAT gateways in staging.Likely causes โ The CI job for staging ran in a workspace named prod (or the comparison uses a name that doesn't match, like production). Logic tied to terraform.workspace breaks silently.
Diagnose โ terraform workspace show in the job; compare names exactly.
Fix โ Drive sizing from per-env tfvars (nat_gateway_count = 1), not from the workspace name.
Prevent โ Use terraform.workspace only for naming; put behaviour differences in variables.
Q164 A module call with for_each = var.services fails: The "for_each" set includes values derived from resource attributes that cannot be determined until apply only on first apply.
Intermediate
for_each = var.services fails: The "for_each" set includes values derived from resource attributes that cannot be determined until apply only on first apply.Likely causes โ var.services (a root variable) is fine, but you merged it with something unknown (for example merge(var.services, { for k, v in aws_ecr_repository.r : k => ... })).
Diagnose โ terraform console with the plan state; trace which part is unknown.
Fix โ Keep the for_each keys from static data (keys(var.services)) and pass resource values inside each element.
Prevent โ Separate "which instances" (static keys) from "their config" (can be dynamic).
Q165 A child module's variable "tags" { type = map(string) } rejects your input: Invalid value for input variable ... element "cost_center": string required.
Beginner
variable "tags" { type = map(string) } rejects your input: Invalid value for input variable ... element "cost_center": string required.Likely causes โ A value in the map isn't a string (number, bool, null, nested object).
Diagnose โ Look at the element named in the error.
Fix โ Convert: cost_center = tostring(var.cost_center), or remove nulls with { for k, v in var.tags : k => v if v != null }.
Prevent โ Normalize tags in one locals block before passing to modules.
Q166 A module variable with type = object({ name = string, size = number }) fails when callers omit size.
Intermediate
variable with type = object({ name = string, size = number }) fails when callers omit size.Likely causes โ All object attributes are required unless marked optional.
Diagnose โ Error says attribute "size" is required.
Fix โ Use optional attributes with defaults (Terraform 1.3+):
variable "disk" {
type = object({
name = string
size = optional(number, 50)
})
}Prevent โ Design module inputs with optional() so adding fields isn't a breaking change.
Q167 Two module instances in the same account fail on second create with already exists errors for IAM roles and log groups.
Intermediate
already exists errors for IAM roles and log groups.Likely causes โ The module hardcodes names (name = "app-role") instead of building them from inputs; each instance tries the same name.
Diagnose โ grep -rn 'name\s*=\s*"' modules/app/
Fix โ Build names from a name/env input or use name_prefix.
Prevent โ Module guideline: every globally or account-unique name must derive from inputs.
Q168 A module output is null in the root although the resource exists.
Intermediate
null in the root although the resource exists.Likely causes โ The output uses try(aws_x.this[0].id, null) and the resource has a different index/key; or the resource is conditional (count = var.create ? 1 : 0) and create is false for this call.
Diagnose โ terraform state list | grep module.app, terraform console โ module.app.
Fix โ Correct the output expression (one(aws_x.this[*].id)), or set the create flag.
Prevent โ Avoid try() that hides real errors in outputs; return explicit nulls only for genuinely optional resources.
Q169 terraform apply with an ephemeral resource fails: Ephemeral values are not valid when you assign the secret to a normal resource argument.
Advanced
terraform apply with an ephemeral resource fails: Ephemeral values are not valid when you assign the secret to a normal resource argument.Likely causes โ Ephemeral values (Terraform 1.10+) can only flow into other ephemeral contexts: provider config, ephemeral variables/outputs, locals, provisioner/connection blocks, or write-only arguments (1.11+). Normal arguments would persist them in state, so Terraform blocks it.
Diagnose โ Check if the target argument is write-only in the provider docs (for example password_wo on aws_db_instance).
Fix โ
ephemeral "random_password" "db" {
length = 24
}
resource "aws_db_instance" "main" {
# ...
password_wo = ephemeral.random_password.db.result
password_wo_version = 1
}Bump password_wo_version to rotate.
Prevent โ Check Terraform and provider versions in CI support ephemeral/write-only features before adopting them.
Q170 A module with count = var.enabled ? 1 : 0 breaks every reference with Missing resource instance key after you added the count.
Intermediate
count = var.enabled ? 1 : 0 breaks every reference with Missing resource instance key after you added the count.Likely causes โ Module instances now have keys (module.cache[0]); references and state addresses changed.
Diagnose โ The plan wants to destroy module.cache.* and create module.cache[0].*.
Fix โ
moved {
from = module.cache
to = module.cache[0]
}Update references to module.cache[0].endpoint or one(module.cache[*].endpoint).
Prevent โ Add moved blocks whenever you add count/for_each to an existing module or resource.
Q171 Terragrunt: Error: Backend configuration changed appears in every module after upgrading Terragrunt.
Advanced
Error: Backend configuration changed appears in every module after upgrading Terragrunt.Likely causes โ The generated backend.tf content changed (new defaults, different key format or added settings), so cached .terraform doesn't match. Or .terragrunt-cache is stale.
Diagnose โ Compare generated backend files between versions; check remote_state block and path_relative_to_include() output.
Fix โ Clear caches (find . -type d -name .terragrunt-cache -prune -exec rm -rf {} +) and run terragrunt init -reconfigure. If the key changed, migrate state (-migrate-state) or fix the key back.
Prevent โ Pin the Terragrunt version (terragrunt_version_constraint) and test upgrades on one stack first.
Q172 HCP Terraform workspace plan fails with No value for required variable although the variable is set in the workspace.
Advanced
No value for required variable although the variable is set in the workspace.Likely causes โ The variable was created as an Environment variable (without the TF_VAR_ prefix) instead of a Terraform variable; or it's set on a variable set not attached to this workspace/project; or the CLI-driven run uses a local -var-file that HCP doesn't read.
Diagnose โ Check the workspace Variables page: category "terraform" vs "env", and attached variable sets.
Fix โ Create it as a Terraform variable (or env var named TF_VAR_name), and attach the variable set to the workspace or project.
Prevent โ Manage HCP variables and variable sets with the tfe provider.
Q173 A data source inside a reusable module fails in some accounts with no matching EC2 VPC found.
Intermediate
no matching EC2 VPC found.Likely causes โ The module looks up a VPC by tag (Name = "main") that isn't consistent across accounts, or the provider region differs.
Diagnose โ aws ec2 describe-vpcs --filters Name=tag:Name,Values=main --region ap-south-1
Fix โ Pass vpc_id as an input instead of looking it up inside the module.
Prevent โ Reusable modules should take IDs as inputs; root modules do the lookups.
Q174 A check block prints Warning: Check block assertion failed but the apply succeeds. Is this a problem?
Advanced
check block prints Warning: Check block assertion failed but the apply succeeds. Is this a problem?Likely causes โ By design, check blocks (1.5+) only warn; they don't block applies. A failed assertion means a health condition isn't met (for example an endpoint returns non-200).
Diagnose โ Read the assertion's error_message and test the target manually (curl -s -o /dev/null -w '%{http_code}' https://api.example.com/health).
Fix โ Investigate the real problem. If it must block, use a precondition/postcondition on the resource instead.
Prevent โ Send check failures from CI/HCP drift runs to alerting, so warnings aren't ignored.
Q175 Error: Resource postcondition failed โ The selected AMI must be for the x86_64 architecture. after a data source returns a new image.
Advanced
Error: Resource postcondition failed โ The selected AMI must be for the x86_64 architecture. after a data source returns a new image.Likely causes โ The postcondition caught a bad lookup result: filters matched an arm64 image published recently.
Diagnose โ terraform console โ data.aws_ami.app.architecture and .name.
Fix โ Tighten filters (architecture = x86_64, exact name pattern, owners) so the lookup is deterministic.
Prevent โ Keep postconditions on lookups whose results matter; they turn surprises into clear plan-time errors.
Partial Applies, Version Mismatches and CI Failures
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q176 An apply failed halfway: 14 resources created, then Error: creating RDS DB Instance ... InvalidParameterCombination. What state are you in?
Beginner
Error: creating RDS DB Instance ... InvalidParameterCombination. What state are you in?Likely causes โ Terraform doesn't roll back. Resources created before the failure are in state; the failed one is not (or is tainted if it was created but a later wait failed).
Diagnose โ
terraform state list | wc -l
terraform plan # shows only what is still missing or taintedFix โ Fix the cause (here, an invalid engine/instance class combination) and re-apply. Terraform continues from where it stopped.
Prevent โ terraform validate and dev-environment applies catch most parameter errors; keep stacks small so partial failures are easy to reason about.
Q177 Error: Saved plan is stale โ The given plan file can no longer be applied because the state was changed by another operation after the plan was created.
Intermediate
Error: Saved plan is stale โ The given plan file can no longer be applied because the state was changed by another operation after the plan was created.Likely causes โ Another apply (or a state command) ran on the same state between your plan -out and apply. Common when two PRs plan in parallel and one merges first.
Diagnose โ Check state history / pipeline runs between the plan and apply timestamps.
Fix โ Re-run terraform plan -out=tfplan, review, then apply the new plan.
Prevent โ Plan โ apply in one pipeline with concurrency control per stack; require branches to be up to date before merge.
Q178 Apply job fails: Failed to read plan from plan file โ The given plan file was created by Terraform v1.9.5, but this is v1.9.8.
Intermediate
Failed to read plan from plan file โ The given plan file was created by Terraform v1.9.5, but this is v1.9.8.Likely causes โ Plan and apply jobs used different Terraform versions (different runner images or setup-terraform with latest). Plan files are not portable across versions.
Diagnose โ terraform version in both job logs.
Fix โ Pin one version in both jobs and re-plan.
Prevent โ terraform_version: 1.9.8 in hashicorp/setup-terraform, plus required_version in code, plus a .terraform-version file shared with laptops.
Q179 Error: Unsupported Terraform Core version โ This configuration does not support Terraform version 1.5.7.
Beginner
Error: Unsupported Terraform Core version โ This configuration does not support Terraform version 1.5.7.Likely causes โ The code or a module requires a newer version (required_version = ">= 1.7") than your binary.
Diagnose โ grep -rn required_version . .terraform/modules
Fix โ Upgrade the binary (tfenv install 1.9.8 && tfenv use 1.9.8, or mise use [email protected]). Don't lower the constraint without knowing why it was set.
Prevent โ Version manager file in every repo; CI uses the same version.
Q180 A teammate with Terraform 1.10 applied; now your 1.6 gets state snapshot was created by Terraform v1.10.0, which is newer than current v1.6.6; upgrade to Terraform v1.10.0 or greater to work with this state.
Intermediate
state snapshot was created by Terraform v1.10.0, which is newer than current v1.6.6; upgrade to Terraform v1.10.0 or greater to work with this state.Likely causes โ Terraform records its version in state; older binaries refuse to read state written by newer ones (to avoid losing data they don't understand).
Diagnose โ terraform state pull | jq -r .terraform_version (from a newer binary).
Fix โ Upgrade your binary. Downgrading state isn't supported; restoring an older state version would lose changes.
Prevent โ Upgrade Terraform as a team decision: bump required_version lower bound and CI image in one PR; nobody applies with a different version.
Q181 Error: Resource instance managed by newer provider version โ The current state of aws_instance.web was created by a newer provider version than is currently selected.
Intermediate
Error: Resource instance managed by newer provider version โ The current state of aws_instance.web was created by a newer provider version than is currently selected.Likely causes โ Someone applied with a newer AWS provider (unlocked or an older lock file in your branch). State now has a newer schema version.
Diagnose โ Compare .terraform.lock.hcl on your branch with main.
Fix โ Rebase on main and terraform init, or terraform init -upgrade to at least that provider version.
Prevent โ Commit the lock file; CI uses -lockfile=readonly; Renovate manages provider bumps.
Q182 In GitHub Actions, terraform plan -detailed-exitcode makes the job fail whenever there are changes.
Beginner
terraform plan -detailed-exitcode makes the job fail whenever there are changes.Likely causes โ -detailed-exitcode returns 0 (no changes), 1 (error), 2 (changes present). Most CI systems treat any non-zero exit as failure.
Diagnose โ The job log shows exit code 2.
Fix โ
set +e
terraform plan -detailed-exitcode -out=tfplan -input=false
code=$?
set -e
if [ "$code" -eq 1 ]; then exit 1; fi
echo "has_changes=$([ "$code" -eq 2 ] && echo true || echo false)" >> "$GITHUB_OUTPUT"Prevent โ Wrap the logic in a shared script used by all pipelines.
Q183 terraform output -raw vpc_id in a GitHub Actions step returns extra text like ::debug:: lines or the command echo.
Intermediate
terraform output -raw vpc_id in a GitHub Actions step returns extra text like ::debug:: lines or the command echo.Likely causes โ hashicorp/setup-terraform installs a wrapper script by default that captures stdout/stderr into step outputs and adds noise when you capture output in shell.
Diagnose โ which terraform points to the wrapper script.
Fix โ
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.9.8
terraform_wrapper: falsePrevent โ Disable the wrapper in shared workflows that parse Terraform output.
Q184 A CI apply job waits until timeout after printing Do you want to perform these actions? ... Enter a value:.
Beginner
Do you want to perform these actions? ... Enter a value:.Likely causes โ terraform apply without a saved plan or -auto-approve asks for confirmation; there is no TTY in CI.
Diagnose โ The last log line is the prompt.
Fix โ Apply the reviewed plan file: terraform apply -input=false tfplan (a saved plan doesn't prompt).
Prevent โ Always plan -out then apply tfplan in pipelines; set TF_INPUT=0 globally.
Q185 The apply job uses the plan artifact from the plan job but fails with Error: Inconsistent dependency lock file or missing provider plugins.
Intermediate
Error: Inconsistent dependency lock file or missing provider plugins.Likely causes โ Only tfplan was passed between jobs; the apply runner has a fresh checkout with no .terraform, or a different commit checked out.
Diagnose โ Check the apply job runs terraform init and checks out the same commit SHA as the plan job.
Fix โ In the apply job: checkout the same SHA, terraform init -input=false -lockfile=readonly with the same backend config, then terraform apply tfplan.
Prevent โ Treat plan artifacts as tied to commit + Terraform version + lock file; store them with the SHA in the name.
Q186 terraform fmt -check fails in CI with exit code 3, but terraform validate passes.
Beginner
terraform fmt -check fails in CI with exit code 3, but terraform validate passes.Likely causes โ Files aren't canonically formatted; fmt -check returns non-zero when changes are needed.
Diagnose โ terraform fmt -check -recursive -diff
Fix โ terraform fmt -recursive and commit.
Prevent โ pre-commit hook (pre-commit-terraform terraform_fmt) and editor format-on-save.
Q187 Atlantis comments This project is currently locked by an unapplied plan from pull #412 on your PR.
Intermediate
This project is currently locked by an unapplied plan from pull #412 on your PR.Likely causes โ Atlantis holds a project/workspace lock from another PR that ran atlantis plan but wasn't applied or closed.
Diagnose โ Open the Atlantis UI locks page or PR #412.
Fix โ Apply or close #412, or run atlantis unlock on that PR (with the owner's agreement). Then atlantis plan again.
Prevent โ Auto-unlock on PR close, keep PRs small, and use separate projects per stack so locks are narrow.
Q188 PR plans pass, but the post-merge apply fails because the plan role is read-only and the apply role lacks a permission that the new resource needs.
Advanced
Likely causes โ Plans only need read/describe permissions; create/update permissions are first exercised at apply. A new resource type (for example aws_wafv2_web_acl) isn't in the apply role.
Diagnose โ The apply error names the denied action (AccessDenied ... wafv2:CreateWebACL); decode with aws sts decode-authorization-message if encoded.
Fix โ Add the permissions to the apply role (through its own IAM pipeline) and re-run apply. The partial apply resumes.
Prevent โ Apply new resource types in a dev account with the same role definition first; review role changes alongside infrastructure PRs.
Q189 A CI pipeline applies dev successfully, then the prod stage applies dev's plan file to prod state.
Advanced
dev successfully, then the prod stage applies dev's plan file to prod state.Likely causes โ Artifact name collision (tfplan used for both stages) or a missing -backend-config switch between stages. Terraform may error (Saved plan does not match the given state/stale) or, worse, apply with dev vars if the job re-plans with the wrong var file.
Diagnose โ Check artifact names, the var file and backend config used in each stage's logs.
Fix โ Stop the pipeline, inspect prod changes from the apply log, and correct with a proper prod plan.
Prevent โ Name artifacts tfplan-<env>-<sha>, run each environment in its own directory/job with explicit -backend-config=backends/<env>.hcl and -var-file=env/<env>.tfvars, and print account ID + workspace before apply.
Q190 Ctrl-C during an apply printed Two interrupts received. Exiting immediately. Note that data loss may have occurred.
Advanced
Two interrupts received. Exiting immediately. Note that data loss may have occurred.Likely causes โ The first Ctrl-C asks Terraform to stop gracefully after in-flight operations; the second kills it. In-flight resources may exist in the cloud but not in state, and the lock may remain.
Diagnose โ
terraform force-unlock <ID> # only if the lock remains and nothing else runs
terraform plan # look for creates of things that already existFix โ Import any resources that exist but are missing from state; untaint or replace half-created ones.
Prevent โ Press Ctrl-C once and wait. In CI, give jobs a graceful shutdown period.
Q191 CI and laptops produce different plans for archive_file / local_file resources (hashes differ).
Intermediate
archive_file / local_file resources (hashes differ).Likely causes โ Line endings (core.autocrlf on Windows), file permissions, or build artifacts present locally but not in CI change the archive content.
Diagnose โ git ls-files --eol | grep crlf; compare the source dirs.
Fix โ Add .gitattributes (* text=auto eol=lf), build artifacts in CI only, and exclude local junk with excludes in archive_file.
Prevent โ Run applies only from CI so one environment builds artifacts.
Q192 terraform test fails with Error: Test assertion failed in CI, while the same .tftest.hcl passes locally.
Intermediate
terraform test fails with Error: Test assertion failed in CI, while the same .tftest.hcl passes locally.Likely causes โ Tests with command = apply create real resources and depend on credentials/region/account quotas; local runs use different variables; leftover resources from a previous failed run conflict.
Diagnose โ terraform test -verbose to see the plan/apply inside each run block; check for orphaned test resources by tag.
Fix โ Pass explicit variables in the test file, use unique names per run (for example a random_id in a setup module), and clean orphans.
Prevent โ Use command = plan and mocked providers (mock_provider, Terraform 1.7+) for most unit tests; reserve apply tests for a dedicated test account.
Q193 OpenTofu migration: after switching CI to tofu, init shows provider downloads from registry.opentofu.org and the lock file diff changes all hashes.
Intermediate
tofu, init shows provider downloads from registry.opentofu.org and the lock file diff changes all hashes.Likely causes โ OpenTofu resolves the default hashicorp/aws source against its own registry, so lock file entries and hashes differ from Terraform's.
Diagnose โ git diff .terraform.lock.hcl shows registry.opentofu.org/hashicorp/aws.
Fix โ Commit the regenerated lock file once everyone moves to OpenTofu. Don't alternate between terraform and tofu on the same state; newer features diverge.
Prevent โ Pick one tool per repo and pin its version; run tofu providers lock for all platforms.
Q194 A pipeline runs terraform apply -target=module.app "to save time". Weeks later, plans show many unrelated pending changes.
Advanced
terraform apply -target=module.app "to save time". Weeks later, plans show many unrelated pending changes.Likely causes โ -target applies only part of the graph; everything else drifts from code, and dependent changes pile up. Terraform warns: Resource targeting is in effect.
Diagnose โ Run a full terraform plan and review the accumulated diff.
Fix โ Review and apply the full plan in a maintenance window, in steps if needed.
Prevent โ Ban -target in CI except for documented recovery runbooks; split large stacks instead of targeting.
Q195 Pull request plans fail with Error: Error acquiring the state lock whenever two PRs touch the same stack.
Intermediate
Error: Error acquiring the state lock whenever two PRs touch the same stack.Likely causes โ Plans take the state lock too (to read a consistent snapshot); two concurrent PR plans collide.
Diagnose โ Pipeline logs show overlapping plan runs on the same key.
Fix โ Retry with a lock timeout: terraform plan -lock-timeout=5m. For PR plans, -lock=false is acceptable only for read-only speculative plans that are never applied.
Prevent โ Concurrency groups per stack in CI; HCP Terraform/Atlantis queue runs automatically.
Q196 Renovate opened a PR bumping the AWS provider; CI plan shows changes in 30 stacks. How do you triage?
Advanced
Likely causes โ New provider defaults, schema normalization or deprecations; most diffs are harmless, a few might be real behaviour changes.
Diagnose โ
for d in stacks/*/; do
(cd "$d" && terraform plan -input=false -out=tfplan >/dev/null && \
terraform show -json tfplan | jq -r --arg d "$d" '.resource_changes[] | select(.change.actions != ["no-op"]) | "\($d) \(.address) \(.change.actions|join(","))"')
done | sort | uniq -cGroup by resource type and action; any delete/create is a red flag.
Fix โ Approve state-only updates; for real changes, set arguments explicitly or pin the old version for affected stacks and report upstream.
Prevent โ Small, frequent provider bumps and a plan-summary bot that flags destroys.
Q197 An apply in CI fails with Error: Failed to install provider ... checksum list has no SHA-256 hash or registry 429 Too Many Requests when 50 jobs start together.
Advanced
Error: Failed to install provider ... checksum list has no SHA-256 hash or registry 429 Too Many Requests when 50 jobs start together.Likely causes โ Many runners downloading the same providers simultaneously from the public registry/releases endpoint, hitting rate limits or partial downloads.
Diagnose โ Failures correlate with burst start times; the same jobs pass on retry.
Fix โ Retry with backoff; pre-bake providers into the runner image or use a network/filesystem mirror and CI cache keyed on the lock file.
Prevent โ Internal provider mirror (Artifactory/Nexus or terraform providers mirror to S3) for all CI runners.
Q198 terraform destroy in an ephemeral PR environment leaves resources behind and the cleanup job reports success.
Advanced
terraform destroy in an ephemeral PR environment leaves resources behind and the cleanup job reports success.Likely causes โ The destroy job ran with a different workspace/backend key (so state was empty: No changes. No objects need to be destroyed.), or resources created outside Terraform (controllers, Lambda ENIs, log groups auto-created by services).
Diagnose โ Check the destroy log for 0 destroyed; find leftovers by tag:
aws resourcegroupstaggingapi get-resources --tag-filters Key=pr,Values=412Fix โ Rerun destroy with the correct workspace/key, then delete untracked leftovers.
Prevent โ Fail the cleanup job if it destroys 0 resources when state is expected; tag everything with pr=<number>; manage auto-created log groups explicitly in Terraform.
Q199 A self-hosted runner's apply fails with Error: Failed to load plugin schemas / Plugin did not respond.
Intermediate
Error: Failed to load plugin schemas / Plugin did not respond.Likely causes โ The provider process crashed or was OOM-killed (AWS provider can use over 1 GB on big stacks), the binary is for the wrong architecture, or /tmp is mounted noexec.
Diagnose โ
dmesg | grep -i -E 'killed process|oom'
file .terraform/providers/registry.terraform.io/hashicorp/aws/*/linux_amd64/terraform-provider-aws_*
mount | grep /tmpFix โ Give the runner more memory, lower -parallelism, or split the stack; fix arch/noexec issues and re-init.
Prevent โ Size runners for your largest stack and monitor memory per job.
Q200 Your CI apply worked yesterday, today terraform init fails with Error: Failed to query available provider packages for a pinned community provider version that "disappeared".
Advanced
terraform init fails with Error: Failed to query available provider packages for a pinned community provider version that "disappeared".Likely causes โ The provider author yanked or deleted that release, or moved the provider to a new namespace; the registry no longer serves it.
Diagnose โ Check the provider's registry page and GitHub releases; terraform providers shows which modules require it.
Fix โ Move to the nearest available version (update constraints and terraform init -upgrade), or switch to the new namespace with terraform state replace-provider:
terraform state replace-provider registry.terraform.io/oldns/kubectl registry.terraform.io/newns/kubectlPrevent โ Mirror every provider you depend on internally, prefer well-maintained providers, and keep the mirror as the source of truth for CI.