Containers with Amazon ECR and ECS on Fargate
Containers package an application with everything it needs, so it runs the same on a laptop and in the cloud. AWS stores images in Amazon ECR and runs them with Amazon ECS – on servers you manage (EC2) or serverless (Fargate).
From code to running service
flowchart LR
D[Dockerfile] -->|docker build| I[Image notes-api:1.4]
I -->|docker push| R[(Amazon ECR repository)]
R --> TD["Task definition<br/>image · CPU · memory · port · env · roles"]
TD --> S["ECS service (desired = 3)<br/>Fargate · private subnets"]
ALB[Application Load Balancer] --> S
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
aws ecr create-repository --repository-name notes-api
aws ecr get-login-password | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-south-1.amazonaws.com
docker build -t notes-api:1.4 .
docker tag notes-api:1.4 123456789012.dkr.ecr.ap-south-1.amazonaws.com/notes-api:1.4
docker push 123456789012.dkr.ecr.ap-south-1.amazonaws.com/notes-api:1.4
ECS concepts
| Concept | Meaning |
|---|---|
| Cluster | Logical group of capacity (Fargate and/or EC2) |
| Task definition | Versioned blueprint: containers, image, CPU/memory, ports, env vars, secrets, logging, IAM roles |
| Task | A running instance of a task definition |
| Service | Keeps N tasks running, replaces failed ones, rolling deployments, registers with a target group |
| Capacity provider | Fargate, Fargate Spot or an EC2 Auto Scaling group |
Fargate vs EC2 launch type
| Fargate | EC2 | |
|---|---|---|
| Servers | None to manage | You manage instances (AMI, patching, scaling) |
| Pricing | Per vCPU and GB per second | Per instance |
| Best for | Most services, teams without ops capacity | GPUs, special instance types, maximum bin-packing |
Networking and security
- awsvpc mode gives each task its own ENI and private IP – use security groups per service.
- Run tasks in private subnets behind an ALB; pull images through a NAT gateway or VPC endpoints.
- Task execution role – used by ECS to pull images and write logs (
AmazonECSTaskExecutionRolePolicy). - Task role – used by your code to call AWS APIs (e.g. S3, DynamoDB).
- Secrets from Secrets Manager/SSM injected as environment variables.
Don’t confuse the two roles: the execution role is for ECS itself (pull, logs), the task role is for your application.
Operations
- Rolling update or blue/green (CodeDeploy) deployments with health checks.
- Service auto scaling on CPU, memory or ALB requests per target.
- Logs to CloudWatch Logs via the
awslogsdriver; Container Insights for metrics. - ECR features: image scanning, lifecycle policies to delete old images, immutable tags.
Practise it
Virtual lab · Amazon Web Services · 50 min
Containerise an API and run it on Amazon ECS with Fargate
Build a Docker image in CloudShell, test it locally, push it to a private Amazon ECR repository, then run it as an ECS service on Fargate with public tasks, logs in CloudWatch and scaling – no servers to manage.
Quick revision
- Build → tag → push the image to ECR.
- Task definition = blueprint; service keeps tasks running.
- Fargate removes server management; EC2 gives control.
- awsvpc: each task has its own IP and security group.
- Execution role for ECS, task role for your code.
Practise this topic
Hands-on virtual lab
Apply what you just read in a realistic, guided simulation. Free with a verified account.
Create a free account to track your progress and unlock virtual labs.