terraform state list # all addressesterraform state list module.vpc # filterterraform state show aws_instance.web # attributes of one resourceterraform state mv aws_instance.a aws_instance.b # rename in stateterraform state mv -state-out=other.tfstate aws_instance.a aws_instance.a # move between statesterraform state rm aws_instance.web # forget it, do not destroy itterraform state pull > backup.tfstate # download current stateterraform state push backup.tfstate # upload (dangerous, check serial)terraform state replace-provider registry.terraform.io/-/aws hashicorp/awsterraform show -json | jq '.values.root_module.resources[].address'
Rule
Why
Never edit terraform.tfstate by hand
Lineage and serial checks, easy to corrupt
Back up before surgery
state pull, plus S3 bucket versioning
State holds secrets in plain text
Encrypt the bucket, restrict access, never commit
Prefer moved, import and removed blocks over CLI surgery
import { to = aws_s3_bucket.legacy id = "my-existing-bucket"}import { for_each = var.subnet_ids to = aws_subnet.imported[each.key] id = each.value}moved { from = aws_instance.web to = module.web.aws_instance.this}removed { from = aws_instance.old lifecycle { destroy = false } # drop from state, keep in AWS}
bash
terraform plan -generate-config-out=generated.tf # draft code for import blocksterraform import aws_s3_bucket.legacy my-existing-bucket # legacy CLI formterraform apply -replace=aws_instance.web # recreate one resource
Variable precedence, lowest to highest: defaults, TF_VAR_*, terraform.tfvars, *.auto.tfvars (alphabetical), then -var and -var-file in command-line order.
OIDC trust sub values: repo:ORG/REPO:pull_request, repo:ORG/REPO:ref:refs/heads/main, repo:ORG/REPO:environment:prod. Gate prod with a GitHub environment that has required reviewers, and serialise runs with concurrency: { group: tf-prod, cancel-in-progress: false }.
What is Terraform? A declarative, provider-based tool that builds a dependency graph from your config, diffs it against state, and calls cloud APIs to make reality match.
Why state? It maps code addresses to real resource IDs and caches attributes, so Terraform knows what it owns and what to change.
Why remote state with locking? Teams need one shared source of truth and a lock so two applies cannot corrupt it. On S3 use versioning, encryption and use_lockfile.
plan versus apply?plan is a read-only diff. apply executes it. In CI we save the plan with -out and apply that exact file.
count versus for_each?count addresses by index and shifts when items are removed. for_each uses stable keys, so we default to it.
How do you handle secrets? Keep them out of code and Git, mark inputs sensitive, fetch from Secrets Manager or SSM at runtime, and remember they can still appear in state, so lock state down.
How do you structure environments? Separate root modules per environment sharing versioned modules, each with its own state key, rather than workspaces for dev and prod.
What is drift and how do you find it? Real infrastructure differing from state or code. plan -refresh-only shows it, and a scheduled plan with -detailed-exitcode alerts on it.
How do you adopt existing infrastructure? An import block plus plan -generate-config-out, then clean the code until the plan shows no changes.
How do you rename or refactor without downtime? Use moved blocks so the plan shows a move instead of destroy and create.
What does terraform destroy actually do, and how do you protect against it? It deletes everything in that state. Use prevent_destroy, deletion protection on resources, scoped IAM, and approval gates.
What do tflint and checkov do?tflint validates provider-specific correctness and conventions. checkov scans for insecure configuration. Both run on every PR before the plan is read.
How does CI authenticate to AWS? OIDC federation with short-lived tokens and separate roles: read-only for PR plans, scoped write for apply restricted to a protected environment.
How do you version and share modules? Semantic version tags in Git or a registry, pinned by consumers, upgraded dev first, and moved blocks to keep upgrades non-destructive.
A prod apply failed halfway. What now? Do not panic and do not re-run blindly. Read the error, check state with state list and the console, fix the cause, and run plan again. Terraform is idempotent, and the bucket's state versioning is the safety net if state itself is bad.