terraform state mv is the dedicated command for moving addresses inside the state file without touching real infrastructure. It is your safety net during refactors: renaming resources, extracting modules, and redesigning count/for_each.
Misuse it, however, and the next apply can create or destroy resources you never intended to touch. Based on the official documentation, this article walks through the operational procedure and the exam-relevant points with minimal risk.
terraform state mv transfers the binding in the state from a SOURCE address to a DESTINATION address. It never creates, modifies, or deletes a cloud resource. After the move, if the HCL address matches the state address, terraform plan reports no diff.
On remote backends, the command acquires a lock (when supported) and updates the state directly. Moves across workspaces or across backends are not handled directly.
A Terraform address is composed of resource type/name, module hierarchy, and count/for_each indices. state mv exists to follow address changes safely.
The typical patterns are: (1) resource renames, (2) lifting a resource from the root into a module, and (3) redesigning count to for_each by moving each index-to-key mapping individually.
Visualizing a root-to-module move
Before (state)
aws_security_group.sg
|
v
After (state)
module.network.aws_security_group.sg
Configuration (HCL)
- Before: resource "aws_security_group" "sg" { ... }
+ After: module "network" { source = "./modules/network" }
# aws_security_group.sg is defined inside the moduleIn practice, take a backup before any move, confirm a no-diff plan, and have an immediate rollback path ready. On remote backends, assume locking is in effect and finish quickly.
Never apply while a partial move is in flight. If the HCL and state get out of sync, you risk unintended recreations.
Minimal procedure example (renaming, then modularizing, each verified)
# Back up the state
terraform state pull > backup.tfstate
# Rename the resource
terraform state mv aws_instance.web aws_instance.app
terraform plan # must show no diff
# Move into a module
terraform state mv aws_security_group.sg module.network.aws_security_group.sg
terraform plan # must show no diff
# Example rollback if something goes wrong
# 1) Inspect the locally saved backup.tfstate
# 2) If not remote: terraform state push backup.tfstate
# For remote backends, follow your team's equivalent recovery runbookTerraform 1.1+ introduced the moved block, which lets you declare renames in the configuration itself. moved transfers state automatically during plan/apply and fits team-based, staged migrations and CI. state mv, by contrast, is an operator tool that rewrites state immediately.
As a design rule of thumb, prefer moved for stable renames and modularizations, and use state mv for emergency fixes or precise per-instance adjustments.
| Aspect | state mv | moved block | Notes |
|---|---|---|---|
| When it applies | Updates state immediately | Moves during plan/apply | moved is easy to verify in PR / CI |
| Change record | Leaves no trace in configuration | History stays in HCL | In strict audit environments, prefer moved |
| Granularity | Precise, instance-level control | Declarative, block-level | state mv is best for individual count/for_each keys |
| Recovery on failure | Manual recovery from backup.tfstate | Easier to catch during plan | Either way, a prior backup is mandatory |
| Learning / exam focus | Understanding CLI options and address syntax | Understanding moved syntax and behavior | Be able to explain when to pick each |
When migrating count to for_each, move each index-to-key mapping one at a time. Plan the order deliberately, and confirm a no-diff plan after every single move.
Direct moves between workspaces or backends are not supported. If required, combine import with state rm, or fall back to a carefully orchestrated pull/push transfer (watch closely for conflicts and locks).
On Professional-level exams, expect questions about the scope of state mv, its relationship with configuration changes, comparisons with moved, and address notation across count/for_each and module hierarchies.
Operationally, lining up four things — backups, locks, staged application, and review process — drastically reduces incident rates.
Pro
問題 1
You have moved aws_security_group.sg under module.network in an existing environment. The HCL has been updated, and you want to align state with the new address without recreating the resource. Which action is most appropriate?
正解: A
Use terraform state mv to move state to a new address. import creates a new binding but leaves the old address in place, causing duplicates. taint forces recreation, which violates the requirement. A configuration update alone does not move state automatically — the moved block is the only exception.
Does state mv affect real infrastructure? Do I need to take downtime?
It does not. The command only rewrites addresses inside the state file and never calls any cloud API. No downtime is needed, but on lock-capable backends it is safer to reserve a maintenance window so other people do not push competing changes while you work.
Can state mv move resources between workspaces?
Not directly. Moving across workspaces or backends is not supported. The usual pattern is to terraform import in the destination and terraform state rm in the original workspace. Manual transfers via pull/push carry a high risk of conflicts and corruption, so do them only under documented procedures and peer review.
What is the best procedure to migrate from count to for_each?
Decide on a stable key scheme first, then map each count index to a for_each key one by one using terraform state mv. After every move, run terraform plan and confirm a no-diff result. Crucially, do not apply midway through the migration.
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...