Use refresh-only mode to sync Terraform state
Terraform relies on the contents of your workspace's state file to generate an execution plan to make changes to your resources. To ensure the accuracy of the proposed changes, your state file must be up to date.
In Terraform, refreshing your state file updates Terraform's knowledge of your
infrastructure, as represented in your state file, with the actual state of your
infrastructure. Terraform plan
and apply
operations run an implicit
in-memory refresh as part of their functionality, reconciling any drift from
your state file before suggesting infrastructure changes. You can also update
your state file without making modifications to your infrastructure using the
-refresh-only
flag for plan
and apply
operations.
In this tutorial, you will safely refresh your Terraform state file using the
-refresh-only
flag. You will also review Terraform's implicit refresh
behavior and the advantages of the -refresh-only
flag over the deprecated
terraform refresh
subcommand.
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 Get Started tutorials first.
In order to complete this tutorial, you will need the following:
- Terraform v1.1+ installed locally.
- An AWS account with local credentials configured for use with Terraform.
Note
Some of the infrastructure in this tutorial may not qualify for the AWS free tier. Destroy the infrastructure at the end of the guide to avoid unnecessary charges. We are not responsible for any charges that you incur.
Clone example repository
Clone the sample repository for this tutorial.
Change into the repository directory.
Review configuration
Open main.tf
to review the sample configuration. It defines an EC2 instance
and a data source to identify the latest Amazon Linux AMI. The provider block
references the region
input variable, which defaults to us-east-2
.
Create infrastructure
Initialize this configuration.
Apply your configuration. Respond yes
to the prompt to confirm the operation.
Run a refresh-only plan
A common error scenario that can prompt Terraform to refresh the contents of your state file is mistakenly modifying your credentials or provider configuration. Simulate this situation by updating your AWS provider's region. You will then review the proposed changes to your state file from a Terraform refresh.
Create a terraform.tfvars
file in your learn-terraform-refresh
directory.
Open the file, and paste in the following configuration to override the default
region variable.
Since you pass the region
variable to your AWS provider configuration in
main.tf
, this will reconfigure your provider for the us-west-2
region. The
resources you created earlier are still in us-east-2
.
Run terraform plan -refresh-only
to review how Terraform would update your state file.
Because you updated your provider for the us-west-2
region, Terraform tries
to locate the EC2 instance with the instance ID tracked in your state file but
fails to locate it since it's in a different region. Terraform assumes that you
destroyed the instance and wants to remove it from your state file.
If the modifications to your state file proposed by a -refresh-only
plan were
acceptable, you could run a terraform apply -refresh-only
and approve the
operation to overwrite your state file without modifying your infrastructure.
However, in this tutorial, refreshing your state file would drop your
resources, so do not run the apply operation.
Review Terraform's refresh functionality
In previous versions of Terraform, the only way to refresh your state file was
by using the terraform refresh
subcommand. However, this was less safe than
the -refresh-only
plan and apply mode since it would automatically overwrite
your state file without giving you the option to review the modifications
first. In this case, that would mean automatically dropping all of your
resources from your state file.
The -refresh-only
mode for terraform plan
and terraform apply
operations
makes it safer to check Terraform state against real infrastructure by letting
you review proposed changes to the state file. It lets you avoid mistakenly
removing an existing resource from state and gives you a chance to correct your
configuration.
A refresh-only apply
operation also updates outputs, if necessary. If you have
any other workspaces that use the terraform_remote_state
data source to
access the outputs of the current workspace, the -refresh-only
mode allows you
to anticipate the downstream effects.
In order to propose accurate changes to your infrastructure, Terraform first
attempts to reconcile the resources tracked in your state file with your actual
infrastructure. Terraform plan
and apply
operations first run an in-memory
refresh to determine which changes to propose to your infrastructure. Once you
confirm a terraform apply
, Terraform will update your infrastructure and
state file.
Though Terraform will continue to support the refresh
subcommand in future
versions, it is deprecated, and we encourage you to use the -refresh-only
flag instead. This allows you to review any updates to your state file. Unlike
the refresh
subcommand, -refresh-only
mode is supported in workspaces using
HCP Terraform as a remote backend, allowing your team to collaboratively
review any modifications.
Clean up resources
Now that you have reviewed the behavior of the -refresh-only
flag, you will
destroy the EC2 instance you provisioned.
First, remove your terraform.tfvars
file to use default value for the
region
variable.
Now run terraform destroy
to destroy your infrastructure. Respond yes
to
the prompt to confirm the operation.
If you used HCP Terraform for this tutorial, after destroying your resources,
delete the learn-terraform-refresh
workspace from your HCP Terraform organization.
Next steps
You used Terraform's -refresh-only
mode to safely compare your infrastructure
and state file. You also reviewed the implicit refresh behavior in standard
Terraform operations.
To learn more about managing state and drift, complete the following tutorials:
- Learn about how HCP Terraform's drift detection feature can help you make sure that your real resources match the Terraform state that tracks them.
- Manage Resource Drift
- Manage Resources in State
- Troubleshoot common issues with Terraform