Hands-on exercises “Modules”
Goal
Refactor the bucket and upload configuration into a reusable child module. The module interface should hide provider-specific resource details while exposing the inputs and outputs a caller needs.
Preparation
- Continue with the provider and state setup from Hands-on 3.
- Review the module documentation and the matching provider module examples in the repository.
- Keep provider configuration in the root module unless the module has a deliberate reason to accept an aliased provider.
Steps
- Create a child module with inputs for the bucket name, location, and any provider-specific settings that are genuinely required.
- Move the bucket and upload resources into the child module.
- Expose the bucket name and uploaded object information as module outputs.
- Call the module from the root module and pass values through variables rather than hard-coding environment-specific settings.
- Run
terraform fmt,terraform validate, andterraform plan. The refactor should preserve the existing resources instead of destroying and recreating them. - If resource addresses change, add
movedblocks or use a reviewed state migration so Terraform keeps tracking the existing objects.
The success criterion is a reusable module with a small interface and a plan that contains no accidental replacement.