- Introduction: Terraform introduced the
movedblock in version 1.1.0 to refactor resources by mapping old to new addresses, reducing state loss and import management risks. - What Is the Terraform Moved Block?: Used within a module's
movedsection to declare resource or data source migration, preserving state and handling renaming/movement across modules. Syntax:moved { from = "<old_address>", to = "<new_address>" }. - When to use the Terraform Moved Block?: Useful for renaming resources, reorganizing modules, refactoring module names, changing resource block types, and splitting/consolidating configurations.
How to Use the Terraform Moved Block:
- Example 1: Renaming a Resource: Declare the old and new resource names in separate configurations and use the
movedblock to map them. - Example 2: Moving Resources Between Modules: Update the resource location in the new configuration and use the
movedblock to reflect the change. - Example 3: Using Moved With for_each: Map resources with
for_eachloops using multiplemovedblocks. - Example 4: Moving a Resource With a Changed Identifier: Map old and new identifiers using
movedblocks when thefor_eachkey expression changes. - Example 5: Moving a Resource Between Providers: Ensure the resource is associated with the new provider using the
movedblock.
- Example 1: Renaming a Resource: Declare the old and new resource names in separate configurations and use the
- Terraform Moved Block Limitations: Requires manual specification, is only for resource renaming/moving, is static with no conditional logic, is only applied during
planandapply, requires state compatibility, and is only supported in Terraform 1.1+. - Key Points: The
movedblock is a useful tool for resource restructuring, ensuring seamless state updates and maintaining operational stability.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用。你还可以使用@来通知其他用户。