Modularize AWS Infrastructure with Local Child Modules
Break down monolithic infrastructure into reusable, self-contained local child modules (./module/ec2 and ./module/s3) invoked cleanly from a root main.tf module.
Core Implementation Highlights
Project Directory Structure
Organize your code by separating the root module from individual child resource modules. Each sub-folder under module/ serves as an independent, encapsulated Terraform module.
task5-module/
├── main.tf # Root module calling ec2 and s3 child modules
└── module/
├── ec2/
│ └── main.tf # EC2 instance resource definition
└── s3/
└── main.tf # S3 bucket resource definitionRoot & Child Module Code
1. Root Configuration (main.tf)
The root module acts as the orchestrator. It uses module blocks with relative file paths in source to invoke the child modules located in subdirectories.
# Instantiate EC2 Child Module
module "ec2" {
source = "./module/ec2"
}
# Instantiate S3 Child Module
module "s3" {
source = "./module/s3"
}2. EC2 Child Module (module/ec2/main.tf)
Contains the aws_instance resource configuration for provisioning an Amazon EC2 virtual machine using AMI ID ami-0272eca93537d5c9d and t3.micro instance size.
resource "aws_instance" "my_ec2" {
ami = "ami-0272eca93537d5c9d"
instance_type = "t3.micro"
}3. S3 Child Module (module/s3/main.tf)
Contains the aws_s3_bucket resource block for creating a unique object storage bucket named shan-demo-bucket-12345.
resource "aws_s3_bucket" "my_bucket" {
bucket = "shan-demo-bucket-12345"
}Code Breakdown & Concepts
Local Module Source Paths
The source argument tells Terraform where to find the module configuration. When starting with ./ or ../, Terraform treats it as a local path relative to the root directory.
source = "./module/ec2"points to local directorymodule/ec2.source = "./module/s3"points to local directorymodule/s3.
module "ec2" {
source = "./module/ec2"
}
module "s3" {
source = "./module/s3"
}Module Resource Addressing
When resources are defined inside a child module, Terraform namespaces their state addresses under the module name:
module.ec2.aws_instance.my_ec2— EC2 instance state address.module.s3.aws_s3_bucket.my_bucket— S3 bucket state address.
Deployment & Execution Commands
Run these commands in your root directory (task5 - module/) to initialize, plan, and apply your modular infrastructure:
# 1. Initialize Terraform (Crucial: Indexes local child modules)
$ terraform init
# 2. Validate configuration syntax across all modules
$ terraform validate
# 3. Format code recursively across root and child modules
$ terraform fmt -recursive
# 4. Preview resource creation plan
$ terraform plan
# 5. Apply and provision EC2 and S3 resources
$ terraform apply -auto-approve
# 6. Verify state address namespacing
$ terraform state listExpected State Output
Running terraform state list after apply confirms that both resources were successfully created under their respective module namespaces:
module.ec2.aws_instance.my_ec2
module.s3.aws_s3_bucket.my_bucket