0
0
Terraformcloud~5 mins

Terraform plan for preview - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to see what changes Terraform will make to your cloud resources before actually applying them, you use the terraform plan command. This helps avoid surprises by previewing the changes safely.
Before creating new cloud resources to check what will be added.
Before updating existing infrastructure to see what will change.
Before deleting resources to confirm what will be removed.
When collaborating with a team to review planned changes.
To verify your Terraform code logic without making real changes.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

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

resource "aws_s3_bucket" "example_bucket" {
  bucket = "example-terraform-bucket-12345"
  acl    = "private"
}

This file tells Terraform to use AWS as the cloud provider in the us-east-1 region. It defines one resource: an S3 bucket named "example-terraform-bucket-12345" with private access. The terraform block sets the required Terraform and provider versions.

Commands
This command sets up Terraform in the current folder by downloading the AWS provider plugin and preparing the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.50.0... - Installed hashicorp/aws v4.50.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command previews the changes Terraform will make to your cloud resources based on your configuration, without applying them.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... 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_s3_bucket.example_bucket will be created + resource "aws_s3_bucket" "example_bucket" { + acl = "private" + bucket = "example-terraform-bucket-12345" + force_destroy = false + id = (known after apply) + region = (known after apply) + 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 that exactly these actions will be performed if "terraform apply" is subsequently run.
-out=planfile - Save the plan to a file for later apply
This command creates a plan and saves it to a file named preview.plan. You can apply this exact plan later to ensure the changes match the preview.
Terminal
terraform plan -out=preview.plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and saved to preview.plan. To perform exactly these actions, run the following command to apply: terraform apply "preview.plan"
-out=preview.plan - Save the plan to a file for exact apply later
Key Concept

If you remember nothing else from this pattern, remember: terraform plan shows you exactly what changes will happen before you make them.

Common Mistakes
Running terraform apply without running terraform plan first
You might make unexpected changes or delete resources without knowing.
Always run terraform plan to preview changes before applying.
Not running terraform init before terraform plan
Terraform won't have the necessary provider plugins and will fail.
Run terraform init once per project folder before planning or applying.
Ignoring the plan output and applying blindly
You might miss warnings or unintended resource changes.
Carefully read the plan output to confirm changes are expected.
Summary
terraform init prepares Terraform and downloads provider plugins.
terraform plan previews the changes Terraform will make without applying them.
terraform plan -out=filename saves the plan to apply exactly later.