Move a resource behind a module interface
Read module sources, input scope, outputs and versions, then preserve an existing resource through a refactor.
Move a resource behind a module interface
A directory of Terraform configuration is a module. The root calls children with module blocks. A child receives declared input variables and exposes selected outputs. It cannot reach into its caller’s local values. Compose small modules by passing one module’s output as another module’s input. Avoid hidden assumptions about provider credentials or the environment.
For registry modules, specify a version constraint and review upgrades. A Git source can select a revision with a ref. The dependency lock file locks provider selections. It does not choose module versions. When a managed resource moves into a child, its address changes even if the physical object is unchanged. Record the address transition with a moved block and inspect the resulting plan.
Worked case 1
A rename into a child causes a plan to remove the old object and create a new one with the same settings. What explains the proposed replacement?
Check your reasoning
The old state address and new configuration address differ. Record the supported move, then check whether the plan recognizes the existing object and whether any real attribute changes remain.
Worked case 2
A network module produces several subnets. An application module needs only one subnet ID. How should the root connect them?
Check your reasoning
Expose the required ID through a network module output. Pass that value into a declared application module input. Internal resource names can then change without forcing consumers to depend on them.
Move a local resource without replacing it
Run the block in Bash with Terraform 1.12 and Python 3. It uses a fresh temporary directory and the built-in provider. Inspect the two plans before applying the one that contains the move.
(
set -eu
cd "$(mktemp -d)"
cat > main.tf <<'HCL'
resource "terraform_data" "receipt" {
input = "alpha"
}
output "tracked_id" {
value = terraform_data.receipt.id
}
HCL
terraform init -input=false >/dev/null
terraform apply -auto-approve -input=false >/dev/null
original=$(terraform output -raw tracked_id)
mkdir child
mv main.tf child/main.tf
cat > main.tf <<'HCL'
module "service" {
source = "./child"
}
output "tracked_id" {
value = module.service.tracked_id
}
HCL
terraform init -input=false >/dev/null
terraform plan -input=false -out=unmapped >/dev/null
cat >> main.tf <<'HCL'
moved {
from = terraform_data.receipt
to = module.service.terraform_data.receipt
}
HCL
terraform plan -input=false -out=mapped >/dev/null
for plan in unmapped mapped; do
terraform show -json "$plan" | python3 -c 'import json,sys; changes=json.load(sys.stdin)["resource_changes"]; print(", ".join(sorted("+".join(x["change"]["actions"]) for x in changes)))'
done
terraform apply -input=false mapped >/dev/null
test "$original" = "$(terraform output -raw tracked_id)"
echo 'Original ID preserved'
)
Check your output
create, delete no-op Original ID preserved
The first plan treats the addresses as distinct. The moved block binds the new address to the original object. Applying the mapped plan updates its address without a replacement.
HashiCorp objective references: 5a–5d.
Move resource and module addresses · Official Terraform 004 objectives for this domain
Related lesson in GitHub Actions. Choose a starter, reusable workflow or composite action.
Practice with the recording
A resource moves from the root module into a child module without changing the remote object. What should a reviewer see in the plan?
Check your reasoning
Declare the address transition in a moved block. Inspect the old and new addresses and confirm the plan describes a move without replacement. Keep the migration declaration for callers upgrading from the earlier layout.
sthithapragna · Terraform Associate 004 full course · 4:06:35
Practice the individual exam skills
- 5a–5d Compare using a local module with pinning a registry module version. Pass one value into the child and return a declared output. Review the plan when changing the module source, version or address so a refactor does not replace working infrastructure.
Move a resource behind a module interface quiz
Answer 3 questions. Check each answer for an explanation.