Running Containers with Amazon ECS
Amazon Elastic Container Service (Amazon ECS) is AWS's own container orchestrator. It runs containers as tasks, keeps services at their desired count and integrates with load balancers, IAM, CloudWatch and the deployment tools covered earlier. This lesson explains clusters, task definitions and their key settings, services, the compute options from EC2 to AWS Fargate, capacity providers and service-to-service communication.
Learning outcomes
After completing this lesson, you will be able to:
- Describe ECS clusters, task definitions, tasks and services and how they relate.
- Explain the important task definition settings: network mode, task role and task execution role, logging and health checks.
- Compare the EC2, Fargate and ECS Managed Instances compute options and explain capacity provider strategies.
- Describe how services are exposed and discovered with load balancers, Service Connect and service discovery.
The ECS Resource Model
ECS organises containers with a few resources. A cluster is a logical grouping of capacity and services. A task definition is a versioned JSON template describing one or more containers that run together: images, CPU and memory, ports, environment variables, secrets, logging and IAM roles. A task is a running instance of a task definition. A service keeps a desired number of tasks running, replaces tasks that stop or fail health checks, registers them with load balancer target groups and manages deployments. Standalone tasks, started with RunTask or by EventBridge Scheduler, suit batch jobs that run to completion.
Key Task Definition Settings
Several task definition settings appear repeatedly in operations scenarios.
Network mode
awsvpc gives each task its own elastic network interface, private IP address and security groups, which allows security groups per service; it is required for Fargate. bridge and host modes share the instance's network on the EC2 launch type. With awsvpc, each task consumes a subnet IP address, which affects subnet sizing.
Task role and task execution role
The task execution role is used by the ECS agent and Fargate to pull images from ECR, fetch secrets referenced in the task definition and send logs to CloudWatch Logs. The task role is assumed by the application code inside the containers for its own AWS calls, such as reading from S3. Confusing them is a frequent cause of failures, as noted in the artifacts and secrets lessons.
Logging
The awslogs log driver sends container output to CloudWatch Logs. FireLens uses a Fluent Bit sidecar to route logs to multiple destinations, such as CloudWatch Logs, Amazon OpenSearch Service, S3 through Firehose or third-party tools, with filtering and parsing.
Health checks and dependencies
Container health checks, described in the testing module, report whether each container is healthy. essential marks containers whose exit stops the task, and dependsOn orders container start-up, for example starting a sidecar before the application.
Resources and placement
Task-level CPU and memory are required on Fargate and determine billing. Container-level reservations and limits share task resources between containers.
ECS task settings
Compute Options
ECS runs tasks on three kinds of capacity, which differ in how much infrastructure the team manages.
- EC2 launch type: tasks run on EC2 instances that the team manages in Auto Scaling groups, registered as container instances. It gives full control of instance types, GPUs, daemons and cost optimisation with Reserved Instances and Spot, but the team patches and scales the instances.
- AWS Fargate: serverless compute where each task runs in its own isolated environment; there are no instances to manage, patch or scale, and billing is per vCPU and memory per second. Fargate Spot runs interruptible tasks at a discount, with a two-minute warning delivered as a SIGTERM and a task state change event.
- Amazon ECS Managed Instances: ECS provisions, patches and scales EC2 instances on the team's behalf, offering EC2 instance choice, such as GPUs, without operating the instances.
The comparison below summarises the three compute options.
Amazon ECS Express Mode simplifies deploying a containerised web application by creating the service, load balancer, scaling and networking from a container image with sensible defaults. AWS positions it as the successor for workloads that used AWS App Runner and the AWS Copilot CLI, both of which are closed to new customers or out of support.
Capacity Providers
A capacity provider links a cluster to capacity: FARGATE, FARGATE_SPOT, or an Auto Scaling group capacity provider for EC2. A service or task uses a capacity provider strategy listing providers with a base (the minimum number of tasks on that provider) and a weight (the relative share above the base). For example, base 2 on FARGATE with weight 1, and weight 3 on FARGATE_SPOT, places the first two tasks on Fargate and then distributes additional tasks one to three between Fargate and Fargate Spot, combining guaranteed capacity with savings.
For Auto Scaling group capacity providers, managed scaling adjusts the group's size to the capacity tasks need, and managed termination protection prevents scale-in from terminating instances that are running tasks. These are explained further in the next lesson.
Capacity provider strategy
Exposing and Connecting Services
Services are exposed to clients through an ALB or NLB: the service registers each task's IP address and port in a target group, and the load balancer health checks tasks. For communication between services, ECS offers Service Connect, which adds a managed proxy to each task that provides service discovery by short names, client-side load balancing, retries, outlier detection and per-service traffic metrics in CloudWatch, and service discovery through AWS Cloud Map DNS records. Service Connect is the recommended option for new service-to-service communication.
Choosing ECS Settings for a Service
A team runs an API with variable traffic and wants minimal operations, lower cost where interruptions are acceptable, per-service network isolation and logs in CloudWatch. The API reads objects from S3.
-
Compute
Use Fargate with a capacity provider strategy of base 2, weight 1 on FARGATE and weight 2 on FARGATE_SPOT; the application handles SIGTERM to finish requests.
-
Network
Use awsvpc mode with a dedicated security group that allows traffic only from the ALB.
-
Roles
Give the task execution role ECR pull and CloudWatch Logs permissions, and the task role s3:GetObject on the bucket.
-
Operations
Use the awslogs driver, a container health check and the deployment circuit breaker with rollback.
Application code in an ECS task receives AccessDenied when reading from S3. The task execution role has s3:GetObject. What is wrong?
Show answer
Application code uses the task role, not the task execution role. Grant s3:GetObject to the task role.
Exam Tips
Remember for the exam
- Task execution role: image pulls, secrets and logs for the agent; task role: the application's own AWS calls.
- awsvpc mode gives each task an ENI and security groups and is required for Fargate; each task uses a subnet IP.
- Fargate removes instance management; Fargate Spot gives a two-minute SIGTERM warning; ECS Managed Instances provide managed EC2 capacity.
- Capacity provider strategies use base and weight to split tasks across providers such as FARGATE and FARGATE_SPOT.
- Service Connect provides service discovery, retries and traffic metrics between ECS services.