In Terraform, reconciling the drift between the real environment and the Terraform state file — what we call state sync — is a critical operation. Historically this was done with terraform refresh, but today the recommended workflow uses the -refresh-only flag.
This article walks through the correct way to perform state sync per the official recommendations, the common pitfalls, and the points commonly tested on the Associate / Pro exams, all from a practical operations perspective.
Terraform keeps the last known state of real resources in tfstate. When humans or external tools change the real environment, drift opens up between the state file and reality. State sync is the operation that overwrites that drift with the facts on the ground and corrects the state file.
The legacy terraform refresh reads real resources via the provider and updates only the state file. It does not touch your configuration (.tf) and does not create, update, or delete any infrastructure. It is now deprecated, and the -refresh-only flag described below is recommended.
The official documentation treats the legacy terraform refresh as deprecated. The replacements are terraform plan -refresh-only and terraform apply -refresh-only. They explicitly express plan and apply that only update state, and they are the recommended approach today.
Exact behavior, messages, and deprecation warnings can differ by version, so always confirm the local CLI spec in your environment with terraform version and terraform -help.
Minimum set of replacement commands
terraform plan -refresh-only
terraform apply -refresh-only
# When you want to sync only some resources (kept minimal)
terraform plan -refresh-only -target=aws_instance.web
terraform apply -refresh-only -target=aws_instance.web
# Suppress API reads during planning (cache-like usage). Validate with a normal plan/apply before the final apply
terraform plan -refresh=falseTerraform's planning engine first fetches the latest values of real resources through the provider, then computes the diff against the state file. With -refresh-only attached, that diff is restricted to state file updates, and no create, update, or delete actions are proposed against infrastructure.
The diagram below is a simplified path from user action through provider reads to the state update.
High-level flow of state sync (refresh-only)
User CLI
|
v
Terraform Core -----> State Backend (lock)
|
v
Provider(s) -----> Cloud/API (Read only)
|
v
Diff Engine (refresh-only: state updates only)
|
v
Update tfstate (no infra changes)Save the plan, then safely apply (state-only update)
terraform plan -refresh-only -out=refresh.plan
# Review the diff
terraform show refresh.plan
# Apply the state-only update
terraform apply refresh.planFor audits, inventories, or checking whether manual changes occurred, first visualize with terraform plan -refresh-only. If nothing looks wrong, bring tfstate up to date with terraform apply -refresh-only. This is also useful for periodic drift detection in CI and post-incident state reconciliation.
A key caveat: resources whose real entities have disappeared will be removed from state by a refresh-only apply. If recovery is possible or the loss was unintended, do not run apply -refresh-only immediately; instead investigate the cause and consider re-importing with terraform import.
Options when you do not want deleted resources removed from state
# Do not apply right away. First just review the diff
terraform plan -refresh-only
# If accidental deletion is possible, recover and then re-import into state with import
terraform import aws_instance.web i-0123456789abcdef0Commands with similar names differ in purpose and side effects. Here we untangle the easily-confused points for both exam prep and real-world operations.
| Command | Primary Purpose | Infra Change | State Update |
|---|---|---|---|
| terraform refresh (deprecated) | State-only refresh | None | Yes (removed from state if entity is gone) |
| terraform plan -refresh-only | Show state-only diff | None | None (display only) |
| terraform apply -refresh-only | Apply state-only refresh | None | Yes (diff reflected to state) |
| terraform plan (default) | Build a change plan | None (plan only) | Reads internally to fetch latest values |
| terraform plan -refresh=false | Speed up planning / suppress API calls | None (plan only) | Read skipped |
On the Associate exam, what matters is correctly picking the intent of state sync and the meaning of -refresh-only. On the Pro exam, you are tested on situation-aware workflow design: drift handling, scoped use of -target, and intentional use of -refresh=false.
Remember: a normal plan/apply also refreshes state, but refresh-only restricts the operation to a state update. Infrastructure is never modified.
Associate / Pro
問題 1
You suspect manual changes in a running environment. You absolutely do not want to alter infrastructure itself, but you do want to refresh Terraform state. Which command is recommended?
正解: A
Current best practice is to use the -refresh-only flag. apply -refresh-only updates only the state file and does not change infrastructure. refresh is deprecated, plan only shows a diff without applying it, and import is for adopting existing resources and will not run without an ID.
Does apply -refresh-only really leave infrastructure unchanged?
Yes. It reads the latest state of real resources from the provider and applies the diff to tfstate only. It performs no write operations such as create, update, or delete.
What happens to resources whose real entities have already been deleted?
With refresh-based commands, entries judged as nonexistent are removed from state. If you suspect an unintended deletion, do not run apply -refresh-only immediately; instead investigate the cause, restore if possible, and consider re-importing with terraform import as needed.
Is -refresh-only available in every version?
On the current Terraform 1.x line, -refresh-only is the recommended path. However, the exact messages and warnings can differ by version. Before operating, verify the spec on your machine with terraform version, terraform -help, and the official CLI documentation.
Practice with certification-focused question sets
無料で問題を解いてみるNicheeLab Editorial Team
NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.
HCL Syntax: Terraform's Configuration Language (2026)
HCL2 fundamentals for Terraform — blocks, attributes, expres...
Terraform Authoring & Operations Pro: Complete Guide (2026)
Tactics for the Terraform Pro exam — module authoring, works...
Terraform Providers: Plugin Management Fundamentals (2026)
Provider mechanics — required_providers, versions, mirrors, ...
Terraform Resource Blocks: Declarative Infra Units (2026)
Resource block fundamentals — addresses, references, common ...
Terraform Data Sources: Read-Only External Data (2026)
Data source basics — declaration, refresh behavior, dependen...