Stop 12 · Containers on AWSTopics · 4/8

Amazon ECS on Fargate

Part of the AWS Roadmap

In short

You have a Docker image. Now you need it running, restarted when it crashes and scaled when it's busy, without patching servers. ECS on Fargate does exactly that.

Amazon ECS runs containers using task definitions (which image, CPU, memory, ports and environment) and services (how many tasks to keep running). With the Fargate launch type, AWS manages the servers so you only define the containers.

The jaadu with a clipboard watches three identical cooking stations in a bright kitchen and waves in a new cook to replace one who is leaving.

Think of it like this

The cloud kitchen manager

A cloud kitchen in Gurugram has a recipe sheet for each dish: ingredients, cooking time, plate size. The manager's rule is 'always keep three biryani stations running'. If one cook leaves, she brings in another. And she doesn't own the building: the landlord handles electricity and repairs. The task definition is the recipe, the service is the manager, tasks are the stations and Fargate is the landlord.

Watch it happen

How ECS pieces fit together

Cluster: shop
Service: shop-api (desired 3)
Task definition: shop-api:7
Task (AZ a)
Task (AZ b)
Task (AZ c)
Container: api

1/4A cluster is a logical group. With Fargate there are no instances inside to manage.

The big ideas

What you need to know

Task definition

A versioned JSON blueprint. Fargate needs networkMode awsvpc and task-level CPU and memory.

Two roles

The execution role lets ECS pull the image, fetch secrets and write logs. The task role is what your app's code uses to call AWS.

Services and deployments

Rolling deployments replace tasks gradually. The deployment circuit breaker can roll back automatically if new tasks keep failing.

Fargate vs EC2

Fargate: no servers to patch, pay per task. EC2 capacity: cheaper at steady high scale and allows GPUs, but you manage the instances.

See it in code

Walk through the code

json
1{2  "family": "shop-api",3  "requiresCompatibilities": ["FARGATE"],4  "networkMode": "awsvpc",5  "cpu": "256",6  "memory": "512",7  "executionRoleArn": "arn:aws:iam::111122223333:role/ecsTaskExecutionRole",8  "taskRoleArn": "arn:aws:iam::111122223333:role/shop-api-task-role",9  "containerDefinitions": [10    {11      "name": "api",12      "image": "111122223333.dkr.ecr.ap-south-1.amazonaws.com/shop-api:3f9c2ab",13      "portMappings": [{ "containerPort": 8080 }],14      "environment": [{ "name": "APP_ENV", "value": "prod" }],15      "logConfiguration": {16        "logDriver": "awslogs",17        "options": {18          "awslogs-group": "/ecs/shop-api",19          "awslogs-region": "ap-south-1",20          "awslogs-stream-prefix": "api"21        }22      }23    }24  ]25}

1/5The family name groups revisions. Fargate requires awsvpc networking.

Register it with aws ecs register-task-definition --cli-input-json file://taskdef.json. 256 CPU units is a quarter of a vCPU.

Save yourself the pain

Common mistakes

Oops

Giving the execution role your app's S3 and DynamoDB permissions.

Do this instead

App permissions go on the task role. The execution role only needs to pull images, read the secrets it injects and write logs.

Oops

Running tasks in private subnets with no NAT gateway or VPC endpoints, so image pulls time out.

Do this instead

Private tasks need a NAT gateway or VPC endpoints for ECR, S3 and CloudWatch Logs to reach those services.

Your mission

Run a container without a server

~40 min

Quick check

Your container needs to read from an S3 bucket. Which role gets that permission?

Go deeper

  1. 1Write a task definition for your container image with a CloudWatch log configuration.
  2. 2Create an ECS service on Fargate behind an ALB with two tasks.
  3. 3Update the image tag and watch a rolling deployment replace the tasks.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge