Terraform
Infrastructure as Code
DevOps
Terraform Refresh
Cloud Automation

What does terraform refresh really do?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Terraform, an open-source tool developed by HashiCorp, is designed for building, changing, and versioning infrastructure safely and efficiently. One of the commands central to Terraform's operations is terraform refresh . This article delves into the intricacies of what terraform refresh accomplishes, providing you with a technical understanding of its role.

What is terraform refresh

?

The terraform refresh command is responsible for reconciling Terraform's state with the real-world resources managed by your configurations. This command reads the current settings of each resource in your Terraform state and updates the state to reflect reality. Although often implicit in other operations like apply or plan , calling refresh explicitly can occasionally be useful.

Key Purpose

  • Sync State and Reality: terraform refresh updates the Terraform state file to accurately represent the current state of the infrastructure. It pulls the latest data from the API of each provider, ensuring the state file mirrors actual resource attributes.
  • Identify Drift: By updating the state with real-world data, refresh can reveal any divergence from your configuration, often referred to as "drift." This helps maintain consistency.

Technical Details

When you execute terraform refresh , the following occurs:

  1. Resource Querying: Terraform reaches out to the configured cloud provider APIs (like AWS, Azure, GCP, etc.) to request the current state and configuration of resources.
  2. State Update: It captures these parameters from the cloud provider's API and updates the Terraform state file (terraform.tfstate ) with the real-world resource state values.
  3. Logging Changes: Any differences between the stored state and the actual resource configurations are logged, providing insights into changes that have occurred outside of Terraform.

Example Workflow

Consider a scenario where your configuration defines an AWS EC2 instance. If someone modifies the instance directly through the AWS Management Console (changing its size, for example), running terraform refresh would update the state file to reflect this change. You would then see the updated attributes when executing subsequent terraform plan or terraform apply .

  • Before Planning Changes: Running a refresh before terraform plan can give you a clear picture of the current state of resources, preventing surprises.
  • After Manual Changes: Whenever manual changes are made to resources outside of Terraform, performing a refresh helps align the Terraform state with real-world configurations.
  • Detection of Unintended Changes: Helps in auditing any changes made directly by users or other systems that might lead to configuration drift.
  • State Consistency: Ensures that the Terraform state file used during operations reflects the actual deployed infrastructure.
  • Before Refresh: State reflects acl = "private" .
  • After Refresh: State reflects acl = "public-read" .

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.