DEV Community

Mustkhim Inamdar
Mustkhim Inamdar

Posted on

Building Scalable Modular AWS Infrastructure with Terraform (IaC)

by M Inamdar

πŸ‘‹ Hey there! I’m Mustkhim Inamdar, a Cloud-Native DevOps Architect passionate about automation, scalability, and next-gen tooling. With 9+ years of experience across Big Data, Cloud Operations (AWS), CI/CD, and DevOps for automotive systems, I’ve delivered robust solutions using tools like Terraform, Jenkins, Kubernetes, LDRA, Polyspace, MATLAB/Simulink, and more.

I love exploring emerging tech like GitOps, MLOps, and GenAI, and sharing practical insights from real-world projects. Let’s dive into the world of DevOps, cloud, and automation together!


Modern cloud infrastructure demands automation, scalability, and maintainability and that’s exactly where Infrastructure as Code (IaC) shines. But as your infrastructure grows, maintaining hundreds of lines of Terraform in a single file becomes chaotic.

That's why I built this:
A clean, modular, and production-grade Terraform repository that can deploy scalable AWS infrastructure across multiple environments.

This article is a deep dive into the repo:
iac-aws-modular-infra


Why This Project Exists

Most Terraform tutorials and starter templates are overly simple or don’t reflect real-world complexity. This project is built for engineers and teams who need:

  • βœ… Modular, reusable infrastructure components
  • βœ… Multi-environment separation (e.g., dev, prod)
  • βœ… Remote state management
  • βœ… Cleaner code structure and CI/CD-ready setup
  • βœ… A launchpad for integrating EKS, RDS, etc.

This repo follows the best practices recommended by HashiCorp and the Terraform community.


What’s Inside the Repo?

This Terraform repo includes the following modular AWS resources:

Module Description
vpc Creates custom VPCs, subnets, route tables, IGW, etc.
ec2 Deploys EC2 instances inside public/private subnets
s3 Creates S3 buckets with custom configuration
sg Custom security groups for EC2, load balancers, etc.
iam IAM roles and policies (basic version included)
cloudwatch Adds alarms and monitoring for EC2 or custom metrics

Directory Layout

iac-aws-modular-infra/
β”‚
β”œβ”€β”€ backend/                # Remote state config using S3
β”‚   └── s3_backend.tf
β”‚
β”œβ”€β”€ environments/           # Separated environments
β”‚   β”œβ”€β”€ dev/
β”‚   β”‚   └── main.tf
β”‚   └── prod/
β”‚
β”œβ”€β”€ modules/                # All infrastructure modules
β”‚   β”œβ”€β”€ vpc/
β”‚   β”œβ”€β”€ ec2/
β”‚   β”œβ”€β”€ s3/
β”‚   β”œβ”€β”€ sg/
β”‚   β”œβ”€β”€ iam/
β”‚   └── cloudwatch/
β”‚
└── README.md
Enter fullscreen mode Exit fullscreen mode

How to Deploy

1. Clone the Repo

Clone the infrastructure code to your local machine and navigate to the desired environment (e.g., dev).

git clone https://github.com/M-Inamdar/iac-aws-modular-infra.git
cd iac-aws-modular-infra/environments/dev
Enter fullscreen mode Exit fullscreen mode

2. Create terraform.tfvars

Define environment-specific values for input variables. This file keeps your configuration modular and reusable.

region               = "ap-south-1"
vpc_cidr_block       = "10.0.0.0/16"
public_subnet_cidrs  = ["10.0.1.0/24"]
private_subnet_cidrs = ["10.0.2.0/24"]
instance_type        = "t2.micro"
ami_id               = "ami-0abcdef1234567890"
Enter fullscreen mode Exit fullscreen mode

Tip: Add this file to .gitignore to avoid committing sensitive or environment-specific data.


Create the .tf files with touch command


3. Initialize Terraform

Set up the working directory and download the required provider plugins and modules.

terraform init
Enter fullscreen mode Exit fullscreen mode

Result


4. Plan & Apply

Preview the changes with plan and then provision the infrastructure using apply.


terraform plan
Enter fullscreen mode Exit fullscreen mode

Result


terraform apply
Enter fullscreen mode Exit fullscreen mode

βœ… You’ll be prompted to confirm before resources are created.

Result


5. Optional: Destroy Infrastructure

Use this to tear down all provisioned resources if no longer needed.

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Caution: This will remove all resources. Double-check before running.


Remote State Management

Terraform uses an S3 bucket to manage state centrally for team collaboration and history tracking.

Example backend config in backend/s3_backend.tf:

terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key    = "dev/terraform.tfstate"
    region = "ap-south-1"
  }
}
Enter fullscreen mode Exit fullscreen mode

Tip: Enable DynamoDB state locking to avoid concurrent apply conflicts:

dynamodb_table = "terraform-locks"
Enter fullscreen mode Exit fullscreen mode

Additional Suggestions

  • Modular Code: Reuse modules from the modules/ directory across different environments (e.g., dev, staging, prod).
  • Secrets Management: Avoid hardcoding credentials. Use environment variables or tools like AWS Secrets Manager or SSM Parameter Store.
  • Validation: Run terraform validate before applying to catch config errors.
  • Automation: Integrate with CI/CD pipelines (e.g., GitHub Actions, Jenkins) for consistent deployment.
  • Documentation: Maintain a README per environment or module for clarity.

Design Philosophy

β€œInfrastructure should be treated as code and that code should be clean, reusable, and easy to test.”

I designed this repo with the following principles:

βœ… Modularity: Each AWS resource lives in its own module
βœ… Reusability: Modules can be called from any environment
βœ… Clarity: Variables and outputs are explicitly defined
βœ… Separation of concerns: Code for prod/dev stays isolated
βœ… Cloud-readiness: Backend is set up for remote storage


What’s Next?

Here’s what I’m planning to add next:

  • Terraform GitHub Actions for CI/CD
  • Cost estimates with Infracost
  • State locking via DynamoDB
  • Terratest-based module unit tests
  • EKS-ready VPC modules

TL;DR

  • Modular Terraform setup βœ…
  • Real-world AWS patterns βœ…
  • Remote state and multi-env support βœ…
  • Ready for scaling and CI/CD βœ…

🀝 Contributing

Feel free to fork the repo, raise issues, or open pull requests. Feedback and contributions are always welcome!

If you use this in your projects, I’d love to hear about it drop a comment or star ⭐ the repo iac-aws-modular-infra

Happy automating!


πŸ’¬ Got questions or stuck somewhere?

Feel free to drop a comment below or DM me on LinkedIn.

I’m always happy to help!

Top comments (2)

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

this is super clean and i wish i had it in my early devops days tbh you think modular setups ever get too complex for their own good

Collapse
 
mustkhim_inamdar profile image
Mustkhim Inamdar

Thanks a lot!
And yep, totally modular setups can get messy if overdone. Balance is key: reusable but not over nested. Appreciate you stopping by.