[November 20 Webinar] Detecting & Correcting Infrastructure Drift

➡️ Register Now

Terraform

Terraform Refresh Command – Example Usage

Terraform refresh command

In this short article, we will take a look at the  command.

We will cover:

  1. What is the terraform refresh command?
  2. What to use instead of terrafrom refresh?
  3. Example usage

Warning: This command is deprecated because its default behavior is unsafe if you have misconfigured credentials for any of your providers.

What is the Terraform Refresh Command?

By running  after making manual changes to your infrastructure resources, you ensure that your Terraform state file is up-to-date and reflects the current state of your infrastructure resources. Terraform refresh will not alter any of your real infrastructure but will alter the Terraform state file.

Because the command is included in plan and apply, it is typically not needed to be run separately. In fact, Hashicorp recommends you do not use refresh, unless for backward compatibility reasons, for use with Terraform versions prior to v0.15.4.

When creating a terraform plan or running terraform apply, a refresh of the state of existing objects is automatically performed by Terraform (which is the default behavior).

The terraform refresh command is designed to reconcile any differences from the drift between your infrastructure and the information contained in the state file. It will only take into account objects already in the state file. Any objects outside of the state will not be considered and will need to be imported into the state to become managed by Terraform.

What to Use Instead of Terrafrom Refresh?

For Terraform versions v0.15.4 and above, the  option was introduced to the  and  commands.

The  command is essentially an alias to the  in v0.15.4 and above.

Excluding the  option will prompt you for confirmation before the refresh is performed.

You can also check out our Terraform CLI Cheat Sheet.

Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.

Example Usage

To further explain how  works, first create a resource using Terraform, such as a VNET with some subnets.

Next, modify the resource manually through the portal. For example, you could change the address space of one of your subnets.

Run command to reconcile the state of your VNET resources with their current state in the remote backend. This will update your local Terraform state file with the current state of the resource in the remote backend.

Next time you run Terraform will detect the changes you made manually (outside of Terraform) and generate an execution plan that reflects the current state of your infrastructure resources.

Key Points

By running  after making manual changes to your infrastructure resources, you ensure that your Terraform state file is up-to-date and reflects the current state of your infrastructure resources. This helps to prevent any conflicts or inconsistencies that might occur when Terraform is used to manage the same resources as other tools or manual changes.

It is not usually necessary to run  independently.

We encourage you also to explore how Spacelift makes it easy to work with Terraform. If you need any help managing your Terraform infrastructure, building more complex workflows based on Terraform, and managing AWS credentials per run, instead of using a static pair on your local machine, Spacelift is a fantastic tool for this. It supports Git workflows, policy as code, programmatic configuration, context sharing, drift detection, and many more great features right out of the box. You can check it for free, by creating a trial account.

Terraform Management Made Easy

Spacelift effectively manages Terraform state, more complex workflows, supports policy as code, programmatic configuration, context sharing, drift detection, resource visualization and includes many more features.

Start free trial

The Practitioner’s Guide to Scaling Infrastructure as Code

Transform your IaC management to scale

securely, efficiently, and productively

into the future.

ebook global banner
Share your data and download the guide