Terraform Assignment– 4
Modules & Reusability
Basic Questions
- Create a local Terraform module named vpc with main.tf, variables.tf, and outputs.tf.
- Add input variables vpc_cidr and project to the vpc module.
- Add outputs vpc_id and vpc_cidr_block to the vpc module.
- Call the local vpc module from a root configuration using source = “./modules/vpc”.
- Pass vpc_cidr = “10.0.0.0/16” to the vpc module from the root module.
- Create a local module subnet that accepts a list of subnet CIDRs and returns subnet IDs.
- Use the subnet module in the root module and pass two subnet CIDRs.
- Create a local module tags that returns a merged map of common tags.
- Use the tags module output to tag resources in the vpc module.
- Add a README.md to the vpc module describing inputs and outputs.
- Add versions.tf in the vpc module to pin the AWS provider version.
- Create an examples/simple folder that demonstrates using the vpc module.
- Use variable descriptions and types in variables.tf of the vpc module.
- Add default values for non-sensitive inputs in the vpc module.
- Validate the module with terraform validate in the modules/vpc folder.
- Format all module files with terraform fmt -recursive.
- Create a root output that exposes the vpc_id from the vpc module.
- Reference a module output in another module input (VPC ID → Subnet module).
- Add input variable validation rules in the vpc module for CIDR format.
- Call the vpc module twice to provision two isolated VPCs with different CIDRs.
Intermediate Questions
- Create a reusable security-group module that accepts rules via a list of objects.
- Use for_each to instantiate the subnet module per availability zone.
- Create a route-table module and associate it with subnets using module outputs.
- Add locals in the vpc module to standardize name prefixes across resources.
- Add depends_on in the root module to ensure VPC is created before subnets.
- Publish the vpc module to a private Git repo and reference it via source = “git::…#ref”.
- Use a Registry module terraform-aws-modules/vpc/aws in a separate root example.
- Compare outputs of your local vpc module with the Registry VPC module in a table (as code comments).
- Add semantic version constraints when using the Registry module (e.g., ~> 5.0).
- Create an outputs.tf in the root to expose IDs from all child modules.
- Introduce variable validation for environment (allow only dev|qa|prod) in all modules.
- Create a module.tf that wires modules vpc, subnet, and security-group together.
- Use terraform graph to visualize module relationships and save the output.
- Add precondition and postcondition checks inside the vpc module (Terraform 1.3+).
- Split module inputs into required and optional with sensible defaults.
- Create an example/complete that provisions VPC, subnets, and routes using only module variables.
- Add README badges and an inputs/outputs table generated by terraform-docs for the vpc module.
- Refactor duplicate resource arguments into locals across all modules.
- Implement count vs for_each patterns in the subnet module and document the trade-offs.
- Create a sample CI step (script file) that runs init/validate/plan against the examples/complete usage.
Advanced Questions
- Build a reusable vpc module that supports public/private subnets, NAT gateways (toggle), and custom route tables.
- Add optional creation of VPC flow logs in the vpc module controlled by a boolean input.
- Implement cross-module composition: vpc → subnet → nat-gateway → route-table with only outputs/inputs wiring.
- Create a network root stack that can switch between your local vpc module and the Registry VPC module via a variable flag.
- Add validation and preconditions to ensure NAT requires at least one public subnet.
- Introduce module versioning via Git tags and reference a specific tag in the root configuration.
- Add an examples/multi-env folder showing the same modules deployed for dev and prod using different variable files.
- Create a module naming that standardizes resource names and is consumed by all other modules.
- Implement a tests folder with a minimal smoke test (plan JSON diff check) for the vpc module.
- Build a complete reusable stack: vpc module → subnet module → security-group module → root outputs; consume it from two different projects using only variable files.