Topic 2 of 5 · 5 min read
Reading Terraform plans from agent changes
Goal: Read a Terraform plan from an agent's change and flag the resource that would be destroyed or exposed.
Never review infrastructure code alone: review the plan, because the plan shows what will really happen to real resources.
Check yourself · 1 of 3
In Terraform plan output, what does a line marked destroy and replace mean for a database?
Why it matters
Terraform and similar tools describe infrastructure as code. An agent can edit a few lines of .tf and the effect is a database deleted, or a port opened to the whole internet. The diff of the code looks small. The plan output shows the consequence.
How to read a plan
Run terraform plan and save it, so what you review is exactly what gets applied. Read the summary line first: how many resources to add, change and destroy. Then read each symbol:
+create.~change in place.-destroy.-/+destroy and replace. For a database, disk or load balancer this means data loss or downtime, even though the code change was one attribute.
The checks that catch most agent mistakes
- Any destroy or replace on stateful resources such as databases, volumes, buckets and queues. Look for the line that says why: "forces replacement".
- Open network rules. A security group with
0.0.0.0/0on SSH (22), a database port or all ports. - Public storage. A bucket policy or ACL that allows public reads. Cloud providers offer a block-public-access setting; check it stays on.
- Count surprises. The task said "add one rule" and the plan changes 14 resources.
- Unexpected drift. Changes you did not ask for may come from someone editing the console by hand, or from an agent renaming a resource, which Terraform sees as destroy plus create.
- Protection removed. A deleted
prevent_destroyor deletion-protection flag is a red flag in itself.
Before and after
resource "aws_security_group_rule" "db" {
type = "ingress"
from_port = 5432
to_port = 5432
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
The agent opened the database "so the new service can connect". The fix is to allow only the service's security group, not the world.
What goes wrong
- Renaming a resource block in code. State does not know the new name, so the plan destroys the old resource and creates a new one. A
movedblock fixes this. - Reviewing only the code diff and never opening the plan.
- Applying a plan generated earlier, after the code changed again.
- The agent runs
applyitself with auto-approve. Keep apply in the pipeline, behind a human.
Examples
The harmless rename
The agent renames aws_db_instance.main to aws_db_instance.primary for tidiness. The plan reads 1 to add, 0 to change, 1 to destroy. Without a moved block, the old database is deleted and an empty one created. Spotting the destroy line before apply is the whole job.
Task says one rule, plan says fourteen changes
A prompt asks for one new egress rule. The plan shows 14 resources changing because the agent also bumped a module version. Revert the unrelated change and plan again.
Key points
- Review the saved plan, not only the code diff, and apply exactly that plan.
- A destroy or a destroy-and-replace on a database, disk or bucket needs a stop and a question.
- Search plans for 0.0.0.0/0, public ACLs and removed protection flags.
- Compare the number of changed resources with what the task should touch.
- Agents should plan; humans or a gated pipeline should apply.
Common mistakes
- Reading the HCL diff and skipping the plan.
- Treating a change that says replace as a small in-place edit.
- Letting the agent run apply with auto-approve.
- Ignoring a 0.0.0.0/0 rule because the port is a normal service port.
Check yourself · 2 of 3
What is wrong with a database ingress rule that allows 0.0.0.0/0?
Check yourself · 3 of 3
Why should you review the saved plan and not only the code diff?
Practice items, not counted towards your mastery.
Start here
- strong_migrations: catch unsafe migrations in development (opens in a new tab)ToolFree
A catalogue of unsafe migration operations with the safe alternative for each.
- Parallel Change (martinfowler.com) (opens in a new tab)ArticleFree
The expand, migrate, contract pattern behind safe schema changes.
- terraform plan command (HashiCorp docs) (opens in a new tab)DocsFree
What plan output means and how to save and review a plan before apply.
More for this topic
- Blocking public access to your S3 storage (AWS docs) (opens in a new tab)DocsFree
The setting that stops a bucket from being made public by mistake.