Lesson 1 of 3 Planning, Applying and Destroying Infrastructure

Execution Plans and the Resource Graph

terraform plan shows what Terraform will do before it does anything. This lesson explains how to read a plan, how Terraform orders work through the resource graph, and the options that shape a plan.

Learning outcomes

After completing this lesson, you will be able to:

  • Interpret the actions and symbols in an execution plan.
  • Explain how the resource graph orders and parallelises operations.
  • Describe saved plans and the main planning options.

How a plan is made

When you run terraform plan, Terraform reads the configuration and the latest state, refreshes the state by reading every managed object, and compares the result with the configuration. Every difference becomes a proposed action. Plan never changes real infrastructure or the stored state, so it is always safe to run.

Terraform will perform the following actions:

  # aws_instance.web must be replaced
-/+ resource "aws_instance" "web" {
      ~ ami           = "ami-0aaa1111" -> "ami-0bbb2222" # forces replacement
      ~ id            = "i-0abc123" -> (known after apply)
        instance_type = "t3.micro"
    }

  # aws_s3_bucket.logs will be created
  + resource "aws_s3_bucket" "logs" {
      + arn    = (known after apply)
      + bucket = "acme-logs-witty-fox"
    }

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

The symbols identify the action for each resource, as listed in the following table.

SymbolAction
+Create a new object
~Update the object in place
-Destroy the object
-/+Destroy, then create a replacement (default replacement order)
+/-Create the replacement first, then destroy (create_before_destroy)
<=Read a data source during apply

A replacement counts as one add and one destroy in the summary, which is why the example shows two adds. The comment # forces replacement identifies the argument whose change cannot be made in place. Values that the provider computes, such as IDs and ARNs, appear as (known after apply).

The following diagram shows a plan that forces replacement because of drift, and the safe responses.

Reading an unexpected replacement in a plan for a tag change. engine_version forces replacement because the real database was upgraded outside Terraform, for example by automatic minor upgrades, and the configuration still holds the older value. Replacing a database destroys its data, so do not apply: update the version or ignore the attribute, and plan again until only the tag changes.
The plan is abbreviated; version values are placeholders.
Match the pairs

Reading plan symbols

Exam skills 3.4.1

Match each plan symbol with the action it represents.

Log in to try this activity Interactive activities are free with an account.

The resource graph

Terraform builds a dependency graph from the configuration. Each resource, data source, provider configuration, variable and output is a node, and each reference or depends_on entry is an edge. Terraform walks the graph so that an object is created or updated after everything it depends on, and destroyed before everything it depends on.

Nodes with no path between them are independent and are processed in parallel. By default Terraform performs up to 10 operations concurrently; -parallelism=N on plan, apply or destroy changes the limit, which helps when a provider hits API rate limits. terraform graph prints the graph in DOT format, which tools such as Graphviz can draw. If references form a cycle, Terraform reports an error during planning.

10

Default number of concurrent operations

0 / 1 / 2

-detailed-exitcode results: no changes / error / changes

Fill in the number

Plan summaries and parallelism

Exam skills 3.4.1 · 3.4.2

Read plan summaries and Terraform's concurrency settings.

Log in to try this activity Interactive activities are free with an account.

Saved plans and planning options

terraform plan -out=tfplan saves the plan to a binary file. terraform apply tfplan later performs exactly those actions without asking again, which separates review from execution in pipelines. terraform show tfplan prints a saved plan, and terraform show -json tfplan produces JSON for policy tools. A plan file contains the configuration and values, which can include secrets, so protect it like state.

Plan has three modes: normal, -destroy, which plans to destroy everything, and -refresh-only, which only updates state to match reality. The following options are used most often.

OptionEffect
-var 'name=value' and -var-file=FILESet input variables
-target=ADDRESSPlan only the given resource and its dependencies; for exceptional situations
-replace=ADDRESSPlan to replace the given resource even if it has not changed
-refresh=falsePlan from cached state without reading real infrastructure
-detailed-exitcodeExit 0 for no changes, 1 for an error, 2 for changes
-out=FILESave the plan for a later apply

Avoid routine targeting

-target is not a routine way to work on part of a configuration: it can leave the rest out of step, and Terraform warns every time it is used. Split large configurations into separate root modules or workspaces instead.

Match the pairs

Planning options

Exam skills 3.4.3

Match each planning need with the option that meets it.

Log in to try this activity Interactive activities are free with an account.
Worked example

An unexpected replacement

A plan for a small tag change shows -/+ for a production database with # forces replacement next to engine_version. The engineer did not change the engine version. What should they do?

The decisive information is the forcing argument and the refresh step.

Refresh shows the real engine version, which may have been upgraded outside Terraform, for example by automatic minor version upgrades, so the configuration's older value now differs. The engineer must not apply: replacing a database destroys its data. They should update the configuration to the current version, or add the attribute to ignore_changes if the platform manages it, and plan again until only the tag change appears.

Check yourself

What exit code does terraform plan -detailed-exitcode return when changes are present?

Show answer

2. It returns 0 when there are no changes and 1 on an error.

Exam Tips

Remember for the exam

  • plan refreshes, compares and proposes; it changes nothing.
  • -/+ is a replacement; look for # forces replacement.
  • Default parallelism is 10; the graph orders work by dependencies.
  • plan -out saves a plan; protect plan files like state.