Skip to content

Terraform for AWS ECS on Fargate : Base Resources for Mircroservices Deployment with CI/CD

Notifications You must be signed in to change notification settings

sagar-uprety/ecs-on-fargate-main-infra

Repository files navigation

AWS ECS on Fargate Terraform : Base Resources for Mircroservices Deployment with CI/CD

Terraform AWS

Overview

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.

Architecture

AWS Architecture

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
  • CI/CD

    • AWS Codepipeline
    • AWS Codebuild (also builds the microservices docker images)
    • GitHub as the source code repository

Network Configuration

  • 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

Prerequisites

  • AWS Account with appropriate permissions
  • Terraform (>= 1.0.0)
  • AWS CLI configured with credentials
  • Docker installed locally
  • Git

Branch Information

  • main: Complete infrastructure with CI/CD pipeline integration
  • ms: Base infrastructure without pipeline integration

Project Structure

  • main.tf: Defines the core infrastructure for ecs_cluster, ecs_service, and supporting resources
  • codepipeline*.tf: Defines the terraform config for setting up individual CI/CD pipelines for the microservices
  • variables.tf: Input variables for parameterization and sensitive data
  • provider.tf: Specifies the AWS provider
  • output.tf: Defines outputs for the tf configs
  • dev.tfvars: Variables specific to the dev environment
  • versions.tf: Terraform version constraints and backend configuration
  • locals.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

Related Microservices Repositories

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:

  1. User Service
  2. Product Service
  3. Order Service

Deployment Instructions (Quick Start Guide)

  1. Clone the repository:

    git clone https://github.com/sagar-uprety/ecs-on-fargate-main-infra
    cd ecs-on-fargate-main-infra
  2. Select the appropriate branch:

    # For pipeline version (CI/CD)
    git checkout main
    
    # For non-pipeline version (without CI/CD)
    git checkout ms
  3. 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"
    }
    ...
  4. Initialize Terraform:

    terraform init
  5. Configure your deployment:

    # Edit dev.tfvars with your configuration
  6. Review the deployment plan:

    terraform plan --var-file="dev.tfvars"
  7. Apply the infrastructure:

    terraform apply --var-file="dev.tfvars"
  8. To destroy the infrastructure:

    terraform destroy --var-file="dev.tfvars"

Contributing

  1. Fork the repository
  2. Create a feature branch:
    git checkout -b feature/your-feature-name
  3. Commit your changes:
    git commit -m "Add your feature description"
  4. Push to your fork:
    git push origin feature/your-feature-name
  5. Create a Pull Request

License

This project is licensed under The MIT License.

About

Terraform for AWS ECS on Fargate : Base Resources for Mircroservices Deployment with CI/CD

Resources

Stars

Watchers

Forks

Languages