DEV Community

Cover image for How to Deploy a Multi-Container App in Amazon ECS?
Koti Vellanki
Koti Vellanki

Posted on

How to Deploy a Multi-Container App in Amazon ECS?

Real-World Examples and Step-by-Step Implementation 🚀


Welcome to Day 5 of our 15-day AWS Containers learning series! Yesterday, we explored Amazon Elastic Container Registry (ECR) and learned how to securely store and manage container images. Today, we’ll take a big leap forward by deploying a multi-container application in Amazon ECS (Elastic Container Service).

By the end of this blog, you’ll:

  • Understand the architecture of multi-container applications.
  • Learn how to deploy 5 real-world multi-container apps in ECS.
  • Gain hands-on experience with step-by-step implementation for each example.
  • Explore advanced patterns like sidecars, microservices, and event-driven architectures.

Let’s dive in and master the art of deploying multi-container applications in ECS!


Table of Contents

  1. The Story: Ovi Learns Teamwork
  2. What Is a Multi-Container Application?
  3. Key Components of a Multi-Container App in ECS
  4. Real-World Examples with Step-by-Step Implementation
    1. Example 1: Frontend-Backend Deployment
    2. Example 2: Microservices Architecture
    3. Example 3: Sidecar Pattern for Logging
    4. Example 4: Event-Driven Architecture
    5. Example 5: Blue/Green Deployment
  5. Troubleshooting Tips
  6. Summary: Key Takeaways
  7. What’s Next?
  8. Let’s Connect!

The Story: Ovi Learns Teamwork

Ovi is back, and this time she’s building a robot with her friends. Each friend is responsible for a different part: one builds the arms, another the legs, and Ovi programs the brain. But they quickly realize they need a way to coordinate their efforts.

Her dad explains:

“Ovi, your robot is like a multi-container application. Each part of the robot is like a container, and they all need to work together to make the robot function. Amazon ECS helps you manage and deploy these containers as a team!”

Ovi smiles and says, “So ECS is like the project manager for my robot?” Her dad nods, “Exactly! It ensures all the parts work together seamlessly.”


What Is a Multi-Container Application?

A multi-container application is an application composed of multiple containers, each responsible for a specific function. These containers work together to deliver the full functionality of the application.

For example, a typical web application might include:

  • A frontend container for the user interface.
  • A backend container for business logic.
  • A database container for storing data.

Key Components of a Multi-Container App in ECS (with Examples)


1. Task Definition

A Task Definition is like a blueprint for your application. It defines the containers that make up your application, their configurations (e.g., CPU, memory, ports), and how they interact.

Example:

Imagine you're deploying a blogging platform.

  • Frontend Container: Runs a React-based UI for users to interact with.
  • Backend Container: Runs a Node.js API to handle requests from the frontend.
  • Database Container: Runs a MySQL database to store blog posts and user data.

In the Task Definition, you specify:

  • The frontend container listens on port 80.
  • The backend container listens on port 8080.
  • The database container listens on port 3306.

This ensures all containers are configured correctly and can work together.


2. Service

A Service ensures that the desired number of tasks (containers) are running and manages scaling. It also integrates with load balancers to distribute traffic.

Example:

Let's say you're running an e-commerce website.

  • You want 3 instances of your frontend container to handle user traffic.
  • You want 2 instances of your backend container to process orders.

The ECS Service ensures that these containers are always running. If one container crashes, the Service automatically replaces it. Additionally, if traffic increases during a sale, you can configure the Service to scale up the number of containers.


3. Load Balancer

A Load Balancer distributes traffic across multiple containers to ensure high availability and reliability.

Example:

Consider a video streaming platform like YouTube.

  • Users upload and watch videos through a frontend container.
  • The backend container processes video uploads and streams.

A Load Balancer ensures that:

  • User requests are evenly distributed across multiple frontend containers.
  • Backend containers handle video processing without being overwhelmed.

If one container becomes unhealthy, the Load Balancer automatically routes traffic to healthy containers.


4. Service Discovery

Service Discovery allows containers to discover and communicate with each other without hardcoding IP addresses or endpoints.

Example:

Imagine you're building a microservices-based weather app.

  • A weather data service fetches real-time weather data.
  • A notification service sends alerts to users based on weather conditions.

With Service Discovery, the notification service can dynamically find and communicate with the weather data service, even if the IP address changes. This is especially useful in dynamic environments like ECS, where containers are frequently created and destroyed.


5. CloudWatch Logs

CloudWatch Logs captures logs from your containers for monitoring and troubleshooting.

Example:

Suppose you're running a chat application.

  • The frontend container logs user interactions (e.g., messages sent).
  • The backend container logs API requests and responses.

By sending these logs to CloudWatch Logs, you can:

  • Monitor user activity in real time.
  • Troubleshoot issues like failed API requests or slow response times.
  • Set up alerts for unusual activity, such as a sudden spike in errors.

