Sub topic 8
Sending container logs to CloudWatch Logs
This chapter assumes you can read a Fargate task definition and know the difference between the task execution role and the task role. It covers how a container's output reaches CloudWatch Logs, and what breaks when it doesn't.
How the awslogs driver works
Fargate captures everything your container writes to standard output and standard error. The awslogs log driver forwards that output to a CloudWatch Logs log group. You don't install an agent. Log into the container only if you need to debug.
The driver runs on behalf of the platform, not your application. This is why the task execution role needs the logging permissions, not the task role.
Configuring the driver
Add a logConfiguration block to each container definition:
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/web-app",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "web"
}
}Each task writes to its own log stream named prefix/container-name/task-id. With the settings above, a stream looks like web/web/0a1b2c3d.... The prefix is required for Fargate.
Creating the log group
The log group must exist before the task starts, unless you ask the driver to create it. Create it yourself so you can set a retention period. Log groups keep data forever by default, and that cost grows quietly.
aws logs create-log-group --log-group-name /ecs/web-app
aws logs put-retention-policy \
--log-group-name /ecs/web-app \
--retention-in-days 30You can add "awslogs-create-group": "true" to the options instead. That also requires logs:CreateLogGroup on the execution role, which the managed AmazonECSTaskExecutionRolePolicy does not grant. Creating the group yourself avoids that extra permission.
Reading logs
Use aws logs tail to follow a group from the terminal:
aws logs tail /ecs/web-app --follow --since 15mAdd --log-stream-name-prefix web/web to narrow the output to one container.
Common errors
| Symptom | Likely cause |
|---|---|
| Task stops with `ResourceInitializationError` mentioning the log group | The log group named in `awslogs-group` does not exist |
| Task stops with a timeout while setting up logging | A private subnet has no route to CloudWatch Logs |
| Task runs but the group stays empty | The app writes to a file instead of stdout and stderr |
| `AccessDeniedException` on `CreateLogStream` | The execution role lacks `logs:CreateLogStream` or `logs:PutLogEvents` |
For the network case, add a NAT gateway or a VPC interface endpoint for com.amazonaws.<region>.logs. Tasks in private subnets with assignPublicIp=DISABLED have no other path.
Exercise
A new Fargate task in a private subnet stops within seconds of starting. The stopped reason mentions a failure to initialize the log driver. The log group exists and the execution role uses the managed policy. What should you check next?
Show answer
Check the network path to CloudWatch Logs. A private subnet with no NAT gateway and no logs VPC endpoint can't reach the service, so the driver fails during task setup. Add a NAT route or a VPC endpoint for com.amazonaws.<region>.logs.