Deploying Docker Containers on AWS ECS Fargate with ALB: A Step-by-Step Guide

Deploying Docker Containers on AWS ECS Fargate with ALB: A Step-by-Step Guide

ยท

4 min read

Introduction:

In the era of cloud computing, deploying applications in a scalable and reliable manner is essential for businesses of all sizes. Amazon Web Services (AWS) offers a powerful suite of services for container management and orchestration. Among them, Amazon Elastic Container Service (ECS) is a robust platform for deploying and managing Docker containers at scale.

In this guide, we will walk through the process of deploying Docker containers on AWS ECS, augmented with the use of an Application Load Balancer (ALB) to distribute incoming traffic among container instances. By following these steps, you'll be able to create a highly available and scalable architecture for your applications, ensuring they remain responsive even under varying loads.

Whether you're a seasoned AWS user or new to the platform, this guide will provide you with a comprehensive overview of the steps involved in setting up your ECS cluster, defining task definitions, configuring an ALB, and deploying your Docker containers. Let's dive in and explore the world of containerized deployments on AWS ECS.

Step 1: Create a Docker Container

First, you'll need a Docker container to deploy on ECS. You can use an existing Docker image or create your own.

Refer to My blog (How to Push Docker Images to Amazon ECR (Elastic Container Registry) - Private Repository)

For this example, let's create a simple Node.js web server.

  1. Create an Dockerfile in your project directory:
FROM --platform=linux/amd64 node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install
CMD ["node", "index.js"]
EXPOSE 3000
  1. Create a index.js file in the same directory:
const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Welcome to my app!!!!!!");
});

app.get("/info", (req, res) => {
  res.send("hey this is /info api");
});

app.listen(3000, () => {
  console.log("listening on 3000");
});
  1. Build the Docker image:
docker build -t your-image-name .

Step 2: Push Docker Image to ECR (Elastic Container Registry)

Before deploying to ECS, push your Docker image to the Amazon ECR.

  1. Tag your Docker image:
docker tag your-image-name:latest your-ecr-repository-uri:latest
  1. Push the image to ECR:
docker push your-ecr-repository-uri:latest

Step 3: Create an IAM role for ECS

  1. To create an IAM role, navigate to the IAM console, select "Roles," and then "Create role."

  2. Choose the type of trusted entity, "ecs-tasks"

  1. Attach relevant permissions policies, review the role details, provide a name and optional description, and finally, create the role.

  2. This role can be used to grant permissions to AWS services, users, or resources as needed within your AWS environment.

Step 4: Create ECS Cluster and Task Definition

  1. Go to the ECS service in the AWS Management Console.

  2. Create a new ECS cluster (fargate).

  1. Define a Task Definition:

    • Specify container details, including image URI (from ECR).

    • Define CPU, memory, networking, etc.

Step 5: Create an Application Load Balancer (ALB)

  1. Go to the EC2 service in the AWS Management Console.

  2. Under Load Balancers, create a new Application Load Balancer (internet facing).

    • Configure listeners, security settings, and routing.

    • Security Group rules for ALB are below.

    • Create a target group (IP addresses) and select VPC, health check path (/).

      • Don't select any target instances.

Step 6: Configure ECS Service

  1. Create a new ECS service within your cluster.

    • Select your task definition and Configure the desired number of tasks.

  1. Select VPC, subnets, and security group for the cluster.

  1. Select the load balancer and target group created before.

Step 7: Test

Once everything is set up, access your ALB's DNS name in a web browser. You should see your Node.js application running.

Conclusion

That's a basic overview of creating an ECS container with an ALB setup in AWS. Make sure to adjust the configurations according to your specific requirements and security considerations. Let me know if you need further assistance!

Did you find this article valuable?

Support NavyaDevops by becoming a sponsor. Any amount is appreciated!

ย