Your goal
Create a module that owns one managed identity and can be reused without copying resource configuration.
Exercise 12 ยท 60 minutes
Extract the managed identity into a local module, preserve its state address with a moved block, and instantiate a second identity through a clear module contract.
Before you start
Create a module that owns one managed identity and can be reused without copying resource configuration.
Participant task
Create modules/managed-identity. Decide which values callers control, which values the module derives, and which outputs callers need.
Move the identity resource into the module and call it once from the root configuration. Add a moved block from the old address to the new module address.
Initialize the local module and save a plan. The plan must not destroy or recreate the existing identity. Resolve the address mapping before continuing.
terraform init
terraform validate
terraform plan -out refactor.tfplanInstantiate a second identity for a different sandbox role. Reuse the module contract and standard tags; do not duplicate the resource block.
Review a fresh saved plan, apply it, and confirm that callers can consume each module instance's resource ID and principal ID.
terraform plan -out main.tfplan
terraform apply main.tfplan
terraform outputExpected outcome
Replace separate module calls with for_each. Add the necessary moved block and require a no-replacement plan before apply.
Finish
Add a short module README covering purpose, inputs, outputs, and one usage example. Keep both identities for Exercise 14.