terraform refresh Command
What you'll learn: How the terraform refresh command syncs your state file with the actual, current status of your real infrastructure without changing any resources.
What Does terraform refresh Do?
Think of your Terraform state file as a snapshot of your infrastructure. Over time, that snapshot can get stale—someone might have manually changed a resource in the AWS console, or another tool might have modified something. The terraform refresh command is like taking a new photo of your infrastructure and updating your records, but it doesn't rearrange the furniture—it just updates what's written down.
When you run terraform refresh, Terraform:
- Reads your configuration files to know what to check
- Queries each provider (AWS, Azure, GCP, etc.) to get the current real-world state of your resources
- Updates your state file to reflect reality
- Does not make any changes to actual infrastructure
When Is This Useful?
Imagine you deployed a virtual machine with Terraform, and someone later manually changed its instance type in the cloud console. Your state file still says it's a t2.micro, but reality says it's now a t2.small. Running terraform refresh updates the state file to show t2.small, so future terraform plan commands will accurately detect drift.
Modern Practice Note
In newer Terraform workflows, terraform plan and terraform apply automatically refresh state by default, so you rarely need to run refresh separately. It's mostly used for explicit state synchronization or troubleshooting.
Key Takeaway: terraform refresh updates your state file to match real infrastructure without modifying any resources—it's a read-only sync operation that keeps your records accurate.