Sub topic 4
Writing a Fargate task definition
A task definition is the blueprint ECS uses to start a task. This chapter assumes you can build a Docker image, read JSON, and know the basics of IAM and VPC. It walks through the fields Fargate requires and the common errors when you register one.
What a task definition contains
A task definition has two levels. Task-level settings apply to the whole task: the family name, CPU and memory, network mode, and IAM roles. Container-level settings, in containerDefinitions, apply to each container: the image, ports, environment, secrets, health check, and logging.
Every registration creates a new numbered revision of the family (web-app:1, web-app:2, and so on). Revisions are immutable. To change anything, you register a new revision and point the service at it.
A complete example
{
"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",
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
},
"containerDefinitions": [
{
"name": "web",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0",
"essential": true,
"portMappings": [
{ "containerPort": 8080, "protocol": "tcp" }
],
"environment": [
{ "name": "APP_ENV", "value": "production" }
],
"secrets": [
{
"name": "DB_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:us-east-1:123456789012:secret:prod/db-AbCdEf"
}
],
"healthCheck": {
"command": ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 30
},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/web-app",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "web"
}
}
}
]
}Fields Fargate requires
requiresCompatibilities: ["FARGATE"]makes ECS validate the definition against Fargate rules.networkMode: "awsvpc"is the only mode Fargate supports. Each task gets its own network interface.cpuandmemoryat the task level are mandatory. Values are strings, and only certain combinations are valid.executionRoleArnis needed here because the task pulls from ECR, writes logs, and reads a secret.
The parts that matter most
- Image tag. Use a specific version such as
1.0.0. A mutable tag likelatestmeans two tasks from the same revision can run different code. essential. If an essential container stops, ECS stops the whole task. Mark at least one container essential.secretsversusenvironment.secretspulls values at startup using the execution role. Plain values inenvironmentare visible in the task definition.startPeriod. This gives the container time to boot before failed health checks count against it.awslogs-group. The log group must already exist, unless you addawslogs-create-group: "true"and grant the permission to create it.
Registering the definition
aws ecs register-task-definition --cli-input-json file://taskdef.jsonCommon errors
| Symptom | Likely cause |
|---|---|
| Registration fails on CPU or memory | The pair is not a valid Fargate combination |
| `CannotPullContainerError` | The execution role lacks ECR permissions, or the task has no route to ECR |
| `ResourceInitializationError` on a secret | The execution role cannot read the secret, or there is no network path to Secrets Manager |
| Logs are missing | The log group does not exist, or the execution role cannot write to it |
| Task exits immediately | An essential container crashed. Check its exit code and logs. |
Exercise
A task definition registers successfully, but tasks fail to start with an error fetching the DB_PASSWORD secret. Which role needs the secretsmanager:GetSecretValue permission, the task role or the execution role, and why?
Show answer
The execution role. ECS resolves the secrets entries at container startup, before your application code runs, and it does so with the execution role. The task role is only used by code inside the running container.