Deploy Nodejs App to Amazon ECS using GitHub Actions and IaC using Terraform

Listen to this Post

awstip.com

Practice Verified Codes and Commands

1. Terraform Configuration for ECS

[hcl]
provider “aws” {
region = “us-east-1”
}

resource “aws_ecs_cluster” “my_cluster” {
name = “my-ecs-cluster”
}

resource “aws_ecs_task_definition” “my_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” “my_service” {
name = “my-service”
cluster = aws_ecs_cluster.my_cluster.id
task_definition = aws_ecs_task_definition.my_task.arn
desired_count = 1
launch_type = “FARGATE”

network_configuration {
subnets = [“subnet-0123456789abcdef0”]
security_groups = [“sg-0123456789abcdef0”]
}
}
[/hcl]

2. GitHub Actions Workflow for CI/CD

name: Deploy Node.js App to 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: Install Terraform
uses: hashicorp/setup-terraform@v1</p></li>
<li><p>name: Terraform Init
run: terraform init</p></li>
<li><p>name: Terraform Apply
run: terraform apply -auto-approve

3. AWS CLI Commands for ECS Management


<h1>List ECS clusters</h1>

aws ecs list-clusters

<h1>Describe an ECS cluster</h1>

aws ecs describe-clusters --clusters my-ecs-cluster

<h1>Update ECS service</h1>

aws ecs update-service --cluster my-ecs-cluster --service my-service --desired-count 2

What Undercode Say

Deploying a Node.js application to Amazon ECS using GitHub Actions and Terraform is a robust approach for modern cloud-native development. By leveraging Infrastructure as Code (IaC), you ensure reproducibility and scalability of your infrastructure. GitHub Actions automates the deployment pipeline, reducing manual intervention and errors.

For Linux and Windows users, mastering commands like `aws ecs` for ECS management, `terraform apply` for infrastructure deployment, and `git push` for CI/CD integration is essential. Additionally, understanding containerization concepts with Docker and Kubernetes can further enhance your cloud skills.

To deepen your knowledge, explore AWS documentation on ECS and Terraform’s official guides. For hands-on practice, consider using AWS Free Tier to experiment with ECS and GitHub Actions.

Useful URLs:

By combining these tools and commands, you can build a seamless DevOps workflow for deploying and managing containerized applications on AWS.

References:

Hackers Feeds, Undercode AIFeatured Image