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.
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 refreshupdates 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,
refreshcan 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:
- 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.
- 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. - 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
refreshbeforeterraform plancan 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
refreshhelps 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
- What does that code snippet signify tf.logging.set_verbositytf.logging.INFO in tensorflow code?
- What events should go through the RAFT log
- What happens when I reboot an EC2 instance?
- What happens when using higher version tf serving to serve a model from lower version tensorflow?
- What is a Content Delivery Network and Distributed File System?
- What is a good solution for cross datacenter master-master replication?
- What is a Kubernetes Manifest?
- What is a provisioning profile used for when developing iPhone applications?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.