How to Manage AWS ECS with Terraform and GitHub

Listen to this Post

Featured Image
AWS Elastic Container Service (ECS) is a powerful yet simpler alternative to Kubernetes (EKS) for container orchestration. It supports both Fargate (serverless) and EC2-backed deployments, making it versatile for various workloads. Integrating Terraform with ECS automates infrastructure provisioning, ensuring consistency and scalability.

You Should Know:

1. Terraform ECS Task Definition

Define your containerized application in a Terraform configuration:

resource "aws_ecs_task_definition" "my_task" {
family = "my-app"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.ecs_execution_role.arn

container_definitions = jsonencode([{
name = "my-container",
image = "my-ecr-repo/my-app:latest",
portMappings = [{
containerPort = 80,
hostPort = 80
}]
}])
}

2. Deploying an ECS Cluster

resource "aws_ecs_cluster" "my_cluster" {
name = "my-ecs-cluster"
}

3. Running an ECS Service

resource "aws_ecs_service" "my_service" {
name = "my-service"
cluster = aws_ecs_cluster.my_cluster.id
task_definition = aws_ecs_task_definition.my_task.arn
launch_type = "FARGATE"
desired_count = 2

network_configuration {
subnets = [aws_subnet.public.id]
security_groups = [aws_security_group.ecs_sg.id]
assign_public_ip = true
}
}

4. GitHub Integration for CI/CD

Use GitHub Actions to automate Terraform deployments:

name: 'Terraform ECS Deployment'
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: hashicorp/setup-terraform@v1
- run: terraform init
- run: terraform apply -auto-approve

5. Essential AWS CLI Commands

  • List running ECS tasks:
    aws ecs list-tasks --cluster my-cluster
    
  • Describe a task:
    aws ecs describe-tasks --cluster my-cluster --tasks <TASK_ID>
    
  • Update a service:
    aws ecs update-service --cluster my-cluster --service my-service --force-new-deployment
    

What Undercode Say:

AWS ECS with Terraform provides a scalable, cost-effective way to deploy containers without Kubernetes complexity. By integrating GitHub Actions, teams achieve seamless CI/CD, reducing manual errors. Fargate eliminates server management, while EC2 allows GPU/hardware-specific workloads.

Prediction:

As serverless adoption grows, AWS will likely enhance ECS with deeper AI/ML integrations, auto-scaling improvements, and tighter security controls.

Expected Output:

  • Terraform-provisioned ECS cluster
  • Automated GitHub CI/CD pipeline
  • Fargate/EC2 workload deployments

Reference:

How We Manage ECS with Terraform and GitHub Repos

IT/Security Reporter URL:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram