Saturday, 14 December 2024

How to Deploy a Node.js App on AWS ECS and Automate It with GitHub Actions [Hands-On Guide]

Deployments can be intimidating, but with a robust platform like AWS ECS and the automation power of GitHub Actions, it doesn’t have to be. If you’re a developer, tech enthusiast, or DevOps engineer wanting to deploy a Node.js app, this guide walks you through every detailed step — from setting up AWS ECS to automating the process using GitHub Actions.

By the time you finish, your Node.js app will be live in an AWS ECS cluster with continuous deployment in place. Let’s get started!

What You’ll Learn

  1. Creating a simple Node.js application.
  2. Setting up a Docker image for your app.
  3. Deploying the app on AWS Elastic Container Service (ECS) using Fargate.
  4. Automating the deployment pipeline with GitHub Actions.

Grab your coffee because this is a hands-on guide filled with actionable steps and code.

Step 1: Create Your Node.js App
Start by creating a simple Node.js application. Open your terminal and follow these quick steps:

  1. Initialize a Node.js project
    Run the following commands:
mkdir node-ecs-app
cd node-ecs-app
npm init -y
  1. Create a simple Express server
    Install Express:
npm install express

Create an index.js file in your project folder with the following code:

const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
  res.send('Hello AWS ECS!');
});
app.listen(port, () => {
  console.log(`App running on http://localhost:${port}`);
});
  1. Test your app locally
    Run:
node index.js

Visit http://localhost:3000 in your browser to ensure that your app works.


Step 2: Dockerize Your Node.js App
Next, containerize the app using Docker, so that it can run anywhere—including AWS ECS.

  1. Create a Dockerfile in your project directory
    Here’s an example Dockerfile:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]
  1. Build the Docker image
    Run:
docker build -t node-ecs-app .
  1. Test your Docker container
    Run your container:
docker run -p 3000:3000 node-ecs-app

Visit http://localhost:3000 to confirm everything works in the containerized environment.

Step 3: Push Your Image to Amazon ECR

AWS Elastic Container Registry (ECR) stores your Docker images so ECS can access them.

  1. Authenticate Docker to AWS ECR
    Run this AWS CLI command:
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>.amazonaws.com
  1. Create an ECR repository
    Execute:
aws ecr create-repository --repository-name node-ecs-app
  1. Tag and push your image to ECR
    Tag your image:
docker tag node-ecs-app <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/node-ecs-app

Push it:

docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/node-ecs-app

Step 4: Set Up ECS with a Fargate Task


AWS ECS allows you to deploy and manage containers easily.

Create a new ECS cluster
  • Go to the AWS Management Console and create a new cluster:
  • Choose “Networking only” to use Fargate.
  • Name your cluster (e.g., node-ecs-cluster).
  1. Create a task definition
    Define how your container will run:
    Go to the “Task Definitions” section and create a new task.
    Choose Fargate as the launch type.
    Add a container:
    Name it node-ecs-app.
    Use the image URL from your ECR repository.
    Set the port mapping to 3000.
  2. Create a service
    Go to your cluster and create a service.
    Use the task definition created earlier.
    Set the desired number of tasks (e.g., 1).
    Choose a VPC and subnets. Allow public IP assignment.
    Once the service is running, note the public IP or Application Load Balancer (ALB) URL where your app is deployed.

Step 5: Automate Deployment with GitHub Actions

Now it’s time to automate the deployment pipeline whenever there’s a change in your repository.

Set up secrets in your GitHub repo

Go to your repository settings and add the following secrets:
AWS_ACCESS_KEY_ID: Your AWS access key.
AWS_SECRET_ACCESS_KEY: Your AWS secret access key.
AWS_REGION: Your AWS region.
ECR_REPOSITORY: Your ECR repository URL.

Add a GitHub Actions workflow
  1. Create a .github/workflows/deploy.yml file:
name: Deploy to AWS ECS
on:
  push:
    branches:
main
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
name: Checkout code
      uses: actions/checkout@v3
name: Set up AWS CLI
      uses: aws-actions/configure-aws-credentials@v2
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: ${{ secrets.AWS_REGION }}
    
name: Log in to Amazon ECR
      run: |
        aws ecr get-login-password --region $AWS_REGION | \
        docker login --username AWS --password-stdin $ECR_REPOSITORY
    
name: Build and push Docker image
      run: |
        docker build -t node-ecs-app .
        docker tag node-ecs-app $ECR_REPOSITORY
        docker push $ECR_REPOSITORY
    
name: Deploy to ECS
      run: |
        aws ecs update-service --cluster node-ecs-cluster \
        --service node-ecs-service --force-new-deployment
Commit and push your changes
  1. Run:
git add .
git commit -m "Setup GitHub Actions workflow"
git push

GitHub Actions will now automatically build and deploy your app to ECS whenever changes are pushed to the main branch.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home