DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

AWS ECS Lab

Deploy Nginx Container Using Amazon ECS (Fargate)

Objective

In this lab we will deploy a Docker container to Amazon ECS (Elastic Container Service) using AWS Fargate and access it from a web browser.


What is Amazon ECS

Amazon ECS (Elastic Container Service) is a container orchestration service that allows you to run and scale Docker containers on AWS.

It manages:

  • container deployment
  • scaling
  • networking
  • load balancing
  • monitoring

ECS can run containers using two compute options:

Option Description
Fargate Serverless containers (AWS manages servers)
EC2 You manage EC2 instances

In this lab we use:

AWS Fargate
Enter fullscreen mode Exit fullscreen mode

because AWS manages the infrastructure.


Architecture of This Lab

Internet
   ↓
Public IP
   ↓
ECS Service
   ↓
Fargate Task
   ↓
Docker Container (nginx)
Enter fullscreen mode Exit fullscreen mode

Components used:

  • ECS Cluster
  • Task Definition
  • ECS Service
  • Fargate Compute
  • Docker Container

Step 1 — Open ECS

  1. Log in to AWS Console
  2. In the search bar type:
ECS
Enter fullscreen mode Exit fullscreen mode
  1. Open:
Elastic Container Service
Enter fullscreen mode Exit fullscreen mode

Step 2 — Create ECS Cluster

Clusters are logical groups where containers run.

Go to:

Clusters
Enter fullscreen mode Exit fullscreen mode

Click:

Create Cluster
Enter fullscreen mode Exit fullscreen mode

Fill the configuration:

Cluster name

jump-ecs-cluster
Enter fullscreen mode Exit fullscreen mode

Infrastructure

Fargate only
Enter fullscreen mode Exit fullscreen mode

Click:

Create
Enter fullscreen mode Exit fullscreen mode

AWS will create the cluster.


Step 3 — Create Task Definition

A Task Definition is a blueprint that tells ECS:

  • what container image to run
  • how much CPU and memory to use
  • what ports to open

Go to:

Task definitions
Enter fullscreen mode Exit fullscreen mode

Click:

Create new task definition
Enter fullscreen mode Exit fullscreen mode

Configure the following.


Task Definition Configuration

Task definition family

jump-nginx-task
Enter fullscreen mode Exit fullscreen mode

Launch type

AWS Fargate
Enter fullscreen mode Exit fullscreen mode

Operating system

Linux / X86_64
Enter fullscreen mode Exit fullscreen mode

Network mode

awsvpc
Enter fullscreen mode Exit fullscreen mode

Task Size

Set the resources for the container.

CPU

0.25 vCPU
Enter fullscreen mode Exit fullscreen mode

Memory

0.5 GB
Enter fullscreen mode Exit fullscreen mode

Task Execution Role

Click:

Create default role
Enter fullscreen mode Exit fullscreen mode

AWS will create:

ecsTaskExecutionRole
Enter fullscreen mode Exit fullscreen mode

This role allows ECS to:

  • pull container images
  • send logs to CloudWatch

Step 4 — Add Container

Scroll to Container configuration.

Container name

nginx-container
Enter fullscreen mode Exit fullscreen mode

Image URI

nginx
Enter fullscreen mode Exit fullscreen mode

This pulls the public Docker image.


Port Mapping

Add port mapping.

Container port

80
Enter fullscreen mode Exit fullscreen mode

Protocol

TCP
Enter fullscreen mode Exit fullscreen mode

Step 5 — Enable Logging

Enable log collection.

Destination

CloudWatch
Enter fullscreen mode Exit fullscreen mode

Step 6 — Create Task Definition

Click:

Create
Enter fullscreen mode Exit fullscreen mode

The task definition is now saved.

Important:

A task definition does NOT run a container.

It is only a template.


Step 7 — Create ECS Service

Now we run the container.

Go to:

Clusters
Enter fullscreen mode Exit fullscreen mode

Open your cluster.

jump-ecs-cluster
Enter fullscreen mode Exit fullscreen mode

Click:

Create Service
Enter fullscreen mode Exit fullscreen mode

Service Configuration

Launch type

FARGATE
Enter fullscreen mode Exit fullscreen mode

Task definition

jump-nginx-task
Enter fullscreen mode Exit fullscreen mode

Service name

nginx-service
Enter fullscreen mode Exit fullscreen mode

Desired tasks

1
Enter fullscreen mode Exit fullscreen mode

Step 8 — Configure Networking

Select:

VPC

Default VPC
Enter fullscreen mode Exit fullscreen mode

Subnets

Public subnets
Enter fullscreen mode Exit fullscreen mode

Enable:

Auto assign public IP
Enter fullscreen mode Exit fullscreen mode

Security Group rule:

HTTP
Port 80
Source 0.0.0.0/0
Enter fullscreen mode Exit fullscreen mode

Step 9 — Deploy Service

Click:

Create Service
Enter fullscreen mode Exit fullscreen mode

AWS will now:

  • pull nginx image
  • start the container
  • attach networking
  • assign public IP

Deployment takes about 30–60 seconds.


Step 10 — Find Public IP

Go to:

Cluster
→ Service
→ Tasks
Enter fullscreen mode Exit fullscreen mode

Click the Task ID.

Scroll to Networking.

You will see:

Public IP
Enter fullscreen mode Exit fullscreen mode

Example:

18.188.200.132
Enter fullscreen mode Exit fullscreen mode

Step 11 — Open in Browser

Open your browser and type:

http://PUBLIC-IP
Enter fullscreen mode Exit fullscreen mode

Example:

http://18.188.200.132
Enter fullscreen mode Exit fullscreen mode

You should see:

Welcome to nginx
Enter fullscreen mode Exit fullscreen mode

This means the container is working.


What We Built

We deployed a Docker container using ECS.

Final architecture:

Internet
   ↓
Public IP
   ↓
ECS Service
   ↓
Fargate Task
   ↓
nginx container
Enter fullscreen mode Exit fullscreen mode

Important DevOps Notes

In production environments we usually use:

Internet
   ↓
Application Load Balancer
   ↓
ECS Service
   ↓
Multiple Fargate Tasks
   ↓
Containers
Enter fullscreen mode Exit fullscreen mode

Advantages:

  • high availability
  • auto scaling
  • zero downtime deployments
  • DNS instead of IP

Key ECS Components

Component Purpose
Cluster logical environment for containers
Task Definition container blueprint
Task running container
Service keeps tasks running
Fargate serverless container compute

Top comments (0)