Terraform review
Goal
Stop a bad terraform apply before it runs, by reading the code and the plan the way an operator who owns the outage would.
A good review names each resource that will be destroyed or replaced and why, and what protects the data.
When to use
- The user pastes or points to a plan, a module or a pull request touching
.tffiles. - A plan shows replacements the user did not expect.
- State backend or workspace layout needs a check.
- For IAM policy depth, use
use_skillwithleast-privilege-iam.
Steps
- Find the files with
lsandrgforresource "andmodule ". Read the backend and provider blocks first withread_file. - Check provider and module version constraints. Look up current provider docs with
read_urlwhen an argument looks renamed or deprecated. - Run
terraform fmt -checkandterraform validatewithrun_command. Runterraform init -backend=falsefirst if the project is not initialised. - If a plan file or output exists, read it. If not, ask the user before running
terraform plan, because it reads real credentials and real state. - List every line marked destroy or replace (
-/+). For each, find the argument that forces it and say whether it is a rename, an immutable field or a tag change. - Check stateful resources (databases, buckets, disks, keys) for
prevent_destroy, deletion protection, and backup settings. - Search for secrets with
rgonpassword,secret,tokenandaccess_key. Values should come from variables markedsensitiveor a secrets manager. - Check security groups, bucket policies and IAM for
0.0.0.0/0,*actions and public access flags. - Check state: remote backend, locking, encryption, and that state is not committed to git.
- Check that variables have types and validation, outputs do not leak secrets, and
countorfor_eachkeys are stable values, not list positions. - Check environments for copy paste drift: names, regions and account ids that are hard coded instead of variables.
- Write the findings with the resource address, the risk and the fix.
Gotchas
- Reordering a list used with
countmakes Terraform replace resources whose index shifted.for_eachwith stable keys avoids it. - A plan reads state at one moment. Applying later with another change merged can differ from what was reviewed, so save the plan with
-out. terraform state rmandimportchange what Terraform believes without touching real infrastructure.- Sensitive values are hidden in plan output but still stored in plain text in state.
ignore_changescan hide drift that matters, such as a security group edited by hand.- Renaming a resource block plans a destroy and create unless a
movedblock is added.
Output
- Findings ranked by risk with resource address, cause and fix.
- A list of resources the plan destroys or replaces, with a safe or unsafe call on each.
- A verdict, and what was not checked because no plan or credentials were available.
