Skip to content

adityachaudhary99/aws-terraform-multi-env-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AWS Multi-Environment Infrastructure Template

Production-ready Terraform infrastructure for deploying containerized applications on AWS

A complete, modular Terraform setup that provisions a full AWS infrastructure with separate dev, staging, and production environments. Perfect for teams looking to deploy scalable, secure, and cost-optimized containerized applications.


🌟 Overview

This template provides everything you need to run a production-grade containerized application on AWS:

  • πŸ—οΈ Complete Infrastructure: VPC, Load Balancers, Databases, Container Services, and more
  • πŸ”’ Security First: Private subnets, security groups, bastion host for secure access
  • πŸ“ˆ Auto-Scaling: ECS Fargate with CPU/memory-based scaling
  • πŸ’° Cost Optimized: FARGATE_SPOT for non-prod, right-sized instances
  • πŸš€ CI/CD Ready: GitHub Actions integration with ECR
  • 🌍 Multi-Environment: Fully isolated dev, staging, and production environments
  • πŸ“Š Observable: CloudWatch logs and metrics built-in

Total Resources: ~113 AWS resources across all environments


🎯 What Gets Deployed

Infrastructure Components

Component Description Environments
VPC Multi-AZ with public/private subnets, NAT gateways Shared
ALB Application Load Balancers with health checks 3 (dev, staging, prod)
ECS Fargate Serverless container orchestration with auto-scaling 3 (dev, staging, prod)
RDS PostgreSQL Managed databases (Multi-AZ for prod) 3 (dev, staging, prod)
ECR Private Docker container registry Shared
Bastion Host Secure SSH access to databases Shared
Route53 DNS management (optional) Shared
Secrets Manager Secure credential storage 3 (dev, staging, prod)
CloudWatch Logs and monitoring All resources
GitHub Actions Automated CI/CD pipeline Included

Environment Specifications

Resource Dev Staging Production
ECS Tasks 1-4 (SPOT) 1-4 (SPOT) 2-10 (On-Demand)
Task Size 256 CPU / 512 MB 256 CPU / 512 MB 512 CPU / 1024 MB
RDS Instance db.t3.medium db.t3.large db.r6g.2xlarge
RDS Storage 20 GB 50 GB 100-1000 GB (auto-scale)
Multi-AZ No No Yes
Backups 7 days 7 days 30 days

πŸ“ Repository Structure

aws-env-template/
β”œβ”€β”€ πŸ“‚ vpc/                 # VPC, subnets, NAT gateways, routing
β”œβ”€β”€ πŸ“‚ security-groups/     # Security groups for ALB, ECS, RDS
β”œβ”€β”€ πŸ“‚ alb/                 # Application Load Balancers
β”œβ”€β”€ πŸ“‚ database/            # RDS PostgreSQL databases
β”œβ”€β”€ πŸ“‚ ecr/                 # Container registry
β”œβ”€β”€ πŸ“‚ ecs/                 # ECS Fargate clusters & services
β”œβ”€β”€ πŸ“‚ bastion/             # Bastion host for DB access
β”œβ”€β”€ πŸ“‚ route53/             # DNS configuration (optional)
β”œβ”€β”€ πŸ“‚ ci-cd/               # GitHub Actions workflow for deployments
β”œβ”€β”€ πŸ“„ ARCHITECTURE.md      # Detailed architecture documentation
β”œβ”€β”€ πŸ“„ GETTING-STARTED.md   # Step-by-step deployment guide
β”œβ”€β”€ πŸ“„ architecture.mmd     # Mermaid diagram
└── πŸ“„ README.md            # This file

Each module is self-contained with:

  • main.tf - Resource definitions
  • variables.tf - Input variables
  • outputs.tf - Output values
  • README.md - Module-specific documentation
  • terraform.tfvars - You create this with your values

πŸš€ Quick Start

Prerequisites

  • βœ… AWS account with appropriate permissions
  • βœ… AWS CLI installed and configured (aws configure)
  • βœ… Terraform >= 1.0 installed
  • βœ… Basic knowledge of AWS, Terraform, and Docker

Deployment

πŸ“– See GETTING-STARTED.md for detailed step-by-step instructions.

Quick overview:

  1. Clone this repository
  2. Configure each module's terraform.tfvars with your project name
  3. Deploy modules in order: VPC β†’ Security Groups β†’ ALB β†’ Database β†’ ECR β†’ ECS β†’ Bastion β†’ Route53
  4. Deploy your application using the ECR repository and GitHub Actions

Deployment time: ~30-45 minutes for all modules


πŸ“– Documentation

Document Description
GETTING-STARTED.md Complete step-by-step deployment guide
ARCHITECTURE.md Detailed architecture diagrams and explanations
ci-cd/README.md GitHub Actions CI/CD setup and configuration
architecture.mmd Mermaid diagrams (view on GitHub)
Module READMEs Specific instructions for each infrastructure component

πŸ—οΈ Architecture Overview

High-Level Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Internet  β”‚
β”‚   (Users)   β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Application Load    β”‚
β”‚     Balancer         β”‚  ← Public Subnet
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   ECS Fargate        β”‚
β”‚   (Auto-scaling)     β”‚  ← Private App Subnet
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  RDS PostgreSQL      β”‚
β”‚  (Multi-AZ for prod) β”‚  ← Private DB Subnet
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Features

