DEV Community

VENKATA SRI HARI
VENKATA SRI HARI

Posted on

Deploy a Dockerized Application on AWS ECS

Deploying a Dockerized application on AWS ECS involves containerizing your application using Docker and leveraging AWS ECS to orchestrate and manage containers. Key steps include building a Docker image, pushing it to a repository, configuring ECS Task Definitions, and setting up ECS Services with load balancing, logging, and scaling. This approach ensures high availability, scalability, and seamless application management.

Image description

1. Prerequisites
AWS Account: Ensure you have an active AWS account.

  • CLI Tools Installed:
  • AWS CLI
  • Docker
  • Terraform (optional, for IaC)
  • Application Code: A simple Dockerized application (e.g., a Node.js or Python web app).
  • Domain Name (Optional): For configuring Route 53.

2. Steps to Implement the Project
Step 1: Build and Push Docker Image:

  1. Write Dockerfile: Create a Dockerfile in your application root directory. Example for a Node.js app:
FROM node:16-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "app.js"]
EXPOSE 3000
Enter fullscreen mode Exit fullscreen mode
  1. Build the Docker Image:
docker build -t my-docker-repo/my-app:latest .
Enter fullscreen mode Exit fullscreen mode

3: Login to Docker Hub (or another registry like AWS ECR):

docker login
Enter fullscreen mode Exit fullscreen mode

4: Push the Docker Image:

docker push my-docker-repo/my-app:latest
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up AWS ECS

  1. Create an ECS Cluster:
aws ecs create-cluster --cluster-name my-ecs-cluster
Enter fullscreen mode Exit fullscreen mode
  1. Create a Task Definition:
    • Go to AWS ECS Console > Task Definitions > Create new Task Definition.
    • Choose Fargate or EC2 as the launch type.
    • Specify the Docker image in the container section (e.g., my-docker-repo/my-app:latest).
    • Set up environment variables, resource limits (e.g., CPU: 256, Memory: 512), and log configuration for CloudWatch.
    • Create an ECS Service:
    • Go to Services > Create Service.
    • Choose your cluster and task definition.
    • Set desired tasks, min/max scaling policies, and attach a load balancer.

Step 3: Set Up CloudWatch Logs

  • Enable Logging in Task Definition: In the task definition, set up logging:
"logConfiguration": {
  "logDriver": "awslogs",
  "options": {
    "awslogs-group": "/ecs/my-app",
    "awslogs-region": "us-east-1",
    "awslogs-stream-prefix": "ecs"
  }
}
Enter fullscreen mode Exit fullscreen mode
  • View Logs in CloudWatch: Go to CloudWatch Console > Log Groups > /ecs/my-app.

Step 4: Set Up Load Balancer

  1. Create an Application Load Balancer (ALB):
  • Go to EC2 Console > Load Balancers > Create Load Balancer.
  • Choose Application Load Balancer.
  • Configure listeners for HTTP/HTTPS.
  • Add target groups for ECS tasks.
  1. Attach ALB to ECS Service:
  • Update the ECS service to use the ALB.
  • Specify the target group and health check path (e.g., /health).

Step 5: Configure Route 53

  1. Create a Hosted Zone Go to Route 53 Console > Hosted Zones > Create Hosted Zone.
  2. Add an A Record: Map the domain (e.g., myapp.example.com) to the ALB.

Step 6: Set Up Auto Scaling

  1. Scaling Policy in ECS:
  • Go to ECS Service > Auto Scaling > Add Scaling Policy.
  • Use metrics like CPU utilization or request count.
  • Scaling Configuration:
  • Minimum Tasks: 1
  • Maximum Tasks: 10
  • Target Utilization: 75% Step 7: Path-Based Routing**
  • Configure Path Rules in ALB:

  • Go to ALB Console > Listeners > Rules.

  • Add path-based routing rules (e.g., /api/* to one service, /web/* to another).

  • Test Routing:

  • Access the domain or ALB DNS to verify path-based routing.

Step 8: Verify Deployment

  • Test the application by accessing the domain name or ALB DNS.
  • Check CloudWatch logs for errors or debugging.
  • Verify scaling by simulating load.

Bonus: Automate with Terraform
You can automate this setup using Terraform. Example for ECS Cluster:

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

resource "aws_ecs_task_definition" "my_task" {
  family                   = "my-app"
  network_mode             = "awsvpc"
  container_definitions    = jsonencode([
    {
      name = "my-app"
      image = "my-docker-repo/my-app:latest"
      essential = true
      memory = 512
      cpu = 256
      logConfiguration = {
        logDriver = "awslogs"
        options = {
          "awslogs-group" = "/ecs/my-app"
          "awslogs-region" = "us-east-1"
          "awslogs-stream-prefix" = "ecs"
        }
      }
    }
  ])
  requires_compatibilities = ["FARGATE"]
  execution_role_arn       = aws_iam_role.ecs_task_execution_role.arn
  cpu                      = "256"
  memory                   = "512"
}
Enter fullscreen mode Exit fullscreen mode

Conclusion
This project demonstrates setting up an AWS ECS service, integrating Docker, ALB, Route 53, and scaling policies for a robust and scalable infrastructure. You can extend it with advanced monitoring, CI/CD pipelines, and enhanced security features.

………………………………………………………………………………………………………………………………………………………………………………………………

You’re welcome! Have a great time ahead! Enjoy your day!

Please Connect with me any doubts.

Mail: sriharimalapati6@gmail.com

LinkedIn: www.linkedin.com/in/

GitHub: https://github.com/Consultantsrihari

Medium: Sriharimalapati — Medium

Thanks for watching ##### %%%% Sri Hari %%%%



`






Enter fullscreen mode Exit fullscreen mode

Top comments (0)