Using Plan Files in Terraform: A Practical Guide

Updated: February 4, 2024 By: Guest Contributor Post a comment

Introduction to Terraform Plan Files

Welcome to this comprehensive guide on utilizing plan files in Terraform. As a powerful tool for infrastructure as code (IaC), Terraform allows users to define and provision infrastructure through code. A crucial aspect of working with Terraform is the ability to create, manage, and deploy infrastructure predictably and efficiently through the use of plan files.

Terraform plan files serve as a blueprint for the changes Terraform intends to make to your infrastructure. This step is pivotal as it provides a preview of the modifications, additions, and deletions Terraform will execute, offering a chance for review and approval before applying these changes. The command terraform plan is used to create a plan file, and terraform apply to apply it.

Creating a Basic Plan

Let’s start with creating a basic plan file in Terraform. Assume you have a Terraform configuration for a basic AWS EC2 instance:

provider "aws" {
  region = "us-east-1"
}

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

Run the following command to generate a plan:

terraform plan -out=myplan

This will create a plan file named ‘myplan’. The output provides details on the actions Terraform plans to take.

Viewing Plan Contents

To view the details of the plan file ‘myplan’, use:

terraform show myplan

This command displays the actions captured in the plan file, allowing for a thorough review before proceeding to apply them.

Modifying and Updating Plans

It’s not uncommon to tweak your configuration after generating a initial plan. Let’s say you decided to change the instance type:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro" # Updated from t2.micro
}

After updating the configuration, you should generate a new plan to reflect these changes:

terraform plan -out=newplan

Again, reviewing the plan with terraform show newplan is critical before applying.

Advanced Plan Files Usage

As your infrastructure grows, so does the complexity of managing plan files. A more sophisticated approach includes selectively applying changes and sharing plans among teams.

Selectively Applying Changes

Terraform allows for selective application of resources through the -target parameter. Suppose you only want to update the previously created EC2 instance without affecting other resources:

terraform plan -out=selectedplan -target=aws_instance.example

This generates a plan that only includes changes to the specified target.

Sharing Plan Files

In a team setting, it’s beneficial to share plan files for collective review and approval. Plan files can be shared via version control systems or other collaboration tools. It’s critical, however, to ensure that the same Terraform version is used across all members to avoid discrepancies.

Version Control and Automation

Integrating Terraform with version control systems (VCS) elevates your infrastructure management to a new level. You can track changes, revert to previous states, and automate plan and apply processes through continuous integration and continuous deployment (CI/CD) pipelines. Incorporating plan files into this workflow ensures that every change is reviewed, recorded, and safely applied.

Example of CI/CD with Terraform:

For instance, a GitLab CI pipeline could automatically generate and apply Terraform plan files for staging environments upon each commit, while requiring manual approval for production environments:

# Sample GitLab CI configuration
stages:
  - plan
  - apply

plan_stage:
  stage: plan
  script:
    - terraform init
    - terraform plan -out=planfile
  artifacts:
    paths:
      - planfile
  only:
    - master

This setup ensures changes are thoroughly vetted before impacting your production environment.

Conclusion

Effectively using plan files in Terraform is paramount for infrastructure as code best practices. It not only aids in visualizing and understanding infrastructure changes before they are applied but also facilitates collaboration and version control in team environments. Embrace the use of plan files to make your Terraform projects more manageable, predictable, and secure.