Terraform Plan -Out File

I always seem to forget the Terraform Plan out command!


Did you know you can save your plan to a file that your favorite text editor can read? If you try the default command in the man pages, the formatting is all over the place because of Terraform’s colors.

hashicorp-terraform-banner

Outputting your planning file is useful for writing large Terraform files. I find ensuring all my naming conventions look good before applying the code is particularly useful.

Before we start, ensure you have run terraform init before you run terraform plan.

Bash
terraform plan > myplan.txt

The formatting is all messed up

See below.

HCL
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create
 <= read (data resources)

Terraform will perform the following actions:

  # module.mymodule.data.aws_iam_policy_document.nodes will be read during apply
  # (config refers to values not yet known)
 <= data "aws_iam_policy_document" "nodes" {
      + id   = (known after apply)
      + json = (known after apply)

      + statement {
          + actions   = [
              + "autoscaling:DescribeScalingPlanResources",
              + "autoscaling:DescribeScalingPlans",
              + "ec2:AttachNetworkInterface",
              + "ec2:AttachVolume",
              + "ec2:CreateNetworkInterface",
              + "ec2:CreateSnapshot",
              + "ec2:CreateTags",
              + "ec2:DeleteTags",
              + "ec2:DescribeInstances",
              + "ec2:DescribeNetworkInterfaces",
              + "ec2:DescribeSecurityGroups",

A quick workaround is to use the –no-colour option.

Bash
terraform plan -no-color > myplan.txt 

This fixes the formatting issue and makes everything easier to read.

HCL
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create
 <= read (data resources)

Terraform will perform the following actions:

  # module.mymodule.data.aws_iam_policy_document.nodes will be read during apply
  # (config refers to values not yet known)
 <= data "aws_iam_policy_document" "nodes" {
      + id   = (known after apply)
      + json = (known after apply)

      + statement {
          + actions   = [
              + "autoscaling:DescribeScalingPlanResources",
              + "autoscaling:DescribeScalingPlans",
              + "ec2:AttachNetworkInterface",
              + "ec2:AttachVolume",
              + "ec2:CreateNetworkInterface",
              + "ec2:CreateSnapshot",
              + "ec2:CreateTags",
              + "ec2:DeleteTags",
              + "ec2:DescribeInstances",
              + "ec2:DescribeNetworkInterfaces",
              + "ec2:DescribeSecurityGroups",

Storing the plan in a file has several advantages. First, it ensures consistency between the plan and apply phases, eliminating any discrepancies that might arise due to changes in the configuration or the state of the remote resources between the two steps. Second, it facilitates collaboration among team members. You can share the plan file for peer review or for approval processes in a CI/CD pipeline.

What Else Can You Do With Terraform Plan Output?

Relative Path for Output:

Bash
terraform plan -out=./plans/terraform_plan

Specifies a relative path to save the plan in the ‘plans’ directory.

Full Path for Output:

Bash
terraform plan -out=/path/to/terraform/plans/my_plan


Saves the Terraform plan to an absolute path.

Output in JSON Format:

Bash
terraform plan -out=tfplan.json -input=false -lock=false -no-color -detailed-exitcode


Generates the plan in JSON format without user input, locking, color, and with a detailed exit code.

Generate Plan Without Applying, and get a detailed exit code

Bash
terraform plan -detailed-exitcode

Performs a dry run of the Terraform plan without applying changes, providing a detailed exit code.

Plan with Specific Variable Values:

Bash
terraform plan -out=tfplan -var="region=us-east-1" -var="instance_type=t2.micro"

Creates a plan with specific variable values, in this case, setting the region to us-east-1 and instance type to t2.micro.

Terraform Plan Common Q&A


Q1: What is the primary function of the terraform plan command?

A: The terraform plan command is used to generate an execution plan, showing what changes will be made to the infrastructure without actually applying them. It serves as a dry run to visualize the impact of your Terraform configurations.


Q2: How does the -out flag enhances the terraform plan command?

A: The -out flag allows you to save the execution plan to a file. This guarantees that the plan generated precisely matches what will be applied later, eliminating any discrepancies that could arise from alterations in configuration or remote resources.


Q3: What is the format of the file generated by terraform plan -out=<filename>?

A: The file is stored in a binary format, which is not human-readable but is designed to be used by Terraform itself in the terraform apply phase.


Q4: Why is saving the plan to a file beneficial for team collaboration?

A: Saving the plan to a file enables sharing among team members for peer review or approval processes, and it can also be seamlessly integrated into CI/CD pipelines, ensuring that only changes that have been reviewed and approved are applied to the infrastructure.


Q5: Can you modify the plan file generated by terraform plan -out?

A: No, the plan file is in a binary format and is not meant to be manually edited. It is designed to be directly used by the terraform apply command.


Elsewhere On TurboGeek:  What is AWS Security?

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

5 Responses

  1. Gustavo Araujo says:

    Great! Very helpful, thanks!

  2. Chaitanya says:

    Thank You, this is really helpful

  1. 19/10/2022

    […] Terraform Plan Output to File […]

  2. 22/10/2022

    […] Learn out Terraform Plan -Out […]

  3. 19/01/2023

    […] Learn out Terraform Plan -Out […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate ยป