Overview
ECS (Elastic Container Service) launches Docker containers on AWS. A launched container is an ECS Task running on an ECS Cluster. There are two launch types: EC2 and Fargate. ECS Managed Instances is a newer option where AWS manages the EC2 instances for ECS.
ECS Managed Instances means AWS manages the EC2 instances for ECS. The course lists only the EC2 and Fargate launch types. AWS source
Launch Types
| Aspect | EC2 Launch Type | Fargate Launch Type |
|---|---|---|
| Infrastructure | You provision and maintain the EC2 instances in the cluster | No instances to manage, serverless (servers exist behind the scenes) |
| Agent | Each instance must run the ECS Agent, which registers it into the ECS service and the specified cluster | None to manage |
| Running tasks | AWS starts/stops containers; tasks are placed automatically on the pre-provisioned instances | You create a task definition; AWS runs the tasks based on the CPU and RAM you need, without you knowing where |
| Scaling | Cluster of EC2 instances to maintain; see 03-ECS-Auto-Scaling | Increase the number of tasks, no more EC2 instances to manage |
The exam loves telling you to use Fargate: it is serverless and way easier to manage than the EC2 launch type.
ECS EC2 vs Fargate launch type
IAM Roles for ECS
| Role | Details |
|---|---|
| EC2 Instance Profile | Only valid for the EC2 launch type. Used by the ECS Agent only to call the ECS service, send container logs to CloudWatch Logs, pull images from ECR, and reference sensitive data in Secrets Manager or SSM Parameter Store |
| ECS Task Role | Valid for both EC2 and Fargate. A specific role per task, e.g. Task A role for S3 API calls, Task B role for DynamoDB. Defined in the task definition of the ECS service |
Know the distinction between the EC2 Instance Profile Role and the ECS Task Role.
EC2 Instance Profile vs ECS Task Role
Load Balancer Integrations
To expose tasks as an HTTP or HTTPS endpoint, run a load balancer in front; users go to the load balancer and traffic reaches the ECS tasks directly. Works with EC2 launch type or Fargate.
| Load balancer | Use |
|---|---|
| Application Load Balancer | Supports most use cases, a good choice, works with Fargate. See ALB |
| Network Load Balancer | Recommended only for very high throughput / high performance use cases, or with AWS PrivateLink. See NLB |
| Classic Load Balancer | Can be used but not recommended: no advanced features and cannot be linked to Fargate |
Load balancers in front of ECS
Data Persistence with EFS
Data volumes persist data; the notable one is Amazon EFS. See EFS.
- EFS is a network file system, compatible with both EC2 and Fargate launch types, mounted directly onto ECS tasks.
- Tasks in any AZ linked to the file system share the same data and can communicate through it.
- Fargate + EFS is the ultimate serverless combo: EFS is serverless, pay as you go, provisioned in advance.
- Use case: persistent multi-AZ shared storage for containers.
ECS and Fargate tasks can attach EBS volumes, one per task, so EFS is no longer the only persistent option. Fargate ephemeral storage is 20 GiB by default, up to 200 GiB. AWS source
- Amazon EBS volumes can also attach to ECS and Fargate tasks, one volume per task (not shared). Fargate ephemeral storage is 20 GiB by default, up to 200 GiB.
Hands-On Walkthrough
Create the cluster
- In the ECS console, enable the new ECS experience (top left). Go to Clusters and Create cluster.
- Name it
DemoCluster, keep the default namespace. - Infrastructure has three options:
- AWS Fargate (serverless, AWS provides compute on demand) - left enabled.
- Amazon EC2 instances (you provide the compute) - also enabled for the demo.
- External instances using ECS Anywhere (e.g. your own data center).
- For EC2: create a new Auto Scaling group, OS Amazon Linux 2 (or newer Amazon Linux 2023), instance type
t2.micro(free tier eligible), desired capacity minimum 0 and maximum 5, no SSH key pair, default root EBS volume size. - Network: default VPC, the three available subnets, the default security group, auto-assign public IP set to use subnet setting. Leave monitoring and tags. Click Create.
- In EC2 Auto Scaling groups, an ASG
Infra-ECS-Clusteris created by the cluster with desired 0, min 0, max 5, across 3 AZs, so tasks launch across 3 AZs. - In
DemoCluster: services 0, tasks 0. Under Infrastructure there are three capacity providers:- FARGATE: launch Fargate tasks.
- FARGATE_SPOT: launch Fargate tasks in spot mode.
- ASGProvider: launch EC2 instances in the cluster through the ASG (managed scaling; current size 0, can be changed).
- Demo: edit the ASG desired capacity to 1. An EC2 instance is created and registers itself in
DemoClusteras a container instance. It showst2.micro, 0 tasks running, 1,024 CPU and 982 memory available. - A task can run on FARGATE, FARGATE_SPOT, or the container instances from the ASG.
Create the task definition
- Create a new task definition named
nginxdemos-hello, from the Docker Hub imagenginxdemos/hello. - Infrastructure: Fargate (serverless), OS Linux. Task size: 0.5 vCPU and 1 GB memory chosen for low cost. The console allows up to 16 vCPU and a large memory amount (the speaker says "120 gigabytes", partly unclear in the transcript).
- Task role: an IAM role for API calls to AWS services. None here, but of utmost importance if containers use AWS.
- Task execution role: leave default; ECS creates it if missing.
- Container: name
nginxdemos-hello, image URLnginxdemos/hello(pulled from Docker Hub), port mapping 80 to 80. Other options (resource limits, environment variables, logging) left as default. - Storage: Fargate ephemeral storage default of 21 GB. Click Create; the version shows as 1 (the speaker saw 2 because he created it twice).
Create the service
Clusters>DemoCluster> Services > Create. Compute option: Launch type Fargate, platform version latest.- Application type Service (long running, e.g. web app). Use Task for something that terminates, e.g. a batch job.
- Select the
nginxdemos-hellofamily and latest revision; service namenginxdemos-hello; service type Replica with 1 task. Deployment options and failure detection left as is. - Networking: VPC with the three subnets; new security group
nginxdemos-helloallowing HTTP port 80 from anywhere; public IP on. - Load balancing: an Application Load Balancer named
DemoALBForECS, health check grace period 0, containernginxdemos-helloport 80 protocol HTTP, target grouptg-nginxdemos-hello, HTTP health check path/. - Service auto scaling and task placement left untouched (available). Wait a few minutes for deployment.
Verify
- Service shows 1 desired, 1 running, status active, linked to the target group and the ALB; one IP (the container's private IP) is registered as a target.
- Opening the ALB DNS name shows the Nginx welcome page; the server address equals the registered IP.
/testalso works. - The task page shows configuration, revision, private IP, containers and logs. The service Events tab shows task started, registered in target group, deployment complete, steady state.
Scale up
- Update service, desired tasks = 3 (one per AZ). Tasks go pending, activating, running quickly on Fargate. Refreshing the ALB page shows the IP changing, so the ALB distributes load across containers.
Clean Up
- Make sure the service has 0 tasks (update desired tasks to 0 if not), then Delete Service and type
Delete. - Deleting the service runs through CloudFormation, which deletes the ECS service, the load balancer listener, the load balancer, the security group and the target groups. This takes some time.
- When the service is gone, Delete Cluster. CloudFormation deletes the capacity provider, the Auto Scaling group, the cluster and the launch templates.
- Task definitions cost nothing and can be left. Optionally select one and use Actions > Deregister.
- During the demo the ASG desired capacity was also set to 0 so no EC2 instances keep running.
Questions & Answers
Q1: EC2 vs Fargate launch type?
Answer:
EC2: you provision and maintain instances running the ECS Agent. Fargate: serverless, define a task definition with CPU and RAM and AWS runs it. The exam favors Fargate.
Q2: Instance Profile vs Task Role?
Answer:
The EC2 Instance Profile is for the ECS Agent (EC2 launch type only: ECS API, CloudWatch Logs, ECR, Secrets Manager/SSM). The ECS Task Role is per task, works on EC2 and Fargate, and is set in the task definition.
Q3: Which load balancer for ECS?
Answer:
ALB for most cases; NLB for very high throughput or PrivateLink; Classic is not recommended and does not work with Fargate.
Q4: How do ECS tasks share persistent data across AZs?
Answer:
Mount an EFS file system, which works with both EC2 and Fargate. An EBS volume attaches to one task only, so it does not share data across tasks.
EBS can attach to a task (one per task), but only EFS shares data across tasks and AZs. AWS source
Q5: What capacity providers appear in a cluster?
Answer:
FARGATE, FARGATE_SPOT and an ASG provider for EC2 instances.
Q6: Why does deleting an ECS service take time?
Answer:
CloudFormation deletes the whole stack (service, listener, load balancer, security group, target groups).
Check yourself
Quiz
Flashcards
Click the card (or focus it), then use ← → to move and Space to flip.