Summary of Examples:

  • Task Definition: Blogging platform with frontend, backend, and database containers.
  • Service: E-commerce website with scalable frontend and backend containers.
  • Load Balancer: Video streaming platform distributing traffic across containers.
  • Service Discovery: Microservices-based weather app with dynamic communication.
  • CloudWatch Logs: Chat application logging user interactions and API activity.

Real-World Examples with Step-by-Step Implementation

Here are 5 real-world examples of multi-container applications, each with step-by-step instructions for deployment in ECS.


Example 1: Frontend-Backend Deployment

Scenario:

A startup wants to deploy a web application with a React frontend and a Node.js backend.


Step-by-Step Implementation:

  1. Push Images to ECR: Build and push the frontend and backend Docker images to Amazon ECR.
   # Build and push frontend image
   docker build -t frontend-app .
   docker tag frontend-app:latest <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/frontend-app:latest
   aws ecr get-login-password --region <REGION> | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com
   docker push <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/frontend-app:latest

   # Build and push backend image
   docker build -t backend-app .
   docker tag backend-app:latest <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/backend-app:latest
   docker push <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/backend-app:latest
Enter fullscreen mode Exit fullscreen mode
  1. Create an ECS Cluster:

    • Go to the ECS Console.
    • Click Create Cluster → Select Networking only (Fargate).
    • Name the cluster (e.g., frontend-backend-cluster) and click Create.
  2. Define a Task Definition:

    • Navigate to Task Definitions → Click Create new Task Definition → Select Fargate.
    • Add two containers:
      • Frontend Container:
      • Image: <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/frontend-app:latest
      • Port: 80
      • Backend Container:
      • Image: <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/backend-app:latest
      • Port: 8080
    • Save the task definition.
  3. Create a Service:

    • Navigate to Services → Click Create Service.
    • Select the cluster and task definition.
    • Configure the desired number of tasks (e.g., 2).
    • Click Create Service.
  4. Configure a Load Balancer:

    • Navigate to Load Balancers in the EC2 Console.
    • Create an Application Load Balancer (ALB).
    • Configure listeners and target groups for the frontend and backend containers.
    • Associate the load balancer with your ECS service.
  5. Test the Application:

    • Access the application using the ALB DNS name.
    • Verify that the frontend communicates with the backend.

Example 2: Microservices Architecture

Scenario:

A company wants to deploy a microservices-based e-commerce application with separate services for the product catalog, user authentication, and payment processing.


Step-by-Step Implementation:

  1. Push Microservices Images to ECR:

    Repeat the steps from Example 1 to build and push Docker images for each microservice.

  2. Create an ECS Cluster:

    • Create a new ECS cluster (e.g., microservices-cluster).
  3. Define Task Definitions for Each Microservice:

    • Create separate task definitions for the product catalog, user authentication, and payment processing services.
    • Specify the container images, ports, and resource requirements for each service.
  4. Create ECS Services for Each Microservice:

    • Create separate ECS services for each microservice.
    • Configure the desired number of tasks for each service.
  5. Configure Service Discovery:

    • Enable service discovery for each ECS service.
    • Use the service discovery endpoints to allow microservices to communicate.
  6. Test the Application:

    • Verify that the microservices can communicate using service discovery.
    • Test the end-to-end functionality of the application.

Example 3: Sidecar Pattern for Logging

Scenario:

A DevOps team wants to collect logs from an application using a sidecar container running Fluentd.


Step-by-Step Implementation:

  1. Push Application and Fluentd Images to ECR:

    Build and push the application and Fluentd Docker images to ECR.

  2. Define a Task Definition with a Sidecar Container:

    • Add the main application container.
    • Add a sidecar container for Fluentd:
      • Image: fluent/fluentd:latest
      • Mount the application logs directory as a volume.
  3. Deploy the Task in ECS:

    • Create a service using the task definition.
  4. Monitor Logs in CloudWatch:

    • Configure Fluentd to send logs to CloudWatch.
    • Verify that logs are being collected.

Troubleshooting Tips

  1. Task Fails to Start:

    • Check the task definition for errors.
    • Verify that the container images are available in ECR.
  2. Containers Can’t Communicate:

    • Ensure that service discovery is enabled.
    • Check the security group rules for the ECS tasks.

Summary: Key Takeaways

  • Multi-container applications allow you to separate concerns, scale independently, and improve resilience.
  • Amazon ECS simplifies the deployment and management of multi-container apps with features like Task Definitions, Services, and Load Balancers.
  • By following best practices and troubleshooting tips, you can confidently deploy complex applications in ECS.

What’s Next?

In Day 6, we’ll explore scaling with Karpenter on EKS, where you’ll learn how to automate scaling in Kubernetes clusters. Stay tuned for an exciting deep dive into scaling strategies!


Let’s Connect!

Found this helpful? Share it with your network!

See you in the next episode! 🚀

Top comments (0)