Terraform Associate: Create a Terraform Plan
A Detailed Guide to Generating and Analyzing Terraform Execution Plans for Safe Infrastructure Management
Table of contents
- What Does terraform plan Do?
- Steps to Create a Terraform Plan
- Conclusion
- Reference
In the world of Infrastructure as Code (IaC), Terraform is a game-changer. One of its key features is the ability to create a Terraform plan—a preview of the changes that will be applied to your infrastructure. This crucial step lets you review the proposed changes before committing them, ensuring that you avoid unexpected modifications or deletions.
In this guide, we’ll explain what a Terraform plan is, detail the steps to create one, and discuss some useful options to refine your planning process.
What Does terraform plan
Do?
When you run terraform plan
, Terraform:
Compares the current state of your infrastructure (stored in the state file) with the desired state defined in your configuration files.
Calculates the actions needed to reconcile any differences, such as creating, modifying, or destroying resources.
Displays these actions in a human-readable format, giving you a clear preview of what will change.
This preview is vital for ensuring that the changes align with your expectations before they are applied.
Steps to Create a Terraform Plan
1. Write Your Terraform Configuration Files
Begin by creating your Terraform configuration files (usually with a .tf
extension) that describe your desired infrastructure. For example, you might define an AWS EC2 instance in a file called main.tf
:
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
This configuration specifies that Terraform should use the AWS provider in the us-west-2
region to create an EC2 instance.
2. Initialize the Terraform Configuration
Before you can run a plan, you need to initialize your working directory. This step downloads the necessary provider plugins and sets up the backend if specified. Use the following command in your terminal:
terraform init
This command:
Downloads provider plugins (e.g., the AWS provider).
Configures the backend for state management.
Installs any modules referenced in your configuration.
3. Run terraform plan
With your configuration initialized, run the following command in the directory containing your main.tf
file:
terraform plan
Terraform will:
Compare your current state (from
terraform.tfstate
) with your configuration.Calculate the changes needed to achieve the desired state.
Output a detailed execution plan to the terminal.
Example Output
A sample output might look like this:
Refreshing Terraform state in-memory prior to plan...
The refresh will be performed in 1s...
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.example will be created
+ resource "aws_instance" "example" {
+ ami = "ami-0c55b159cbfafe1f0"
+ id = (known after apply)
+ instance_type = "t2.micro"
+ key_name = (known after apply)
+ security_groups = (known after apply)
+ subnet_id = (known after apply)
+ vpc_security_group_ids = (known after apply)
+ ...
}
Plan: 1 to add, 0 to change, 0 to destroy.
This plan indicates that Terraform will create one EC2 instance with the specified properties.
4. Review the Plan
Carefully review the output to ensure the changes are what you expect. Terraform clearly indicates:
Resources to be created (
+ create
)Resources to be modified (
~ update
)Resources to be destroyed (
- destroy
)
This step helps prevent accidental changes that could impact your production environment.
5. Save the Plan (Optional)
If you want to save the execution plan for later use or to share with your team, you can output the plan to a file:
terraform plan -out=tfplan
This command creates a binary plan file (tfplan
) that you can later apply using:
terraform apply tfplan
6. Create a Plan for a Specific Resource (Optional)
Sometimes you might want to limit the plan to a specific resource. Use the -target
flag to focus on a particular resource:
terraform plan -target=aws_instance.example
This command restricts the plan to just the aws_instance.example
resource, ignoring other resources defined in your configuration.
7. Modify and Re-run the Plan (Optional)
If you need to test changes, modify your configuration (e.g., change the AMI ID or instance type) and run terraform plan
again. This iterative process allows you to fine-tune your configuration and see the impact of your changes before applying them.
8. Verify Dependencies and Define Outputs (Optional)
Terraform automatically detects resource dependencies, ensuring that the order of actions is correct. Additionally, you can define output variables in your configuration to display useful information once the plan is applied. For example, add the following to your configuration to output the instance ID:
output "instance_id" {
value = aws_instance.example.id
}
This output helps verify that your resources are created as expected.
Conclusion
Creating a Terraform plan is a critical step in managing your infrastructure with Terraform. It allows you to:
Preview Changes: See exactly what Terraform will do—whether creating, modifying, or destroying resources.
Prevent Mistakes: Verify that your configuration changes align with your expectations before they are applied.
Save and Share Plans: Save the plan to a file for later execution or team collaboration.
By carefully reviewing the plan and understanding the changes, you can confidently manage your infrastructure while minimizing the risk of unintended modifications. With your Terraform plan in hand, you're one step closer to safely applying your changes and provisioning your infrastructure. Happy Terraforming!
Reference
Terraform Plan Command Documentation
Explore theterraform plan
command in detail, including how it compares the current state with your configuration and what changes it will make.Terraform Getting Started Guide
A beginner-friendly guide that covers creating configuration files, initializing your workspace, and planning infrastructure changes.Terraform State Management
Learn how Terraform manages state files, which is crucial for comparing your desired configuration against the live infrastructure.Terraform Output Variables
Understand how to define and use output variables to capture and verify important information from your Terraform-managed resources.Using the -target Flag in Terraform
Discover how to focus your plan on specific resources with the-target
flag, allowing for granular control over your infrastructure changes.