Listen to this Post
Running container-based applications on AWS can be efficiently managed using the Elastic Container Service (ECS). This article explores how to deploy a Node.js app on AWS ECS Fargate using Terraform for Infrastructure as Code (IaC) and GitHub Actions for CI/CD automation.
You Should Know:
1. Terraform Configuration for ECS Fargate:
Below is a basic Terraform script to set up an ECS cluster with Fargate:
provider "aws" {
region = "us-east-1"
}
resource "aws_ecs_cluster" "cluster" {
name = "my-ecs-cluster"
}
resource "aws_ecs_task_definition" "task" {
family = "my-task"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
container_definitions = jsonencode([{
name = "my-container"
image = "my-nodejs-app:latest"
essential = true
portMappings = [{
containerPort = 80
hostPort = 80
}]
}])
}
resource "aws_ecs_service" "service" {
name = "my-service"
cluster = aws_ecs_cluster.cluster.id
task_definition = aws_ecs_task_definition.task.arn
desired_count = 1
launch_type = "FARGATE"
network_configuration {
subnets = ["subnet-0123456789abcdef0"]
security_groups = ["sg-0123456789abcdef0"]
assign_public_ip = true
}
}
2. GitHub Actions Workflow for CI/CD:
Here’s a sample GitHub Actions workflow to automate the deployment process:
name: Deploy to AWS ECS
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
<ul>
<li>name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1</p></li>
<li><p>name: Terraform Init
run: terraform init</p></li>
<li><p>name: Terraform Apply
run: terraform apply -auto-approve
3. Essential Commands:
- Terraform Commands:
terraform init: Initializes the Terraform working directory.terraform plan: Generates an execution plan.terraform apply: Applies the changes required to reach the desired state.-
terraform destroy: Destroys the Terraform-managed infrastructure. -
AWS CLI Commands:
aws ecs list-clusters: Lists all ECS clusters.aws ecs describe-services --cluster my-ecs-cluster --services my-service: Describes the specified service.aws ecs update-service --cluster my-ecs-cluster --service my-service --desired-count 2: Updates the desired count of tasks in a service.
What Undercode Say:
Deploying applications on AWS ECS Fargate using Terraform and GitHub Actions streamlines the process of managing infrastructure and automating deployments. This approach not only enhances efficiency but also ensures consistency and reliability in your deployments. By leveraging these tools, you can focus more on developing your applications while AWS handles the underlying infrastructure. For further reading, you can refer to the original article.
Additional Linux/IT Commands:
- Linux Commands:
docker ps: Lists running containers.docker logs <container_id>: Fetches logs of a specific container.kubectl get pods: Lists all pods in the current namespace.kubectl describe pod <pod_name>: Describes a specific pod.-
Windows Commands:
Get-Process: Lists all running processes.Get-Service: Lists all services.Test-NetConnection -ComputerName <host> -Port <port>: Tests network connectivity to a specific host and port.
References:
Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅



