This repository contains Infrastructure as Code (IaC) for deploying Node.JS microservices (3) using AWS Elastic Container Service (ECS) with Fargate launch type. The infrastructure is designed following AWS best practices for high availability, scalability, and security.
The application is a simple e-commerce application with user details, product catalogue and order service.
The infrastructure implements a microservices architecture with the following key components:
-
VPC Configuration
- Dual Availability Zones (us-east-2a, us-east-2b)
- Public and Private Subnets
- NAT Gateways for private subnet connectivity
- Internet Gateway for public access
-
Container Infrastructure
- ECS Fargate Cluster
- ECS Services (Three Microservices) and Task (# Note: each microservice have it's own repo. Check them below)
- Application Load Balancer
- Elastic Container Registry (ECR)
- Service Discovery
-
Microservices
- User Service (Port 3000)
- Product Service (Port 3001)
- Order Service (Port 3002)
-
Monitoring & Security
- CloudWatch Integration
- Identity and Access Management (IAM)
- Security Groups
- Network ACLs
-
Data Layer
- Amazon DyanamoDB (3 Tables)
- User Table
- Product Table
- Order Table
- Amazon DyanamoDB (3 Tables)
-
CI/CD
- AWS Codepipeline
- AWS Codebuild (also builds the microservices docker images)
- GitHub as the source code repository
- VPC CIDR: 10.0.0.0/16
- Public Subnet 1: 10.0.0.0/16
- Public Subnet 2: 10.0.1.0/16
- Private Subnet 1: 10.0.32.0/19
- Private Subnet 2: 10.0.32.0/19
- AWS Account with appropriate permissions
- Terraform (>= 1.0.0)
- AWS CLI configured with credentials
- Docker installed locally
- Git
- main: Complete infrastructure with CI/CD pipeline integration
- ms: Base infrastructure without pipeline integration
main.tf
: Defines the core infrastructure for ecs_cluster, ecs_service, and supporting resourcescodepipeline*.tf
: Defines the terraform config for setting up individual CI/CD pipelines for the microservicesvariables.tf
: Input variables for parameterization and sensitive dataprovider.tf
: Specifies the AWS provideroutput.tf
: Defines outputs for the tf configsdev.tfvars
: Variables specific to the dev environmentversions.tf
: Terraform version constraints and backend configurationlocals.tf
: local value to be used in main.tf.pre-commit-config.yaml
: git pre-commit configuration.gitignore
: Lists files to exclude from version control
The terraform configuration and application source code to deploy the individual ECS services with CI/CD built-in are in separate repositories. Find them below:
-
Clone the repository:
git clone https://github.com/sagar-uprety/ecs-on-fargate-main-infra cd ecs-on-fargate-main-infra
-
Select the appropriate branch:
# For pipeline version (CI/CD) git checkout main # For non-pipeline version (without CI/CD) git checkout ms
-
You need an S3 bucket as a backend for Terraform state. Create one if you have not already and update the backend configuration in
versions.tf
:... backend "s3" { bucket = "dev-terraform-state-bucket" # update the bucket name here region = "us-east-2" encrypt = true key = "main/ecs-on-fargate-main.tfstate" } ...
-
Initialize Terraform:
terraform init
-
Configure your deployment:
# Edit dev.tfvars with your configuration
-
Review the deployment plan:
terraform plan --var-file="dev.tfvars"
-
Apply the infrastructure:
terraform apply --var-file="dev.tfvars"
-
To destroy the infrastructure:
terraform destroy --var-file="dev.tfvars"
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature-name
- Commit your changes:
git commit -m "Add your feature description"
- Push to your fork:
git push origin feature/your-feature-name
- Create a Pull Request
This project is licensed under The MIT License.