Understanding terraform plan with a Simple Real-Life Example

Varun AkuthotaVarun Akuthota
4 min read

Introduction

Terraform is an essential tool for automating your infrastructure management. After initializing your setup with terraform init, the next step typically involves terraform plan. This command is crucial for understanding what changes Terraform will make to your infrastructure. In this article, we'll break down what terraform plan does using a simple, real-life analogy to make it easy to understand.

Real-Life Analogy: Planning a Home Renovation

Think of terraform plan as planning a home renovation before you start the actual work. When you want to renovate your home, you first create a detailed plan, listing all the changes you want to make and how they will impact your current home. Similarly, terraform plan gives you a detailed preview of the changes Terraform will make to your infrastructure.

What Does terraform plan Do?

When you run terraform plan, it performs several essential tasks to give you a clear picture of the proposed changes:

  1. Read the Configuration Files: Terraform reads your configuration files (.tf files) to understand what resources you want to create, update, or delete.

  2. Compare with Current State: It compares the desired state (defined in your configuration files) with the current state of your infrastructure (stored in the state file).

  3. Generate an Execution Plan: Terraform generates an execution plan, detailing the actions it will take to achieve the desired state. This includes creating, updating, or deleting resources.

  4. Highlight Changes: The plan highlights any changes, additions, or deletions that will occur if you apply the plan.

Step-by-Step Real-Life Example

Let's continue with our example of setting up a simple web server on AWS using Terraform. Here's how terraform plan fits into the process:

1. Ensure Your Configuration is Ready

Make sure you have your main.tf file ready, defining your AWS web server.

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

2. Run terraform plan

Open your terminal, navigate to the directory containing main.tf, and run:

terraform plan

3. What Happens During terraform plan?

Read the Configuration Files: Terraform reads the main.tf file to understand the desired state of your infrastructure.

Compare with Current State: It compares this desired state with the current state stored in the state file. If this is the first time running, the current state is empty.

Generate an Execution Plan: Terraform generates a plan, detailing what resources will be created, updated, or deleted.

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_instance.web will be created
  + resource "aws_instance" "web" {
      + ami                          = "ami-0c55b159cbfafe1f0"
      + instance_type                = "t2.micro"
      + tags                         = (known after apply)
    }

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

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee to take exactly these actions if you run "terraform apply"
without passing it an updated plan file.

Highlight Changes: The plan shows that a new AWS instance (aws_instance.web) will be created.

4. Review the Plan

Review the plan to ensure it matches your expectations. This is a critical step to avoid unintended changes.

Summary

  • Read Configuration Files: terraform plan reads your Terraform configuration to understand the desired state.

  • Compare with Current State: It compares this desired state with the current state of your infrastructure.

  • Generate an Execution Plan: It generates a plan detailing the actions Terraform will take.

  • Highlight Changes: The plan highlights any additions, changes, or deletions.

By running terraform plan, you get a detailed preview of the changes Terraform will make, allowing you to review and confirm the changes before applying them.

Conclusion

Understanding terraform plan is crucial for managing your infrastructure effectively with Terraform. With this simple analogy and step-by-step example, you can confidently plan your infrastructure changes and ensure they align with your expectations before applying them.

0
Subscribe to my newsletter

Read articles from Varun Akuthota directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Varun Akuthota
Varun Akuthota