Feature Description
🌍 Multi-Environment Fully isolated dev, staging, and production environments
πŸ“ˆ Auto-Scaling ECS tasks scale based on CPU and memory metrics
πŸ”’ Security Private subnets, security groups, encrypted databases
πŸš€ High Availability Multi-AZ deployment for production databases and load balancers
πŸ“Š Monitoring CloudWatch logs, metrics, and alarms for all services
πŸ”„ CI/CD Ready GitHub Actions workflows for automated deployments
πŸ’Ύ Backup & Recovery Automated RDS backups (7-30 days retention)
πŸ” Secrets Management AWS Secrets Manager for database credentials

πŸ“– See ARCHITECTURE.md for detailed diagrams and network flow explanations.

πŸ’° Cost Breakdown

Monthly Estimate (All 3 Environments)

Service Dev Staging Prod Monthly Total
NAT Gateway $32 $32 $32 $96
ALB $16 $16 $16 $48
ECS Fargate $15 $30 $60 $105
RDS PostgreSQL $50 $100 $200 $350
ECR $1 $1 $1 $3
Secrets Manager $1.20 $1.20 $1.20 $3.60
Bastion Host - - $7.50 $7.50
Total ~$115 ~$180 ~$318 ~$613/month

Cost Optimization

This template includes several cost optimizations:

  • βœ… FARGATE_SPOT for dev/staging (50% cheaper than on-demand)
  • βœ… Right-sized instances (smaller for dev, larger for prod)
  • βœ… RDS storage auto-scaling (pay only for what you use)
  • βœ… Lifecycle policies on ECR (keep only recent images)

Additional savings:

  • πŸ’‘ Stop dev/staging environments during off-hours (~40% savings)
  • πŸ’‘ Use Reserved Instances for production RDS (~30% savings)
  • πŸ’‘ Enable S3 lifecycle policies for logs (~50% savings on storage)

πŸ”§ Customization

This template is designed to be easily customizable:

Project Name

Update the project variable in each module's terraform.tfvars:

project = "my-app"

AWS Region

Change the region variable to deploy in a different region:

region = "ap-south-1"

Resource Sizing

Adjust resources in module main.tf files:

  • ECS: Task count, CPU/memory allocation
  • RDS: Instance class, storage size
  • VPC: CIDR blocks, subnet configuration

πŸ“– See each module's README for specific customization options.


πŸ› οΈ Operations

Monitoring & Logging

All resources include CloudWatch integration:

  • ECS Logs: /ecs/<project>-<env>
  • RDS Logs: Automated to CloudWatch
  • ALB Access Logs: Optional S3 bucket

View logs:

aws logs tail /ecs/your-app-dev --follow

Backups

  • RDS: Automated daily backups (7-30 days retention)
  • Snapshots: Manual snapshots before major changes
  • Secrets: Versioned in Secrets Manager

Scaling

  • ECS: Auto-scales based on CPU/memory (configured per environment)
  • RDS: Manual scaling (requires brief downtime)
  • ALB: Automatically scales with traffic

πŸ“– See GETTING-STARTED.md for operational procedures.


πŸ—‘οΈ Cleanup

To destroy all infrastructure:

# See GETTING-STARTED.md for detailed cleanup instructions
cd route53 && terraform destroy
cd ../bastion && terraform destroy
cd ../ecs && terraform destroy
# ... (continue in reverse order)

⚠️ WARNING: This deletes everything including databases! Take backups first.


❓ FAQ

Can I use this for a single environment?

Yes! Simply deploy only the modules you need for one environment. Modify the Terraform code to remove the environment loops.

Does this support HTTPS/SSL?

The ALB is configured for HTTP. For HTTPS, add an ACM certificate and update the ALB listener. See alb/README.md for instructions.

Can I use Aurora instead of RDS?

Yes! Modify database/main.tf to use Aurora PostgreSQL. Note: Aurora is more expensive but offers better scalability.

How do I add more environments?

Add the environment to the environments variable in each module and run terraform apply.

Is this production-ready?

Yes! This template follows AWS best practices and is suitable for production workloads. However, review and adjust based on your specific requirements.


πŸ› Troubleshooting

Common Issues

Issue Solution
ECS tasks not starting Check CloudWatch logs, verify ECR image exists
ALB health checks failing Ensure /health endpoint returns 200, check security groups
Database connection errors Verify security groups, check Secrets Manager passwords
Terraform state locked Wait for other operations to complete or force unlock

πŸ“– See GETTING-STARTED.md for detailed troubleshooting steps.


🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

How to Contribute

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

TL;DR: You can use this template for personal or commercial projects, modify it, and distribute it freely.


πŸ™ Acknowledgments

  • Built following AWS Well-Architected Framework best practices
  • Inspired by real-world production deployments
  • Community feedback and contributions

πŸ“ž Support


Built with ❀️ for production-ready AWS deployments

License: MIT Terraform AWS

⬆ Back to Top

About

Production-ready Terraform template for AWS multi-environment deployments with ECS Fargate, RDS, ALB, and CI/CD. Includes dev, staging, and prod environments.

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors