Amazon ECS
ECS clusters, tasks and services, the EC2, Fargate and Managed Instances capacity options, IAM roles for ECS, load balancer integration, and shared data volumes with EFS and S3 Files.
What it is
Amazon ECS (Elastic Container Service) runs Docker containers on AWS. Launching containers on ECS means launching ECS tasks on ECS clusters:
- A task definition describes one or more containers: the image, CPU and memory, ports, environment variables, IAM role, volumes.
- A task is a running copy of a task definition.
- A service keeps a set number of tasks running (replacing failed ones) and can put them behind a load balancer.
- A cluster is the group of capacity the tasks run on.
Where tasks run
EC2 launch type
- The customer provisions and maintains the infrastructure: the EC2 instances.
- Each instance runs the ECS agent, which registers it in the ECS cluster.
- AWS takes care of starting and stopping containers on them.
Fargate launch type
- Launch Docker containers on AWS without provisioning any infrastructure: no EC2 instances to manage.
- It's all serverless. The customer only creates task definitions, and AWS runs the tasks based on the CPU and RAM requested.
- To scale, just increase the number of tasks. No more EC2 instances to think about.
- Fargate Spot runs fault-tolerant tasks on spare capacity at a discount.
ECS Managed Instances
- AWS manages the EC2 instances: provisioning, scaling and security patching, using AWS-owned AMIs.
- Unlike Fargate, it offers a choice from a wide range of EC2 instance types (for example GPU instances).
- No SSH and no custom AMIs; instances are replaced at least every 14 days to keep them patched.
AWS Fargate is the default answer when a question says "no servers to manage". The EC2 launch type gives the most control (custom AMIs, specific instances, Reserved Instances or Savings Plans on the hosts).
IAM roles for ECS
- EC2 instance profile (EC2 launch type only), used by the ECS agent to:
- make API calls to the ECS service;
- send container logs to CloudWatch Logs;
- pull Docker images from ECR;
- reference sensitive data in Secrets Manager or SSM Parameter Store.
- ECS task role:
- gives each task its own permissions, for the application inside it;
- allows different roles for different ECS services;
- is defined in the task definition.
AWS's documentation also separates a task execution role, used by ECS and Fargate to pull images, write logs and fetch secrets for the task. On Fargate there's no instance profile, so the execution role does that job.
Load balancer integration
- Application Load Balancer: supported and works for most use cases. With dynamic host port mapping, several tasks of the same service can run on one EC2 instance, each on its own port, and the ALB finds them all. Path-based rules let several services share one ALB.
- Network Load Balancer: recommended only for high throughput / high performance use cases, or to pair with AWS PrivateLink.
- Classic Load Balancer: works, but not recommended (no advanced features, no Fargate).
Data volumes
Containers lose their local files when they stop. For persistent, shared data:
- Amazon EFS:
- mount EFS file systems onto ECS tasks;
- works with the EC2 and Fargate launch types (and Managed Instances);
- tasks running in any AZ share the same data;
- Fargate + EFS = serverless;
- use case: persistent multi-AZ shared storage for the containers.
- Amazon S3 Files:
- mount an S3 bucket exposed as a file system onto ECS tasks;
- changes sync automatically to the S3 bucket;
- use case: containers that need file access to data that already lives in S3.
Updated since the slides
The slides say S3 Files works with Fargate and ECS Managed Instances but not the EC2 launch type. The AWS documentation now lists the EC2 launch type as fully supported too.
Real-world examples
- A media start-up runs its API as an ECS service on Fargate behind an ALB: no servers to patch, and it scales by adding tasks.
- A machine learning team runs inference containers on ECS Managed Instances to use GPU instance types without managing the hosts.
- A CMS runs several WordPress tasks across AZs, all sharing uploads on EFS.
Exam tips
- "Run containers without managing servers" → ECS on Fargate.
- "Need specific instance types, custom AMIs, or full control of hosts" → EC2 launch type (or Managed Instances for specific types without host management).
- "Give one container access to S3 and another to DynamoDB" → separate ECS task roles.
- "ECS tasks need to pull images from ECR and write logs" → instance profile (EC2) or task execution role.
- "Several tasks on one instance behind a load balancer" → ALB with dynamic host port mapping.
- "Persistent storage shared by Fargate tasks across AZs" → EFS.
Common traps
- "Put the app's S3 permissions on the EC2 instance profile." Use task roles, so each task gets only what it needs.
- "Fargate tasks can use EBS-style local persistent disks across restarts." Use EFS (or S3 Files) for persistent data.
- "NLB is the default for ECS web apps." ALB is; NLB is for high throughput or PrivateLink.
Hands-on lab: a Fargate task
Goal: run a public container image as an ECS task on Fargate and open it in a browser.
Costs money
A task with 0.25 vCPU and 0.5 GB on Fargate costs about $0.01 an hour, plus $0.005 an hour for its public IP. Stop it within the hour. Any Region works.
Create a cluster
ECS → Clusters → Create cluster. Name saa-cluster, infrastructure AWS Fargate (serverless) only. Create.
Create a task definition
Task definitions → Create new task definition. Family saa-web, launch type AWS Fargate, CPU 0.25 vCPU, memory 0.5 GB. Container: name web, image public.ecr.aws/nginx/nginx:latest, port 80. Create.
Run the task
In saa-cluster → Tasks → Run new task. Launch type Fargate, task definition saa-web. Under Networking, use the default VPC, a public subnet, Public IP: turned on, and a security group allowing HTTP (80) from anywhere. Create.
Open it
When the task is Running, open it and copy its Public IP. Browse to http://<public-ip>: the nginx welcome page. No EC2 instance was ever created.
Expected result: a container running serverless, reachable from the internet.
Clean up: stop the task, delete the cluster, deregister the task definition revision, and delete the security group if you created one.
AWS documentation
Containers
What Docker containers are, how they differ from virtual machines, how images are built and stored, and the AWS container services in this group.
ECS scaling and patterns
ECS Service Auto Scaling, scaling the EC2 capacity underneath with Auto Scaling groups and capacity providers, and event-driven ECS patterns with EventBridge and SQS.