Create a Terraform plan
The core Terraform workflow consists of three main steps after you have written your Terraform configuration:
- Initialize prepares your workspace so Terraform can apply your configuration.
- Plan allows you to preview the changes Terraform will make before you apply them.
- Apply makes the changes defined by your plan to create, update, or destroy resources.
When you provision infrastructure, Terraform creates an execution plan before it
applies any changes. Terraform creates the plan by comparing your Terraform
configuration to the state of your infrastructure. The execution plan consists
of a set of changes that create, update, or destroy resources. You can use the
terraform plan
command to compare your configuration to your resource's state,
review changes before you apply them, or to refresh your workspace's state.
Terraform plan supports automation workflows in CI/CD pipelines by guaranteeing
that the infrastructure changes Terraform applies match the ones you or your
team approve, even if the deploy process completes across different machines or
at different times.
In this tutorial, you will review how Terraform generates an execution plan,
what the plan contains, and the role of the terraform plan
command in your
Terraform workflow. To do so, you will create and apply a saved Terraform plan,
review its contents, and analyze how a plan reflects changes to your
configuration. You will also learn how to target specific resources when you
create a Terraform plan.
Prerequisites
You can complete this tutorial using the same workflow with either Terraform Community Edition or HCP Terraform. HCP Terraform is a platform that you can use to manage and execute your Terraform projects. It includes features like remote state and execution, structured plan output, workspace resource summaries, and more.
Select the HCP Terraform tab to complete this tutorial using HCP Terraform.
This tutorial assumes that you are familiar with the Terraform workflow. If you are new to Terraform, complete the Get Started tutorials first.
In order to complete this tutorial, you will need the following:
- Terraform v1.6+ installed locally.
- An AWS account with local credentials configured for use with Terraform.
- The jq command line utility.
Clone the example repository
In your terminal, clone the learn-terraform-plan
repository.
Navigate to the cloned repository.
Review configuration
The example configuration in this repository creates an EC2 instance through
resources and local and public modules. The modules/aws-ec2-instance
subdirectory contains the local module used to create the instance.
Terraform uses the provider versions specified in the terraform.tf
file.
Open the top-level main.tf
file. This configuration uses the aws
provider to
create an EC2 instance using an Ubuntu AMI.
This configuration uses the random_pet
resource to generate a name for your
instance. The module.ec2-instance
block uses the local aws-ec2-instance
module to define your instance.
The configuration also passes the random pet name to the hello
module, which
will generate outputs with the random pet name.
Initialize your configuration
In order to generate your execution plan, Terraform needs to install the providers and modules referenced by your configuration. Then, it will reference them to create your plan.
Initialize the Terraform configuration with terraform init
.
Create a plan
There are three commands that tell Terraform to generate an execution plan:
The
terraform plan
command creates a plan consisting of a set of changes that will make your resources match your configuration. This lets you preview the actions Terraform would take to modify your infrastructure before applying them. Terraform plan does not make any changes to your resources, you must apply a plan for Terraform to make changes.You can also save a plan with the
-out
flag. Later, you can apply the saved plan, and Terraform will only perform the changes listed in the plan. In an automated Terraform pipeline, applying a saved plan file ensures that Terraform only makes the changes you expect, even if your pipeline runs across multiple machines at different times.The
terraform apply
command applies a Terraform plan. If you do not pass a saved plan, then Terraform will a create a plan and prompt you for approval before applying the plan.The
terraform destroy
command creates an execution plan to delete all of the resources managed by your workspace.
Generate a saved plan with the -out
flag. You will review and apply this plan
later in this tutorial.
Terraform created a plan and saved it in the tfplan
file.
Since you have not yet applied this configuration, Terraform plans to create all of the resources defined in it.
When you create a plan, Terraform checks your workspace for an existing state file. Since you have not yet applied this configuration, your workspace's state is empty, and Terraform plans to create all of the resources defined in your configuration.
You can apply the saved plan file to execute these changes, but the contents of
the plan are not in a human-readable format. Use the terraform show
command to
print out the saved plan.
This is the same output that Terraform printed when you created your saved plan. You can review this plan output with your team before you apply the saved plan to ensure that the changes are the ones you expect.
Terraform can also report the contents of the saved plan as JSON. This is often useful when using Terraform in automated pipelines, as you can use code to inspect the plan.
Convert the saved plan into JSON, pass it to jq
to format it, and save the
output into a new file.
Warning
Terraform plan files can contain sensitive data. Never commit a plan file to version control, whether as a binary or in JSON format.
Review the plan
In this section, you will review the data Terraform captures about your
resources in a plan file. Use the jq
command to query the JSON formatted
version of your plan.
Terraform records the version of Terraform used to generate the plan, and the version of the plan file format. This will ensure that you use the same version to apply these changes when you use the saved plan.
Review plan configuration
The .configuration
JSON object is a snapshot of your configuration at the time of
the terraform plan
.
This configuration snapshot captures the versions of the providers recorded in
your .terraform.lock.hcl
file, ensuring that you use the same provider
versions that generated the plan to apply it. Note that the configuration
accounts for both the provider version used by the root module and child
modules.
The configuration
section further organizes your resources defined in your top
level root_module
.
The module_calls
section contains the details of the modules used, their input
variables and outputs, and the resources to create.
The configuration
object also records any references to other resources in a
resource's written configuration, which helps Terraform determine the correct
order of operations when it applies your plan.
Review planned resource changes
Review the planned resources changes to the aws_instance
resource from the
ec2-instance
local module.
The representation includes:
- The
action
field captures the action taken for this resource, in this casecreate
. - The
before
field captures the resource state prior to the run. In this case, the value isnull
because the resource does not yet exist. - The
after
field captures the state to define for the resource. - The
after_unknown
field captures the list of values that will be computed or determined through the operation and sets them totrue
. - The
before_sensitive
andafter_sensitive
fields capture a list of any values markedsensitive
. Terraform will use these lists to determine which output values to redact when you apply your configuration.
Review planned values
The planned_values
object is a report of the differences between the "before"
and "after" values of your resources, showing you the planned outcome for a run
that would use this plan file.
In this example, the module.ec2-instance.aws_instance
resource includes the
address that you will use to reference the resource in your Terraform
configuration, the provider name, and the values of all of the attributes as one
object. This format resolves the differences between the prior and expected
state in one object to demonstrate the planned outcomes for the configuration,
which is easier to use for any downstream consumers of the plan data. For
example, the Terraform Sentinel CLI tests policies against the planned outcomes
recorded here. The cost estimation feature in HCP Terraform also relies on the
planned_values
data to determine changes to your infrastructure spend.
Apply a saved plan
In your terminal, apply your saved plan.
Note
When you apply a saved plan file, Terraform will not prompt you for approval and instead immediately execute the changes. This workflow is primarily used in automation.
Terraform applied your changes according to the saved plan.
Modify configuration
Input variables let you easily update configuration values without having to edit your configuration files.
Open the variables.tf
file in the top-level configuration directory. Add the
configuration below to define a new input variable to use for the hello
module.
Then, create a terraform.tfvars
file, and set the new secret_key
input
variable value.
Warning
Never commit .tfvars
files to version control.
Finally, update the hello
module configuration in main.tf
to reference the
new input variable.
Create a new plan
Create a new Terraform plan and save it as tfplan-input-var
.
Convert the new plan file into a machine-readable JSON format.
Review new plan
When you created this plan, Terraform determined that the working directory already contains a state file, and used that state to plan the resource changes.
Since Terraform created this plan with existing resources and using input variables, your plan file has some new fields.
Review plan input variables
Now that you have defined input variables, Terraform captures them in the plan file as well.
Warning
Although you marked the input variable as sensitive
, Terraform still stores
the value in plaintext in the plan file. Since Terraform plan files can
contain sensitive information, you should keep them secure and never commit
them to version control.
Unlike input variables, Terraform does not record the values of any environment variables used for your configuration in your plan files. Using environment variables is one of the recommended ways to pass sensitive values, such as provider credentials, to Terraform.
Review plan prior_state
When you created this plan, Terraform determined that the working directory
already contains a state file, and used that state to plan the resource changes.
Unlike the first run's plan file, this file now contains a prior_state
object,
which captures the state file exactly as it was prior to the plan action.
Review plan resource changes
Now that your state file tracks resources, Terraform will take the existing
state into consideration when it creates an execution plan. For example, the
module.hello.random_pet.server
object now contains data in both the before
and after
fields, representing the prior and desired configurations
respectively.
Notice that the actions
list is now set to ["delete","create"]
and that the
action_reason
is "replace_because_cannot_update"
- the change to the
secret_key
for the resource is destructive, so Terraform must both delete and
create this resource. Terraform determines whether it can update a resource in
place or must recreate it based on which provider attributes you changed.
Once you have created resources in the working directory, Terraform uses the prior state, the data returned by a refresh operation, and the written configuration to determine the changes to make. Terraform supports additional flags that you can use to modify how it constructs an execution plan. For example, you can create a plan that only refreshes the state file without modifying resource configuration, or target only specific resources for either update or replacement.
Clean up infrastructure
Now that you have completed this tutorial, destroy the resources created before moving on. Create and apply a destroy plan.
When you use the -destroy
flag, Terraform creates a plan to destroy all of the
resources in the configuration. Apply the plan to destroy your resources.
The terraform destroy
command is a shortcut that creates a destroy plan and
then waits for you to approve it. Saving a destroy plan allows you to review it
before applying, just like a regular saved plan.
Next steps
In this tutorial, you reviewed how Terraform constructs an execution plan and
uses saved plans. You also explored the relationship of the terraform plan
and
terraform apply
commands.
Check out the following tutorials to try out some of the available plan modes and options:
Learn how to use a refresh-only plan to update your state file with the actual configuration of your resources.
Learn how to use the
-replace
flag to recreate resources.Learn more about using resource targeting to scope the resources affected by Terraform operations.
Review the JSON output format documentation for more detail on the contents and format of a plan file.