Reading a plan and the dependency graph

Beginner · 5 min

Read a Terraform plan line by line, spot the replacement that will delete data, and see how references become the graph that sets order and parallelism.

Before thisHCL syntax: blocks, arguments and expressionsInit, validate, plan, apply and destroy in practice

Matthew Sanabria · 83 min · from 10:28

Reading a plan and the dependency graph

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Matthew SanabriaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Each plan line starts with a symbol: `+` create, `-` destroy, `~` change in place, `-/+` replace. A `-/+` means the real object is deleted and rebuilt.
  • `(known after apply)` means the value does not exist yet. Anything that depends on it is also unknown until the apply gets there.
  • When a provider says an argument `forces replacement`, changing it destroys the resource. This is the line to look for in every review.
  • Terraform turns references and `depends_on` into a directed graph, creates in dependency order, destroys in reverse and runs unrelated branches in parallel.
  • `terraform graph` prints that graph as DOT text that Graphviz can draw.

The plan is the contract

Someone changes instance_type on a database and opens a pull request. The diff is one line. The reviewer approves it, because a one-line diff looks harmless. After apply, the database is gone and a new empty one sits in its place, because that argument is one the provider cannot change on a live instance, so it replaced the whole object.

The code diff did not show that. The plan did. It is the only place Terraform tells you what a change will really do, and reading plans well is the most valuable habit in this tool.

Symbols and what they mean

Every resource in a plan starts with a symbol:

Symbol Meaning
+ create
- destroy
~ update in place
-/+ destroy and create again (replace)
<= read a data source

Under a ~ or -/+, each argument that differs shows old -> new. Look for two phrases:

  • (known after apply): the value is not available yet. It might be an ID, an ARN or an IP address that only exists after creation. Anything that uses it is also unknown for now.
  • # forces replacement: a note the provider attaches to an argument that cannot change without a rebuild. Seeing it next to something that holds data, such as a disk, a database or a bucket, is the moment to stop and ask whether you mean it.

The last line sums it up: Plan: N to add, N to change, N to destroy. A replacement counts as one add and one destroy. I built a tiny stand-in that compares a desired set against a recorded one and prints in the same style, to check the counting. It is not Terraform, and the resources are invented:

  -   aws_instance.old
  -/+ aws_instance.web
        ami = "ami-0old" -> "ami-0new"
        instance_type = "t3.micro" -> "t3.small"
  +   aws_instance.worker
      aws_s3_bucket.logs

Plan: 2 to add, 0 to change, 2 to destroy.

One create, one delete and one replace gave “2 to add, 2 to destroy”. If you only count lines starting with + and -, you will under-count.

A saved plan, from terraform plan -out=tfplan, can be re-read with terraform show tfplan. The CLI docs warn to treat the file as sensitive, because it includes variable values.

The graph behind it

How does Terraform decide order? Not from the order of your blocks. It builds a directed graph. Every resource, data source, variable and output is a node, and each reference or depends_on is an edge pointing at the thing it needs.

I wrote a small graph builder as a stand-in. It reads a configuration, finds references with a pattern match and groups the result into waves. This is my own code, run on a made-up VPC setup, and it simplifies how Terraform really schedules:

dependencies (implicit from references, explicit from depends_on):
 aws_vpc.main                 implicit=[] explicit=[]
 aws_subnet.a                 implicit=['aws_vpc.main'] explicit=[]
 aws_subnet.b                 implicit=['aws_vpc.main'] explicit=[]
 aws_internet_gateway.gw      implicit=['aws_vpc.main'] explicit=[]
 aws_security_group.web       implicit=['aws_vpc.main'] explicit=[]
 aws_instance.web             implicit=['aws_security_group.web', 'aws_subnet.a'] explicit=['aws_internet_gateway.gw']

walk order, each wave runs in parallel:
 wave 0 ['aws_s3_bucket.logs', 'aws_vpc.main']
 wave 1 ['aws_internet_gateway.gw', 'aws_security_group.web', 'aws_subnet.a', 'aws_subnet.b']
 wave 2 ['aws_instance.web']

Four things follow, and the first three match how Terraform behaves:

  • Order comes from references. aws_subnet.a mentions aws_vpc.main.id, so it waits for the VPC.
  • Unrelated branches run together. The logs bucket needs nothing, so it starts at once. Both subnets, the gateway and the security group have only the VPC in common, so they go in parallel.
  • Destroy reverses the order. The instance is deleted first and the VPC last, because you cannot delete a network with things still inside it.
  • My waves are a simplification. Terraform does not run strict waves. It starts a node as soon as its dependencies finish, up to a limit, ten at a time by default via -parallelism.

To see the real thing, terraform graph prints the graph in DOT format, which Graphviz tools can turn into a picture. My stand-in’s DOT looked like this, with the explicit edge dashed:

digraph {
  "aws_subnet.a" -> "aws_vpc.main";
  "aws_instance.web" -> "aws_internet_gateway.gw" [style=dashed];
  "aws_instance.web" -> "aws_security_group.web";
}

The real terraform graph output contains more nodes, including providers and variables. Dependencies, implicit and explicit covers when the second kind of edge is needed.

A reading routine

I read a plan in this order:

  1. The summary line, to see the scale. A 40-resource plan for a “small change” deserves questions.
  2. Every - and -/+, top to bottom.
  3. Each forces replacement annotation, and what is stored on that resource.
  4. Anything (known after apply) that feeds into something I care about, since I cannot review a value that does not exist yet.

Only then do I look at the ~ lines. Most of them are tags and descriptions.

About the video

The embed is the terraform plan chapter (10:28 to 14:59) of Matthew Sanabria’s “All Terraform Commands - A Practical Approach”, a 1 hour 22 minute walkthrough published in March 2021. It covers the plan command and its flags. It is the oldest video in this group, so output formatting has since changed in small ways. I judged it from the title, description and chapter list. The graph chapter (36:37 to 40:04) is linked in deeper, since only one clip can be embedded. English was judged from title, channel and English auto-captions.

Where this fits

You can read what Terraform intends. When a run fails or behaves strangely, you need to see more of what it is doing: debugging with TF_LOG and reading errors.

Check yourself

  1. 01A plan ends with `Plan: 2 to add, 0 to change, 2 to destroy.` and lists one new and one deleted resource. What else must be in it?

    A replacement (`-/+`), since each replacement counts as one add and one destroy. The new and deleted ones account for one of each, and the replacement for the other.

  2. 02Why would a resource attribute show `(known after apply)`?

    The provider only learns it when it creates the object, such as an ID or a generated address. Terraform cannot print a value that does not exist yet.

  3. 03Two subnets both reference the same VPC and nothing else. In what order does Terraform create them?

    The VPC first, then both subnets at the same time. There is no edge between the subnets, so the graph lets them run in parallel.

  4. 04Where in a plan should you look before approving it?

    For `-` and `-/+` lines on anything that holds data, and for `forces replacement` annotations. A one-line change in code can become a destroy-and-recreate.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark