Sub topic 1
From container image to running workload
Prerequisites
Assume basic Docker, IAM, VPC, security group, HTTP, AWS CLI, and JSON knowledge. You should already be able to build and run a container locally.
From container image to running workload
Amazon ECS runs containerized applications. A container image packages the application and its dependencies. ECS uses that image to create one or more running containers.
The path is:
Storing images in Amazon ECR
Amazon Elastic Container Registry (ECR) is a private registry for container images. Push a versioned image to ECR, then reference its full URI in the task definition.
aws ecr create-repository --repository-name web-app
aws ecr get-login-password --region us-east-1 |
docker login --username AWS --password-stdin \
123456789012.dkr.ecr.us-east-1.amazonaws.com
docker build -t web-app:1.0.0 .
docker tag web-app:1.0.0 \
123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0
docker push \
123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0Use immutable version tags rather than relying only on latest. This makes deployments reproducible.
ECS building blocks: clusters, tasks, and services
A cluster is a logical group for ECS workloads. A task is a running instance of a task definition. A service maintains a desired number of tasks and replaces tasks that stop.
A task definition is the deployment blueprint. It specifies the image, ports, resources, logging, secrets, and IAM roles.
What Fargate manages and what you still own
Fargate is a serverless ECS launch type. AWS manages the underlying servers, operating system, container runtime, and task placement.
You still manage the image, application process, task definition, IAM permissions, network design, security groups, logs, health checks, and data storage. Fargate tasks are not automatically durable storage.
Writing a Fargate task definition
Fargate requires awsvpc networking and task-level CPU and memory. The execution role pulls images and writes logs. The task role grants the application access to AWS services.
{
"family": "web-app",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "256",
"memory": "512",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::123456789012:role/webAppTaskRole",
"containerDefinitions": [
{
"name": "web",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0",
"essential": true,
"portMappings": [
{ "containerPort": 8080, "protocol": "tcp" }
]
}
]
}Choosing task CPU and memory
Fargate accepts specific CPU and memory combinations. For example, 256 CPU units supports 512 MiB memory. Size the task for normal load with headroom, then validate using CPU and memory metrics.
Too little memory causes out-of-memory stops. Too much memory increases cost without improving performance.
Exercise
Which ECS object maintains two copies of an application and replaces a stopped copy?
Show answer
An ECS service maintains the desired task count and replaces stopped tasks.