Skip to main content

Planning Infrastructure Changes

1. Creating Execution Plan

The terraform plan command generates an execution plan that shows what changes Terraform will make to your infrastructure. This is a critical step in the Terraform workflow, allowing you to review changes before applying them.

Command Description Notes
terraform plan Generates and displays the execution plan Default behavior, shows what will be changed
terraform plan -out=plan.out Creates a plan file for later use Use with terraform apply
terraform plan -refresh=false Skips state refresh before planning Can speed up plan generation but may be inaccurate

Example: Creating a basic plan

terraform plan

2. Saving Plan Files

Plan files can be saved to disk using the -out flag. These files can later be applied without re-generating the plan, which is useful for automation and ensuring consistency across environments.

Flag Description Usage
-out=<file> Save plan to a file terraform plan -out=infrastructure.tfplan
-var-file=<file> Specify variable files terraform plan -var-file=prod.tfvars

Example: Saving a plan file

terraform plan -out=production.tfplan

3. Reviewing Plan Output

The plan output provides a detailed view of the changes Terraform will make to your infrastructure. It includes resource creation, modification, and destruction actions.

Change Type Description Symbol
Create New resource will be created + create
Update Resource will be updated in place ~ update in-place
Destroy Resource will be destroyed - destroy
Replace Resource will be destroyed and recreated ~ replace

Example: Plan output format

<span class="highlight">+</span> aws_instance.example
<span class="highlight">~</span> aws_security_group.example
<span class="highlight">-</span> aws_elb.example

4. Using Target Resources

The -target flag allows you to limit the scope of the plan to specific resources. This is useful when working with large configurations and you only want to review changes for a subset of resources.

Flag Description Example
-target=<resource> Limit plan to specific resource terraform plan -target=aws_instance.web
-target=<resource> Can be used multiple times terraform plan -target=aws_instance.web -target=aws_security_group.web

Example: Targeting specific resources

terraform plan -target=aws_instance.web_server

5. Refreshing State

The -refresh flag controls whether Terraform should refresh the state before generating the plan. By default, it's enabled to ensure the state reflects the current infrastructure.

Flag Description Default
-refresh=true Refresh state before planning (default) Yes
-refresh=false Do not refresh state before planning No

Example: Skipping refresh for faster plan generation

terraform plan -refresh=false

6. Generating Destroy Plan

To generate a plan for destroying infrastructure, use the -destroy flag. This creates a plan that will remove all resources in the configuration.

Flag Description Usage
-destroy Generate plan for destroying infrastructure terraform plan -destroy
-out=<file> Save destroy plan to file terraform plan -destroy -out=destroy.tfplan

Example: Generating a destroy plan

terraform plan -destroy

7. Disabling Refresh

The -refresh=false option disables state refresh, which can speed up plan generation but may result in an inaccurate plan if the actual infrastructure has changed since the last refresh.

Warning: Disabling refresh can lead to outdated plans if infrastructure has changed externally.
Flag Description Impact
-refresh=false Skips state refresh Faster plan, potentially inaccurate

8. Setting Variable Values

Variables can be set directly on the command line using the -var flag. This is useful for testing different values without modifying configuration files.

Flag Description Example
-var=<name=value> Set variable value terraform plan -var=instance_type=t3.micro
-var=<name=value> Can be used multiple times terraform plan -var=environment=prod -var=region=us-west-2

Example: Setting variables from command line

terraform plan -var=instance_type=t3.micro -var=environment=prod

9. Using Variable Files

Variable values can be loaded from files using the -var-file flag. This is a common pattern for managing environment-specific configurations.

Flag Description Usage
-var-file=<file> Load variables from file terraform plan -var-file=prod.tfvars
-var-file=<file> Can be used multiple times terraform plan -var-file=common.tfvars -var-file=prod.tfvars

Example: Using variable files

terraform plan -var-file=prod.tfvars

10. Generating Machine-Readable Output

Terraform can generate machine-readable output in JSON format using the -json flag. This is useful for integrating Terraform into CI/CD pipelines or other automation tools.

Flag Description Output Format
-json Generate JSON output terraform plan -json
-out=<file> Save JSON to file terraform plan -json -out=plan.json

Example: Generating JSON plan output

terraform plan -json

Was this article helpful?