Blog

EN
AWS Elastic Container Service (ECS)

AWS Elastic Container Service (ECS)

8 min read

Series: The Road to AWS

2 parts

What is Amazon ECS and how to deploy a containerized application using AWS Fargate, an Application Load Balancer (ALB), and API Gateway. Step by step on AWS.

What is Elastic Container Service (ECS)?

Amazon Elastic Container Service (ECS) is a container orchestration service. It allows you to deploy, manage, and scale containerized applications (Docker) easily. ECS takes care of placing containers on the appropriate infrastructure, managing their lifecycle, and ensuring the application is available very easily.

What is it used for?

ECS eliminates the complexity of manually managing container orchestration software. It serves us for:

  • Scalability: Automatically increase or decrease the number of containers based on demand.
  • High Availability: Run the application across multiple Availability Zones to prevent downtime.
  • Simplified Management: Integrates natively with other AWS services such as Load Balancers (ALB), Security (IAM), and Monitoring (CloudWatch).

Creating an Elastic Container Service (ECS)

Go to the Amazon Elastic Container Service in the AWS console:

Elastic Container Service (ECS) AWS

1. Create the "Task Definition"

The Task Definition is the blueprint for the application; it defines which Docker image to use, how much memory it needs, and how it should behave.

  1. In the side menu, go to Task Definitions > Create new task definition.
  2. Task configuration:
  • Family name: Assign a name, for example: task-name.
  • Launch Type: Select AWS Fargate (serverless management).
  • OS/Architecture: Linux/X86_64.
  • Task Size: Define the amount of CPU and RAM required for your container.

Create Task in ECS de AWS

  1. Container Details
  • Name: container-name.
  • Image URI: Paste your image URI (from ECR or Docker Hub).
  • Container Port: Specify the port you exposed in your Dockerfile (e.g., 3000, 8080).

Container configuration in ECS de AWS

  1. (Optional) Define environment variables if your application requires them and finish the creation.

2. Create the Cluster

The cluster is the logical grouping of resources where your tasks will run.

  1. Go to Clusters > Create Cluster.
  2. Cluster name: test-cluster.
  3. Infrastructure: Ensure AWS Fargate (serverless) is checked.
  4. Click Create.

Create cluster in ECS AWS

3. Create the Service and Load Balancer

In this step, we will link the container with the network and the load balancer to expose it to the internet.

  1. Enter the cluster you just created.
  2. In the Services tab, click Create.
  3. Service details:
    1. Select the "Task Definition" (Family) you created in step 1.

Create service in ECS AWS

  1. Deployment configuration:

    1. Desired tasks: (At least 2 is recommended to guarantee high availability and test load balancing).
  2. Networking:

    1. Security Group: Create a new one.

Open your app's port (Custom TCP) and allow traffic from "Anywhere" (0.0.0.0/0) temporarily (we will later restrict it to the ALB).

Configure service networking in AWS ECS

  1. Load balancing: Enable this option to distribute traffic.
    1. Load balancer type: Select Application Load Balancer (ALB).
    2. Load balancer name: Enter a name (e.g., api-alb). AWS will create the resource automatically.
    3. Container to load balance: Select your container and the previously configured port.
    4. Target group: Let it create a new one (e.g., create new).
    5. Listener: Port 80 (HTTP).

Configure service load balancer on AWS ECS

Configure the service target group in AWS ECS

  1. Verify deployment:
    1. Go to the EC2 > Load balancers console.
    2. Select the created load balancer (ALB).
    3. Copy the DNS name, paste it into your browser, and verify that your application responds.

4. The API Gateway

To add a layer of management, security, and routing, we will place an API Gateway in front of the load balancer.

  1. Go to the API Gateway console.
  2. Select HTTP API (it is cheaper and faster than REST API for this case) or REST API.
  3. Create integration:
    1. Integration type: HTTP.
    2. Endpoint URL: Use the Load Balancer (ALB) DNS name.

API Gateway AWS

  1. Configure routes:
    1. Define the routes your API will handle (e.g., POST /users, GET /products).

API Gateway configure AWS routes

  1. Deploy
    1. Create a Stage named prod or v1.

API Gateway create stage AWS

  1. Go to Deploy > Stages in the left menu. There you will find the Invoke URL. This is the final public URL to access your API.

API Gateway Stages AWS

The final architecture is: User -> API Gateway -> ALB -> ECS Fargate -> RDS.

Share this article on

Avatar byandrev

Andres Parra

Software Engineer

I'm Andres Parra, Software Engineer passionate about developing scalable and innovative technological solutions. I specialize in building modern web applications, mastering a versatile stack that includes JavaScript, TypeScript, Python, and Java, along with frameworks like React, Next.js, and Spring Boot. I'm also interested in the latest technologies and tools for development.