CloudWatch Logs

Send container stdout and stderr to CloudWatch Logs with the awslogs driver.

Configure logConfiguration with the awslogs driver to stream each container's output to CloudWatch Logs.

Key options

  • awslogs-group — the log group to write to.
  • awslogs-region — the region of the log group.
  • awslogs-stream-prefix — groups streams per container.

The task execution role needs permission to create log streams and put log events.

Example

Example · json
{
  "containerDefinitions": [
    {
      "name": "app",
      "image": "app:1",
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/app",
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "app"
        }
      }
    }
  ]
}

When to use it

  • A team configures the awslogs driver in every container definition so all container stdout and stderr streams are automatically sent to CloudWatch Logs.
  • An ops engineer sets a CloudWatch Logs retention policy to 30 days on the ECS log group to control storage costs for verbose microservices.
  • A developer tails a CloudWatch log stream in the AWS console to debug a task that starts and immediately exits with an error.

More examples

awslogs Driver in Container Definition

Configures the awslogs driver so container stdout/stderr is sent to the /ecs/api CloudWatch Logs group with a per-task stream prefix.

Example · json
{
  "name": "api",
  "image": "my-api:latest",
  "logConfiguration": {
    "logDriver": "awslogs",
    "options": {
      "awslogs-group": "/ecs/api",
      "awslogs-region": "us-east-1",
      "awslogs-stream-prefix": "api"
    }
  }
}

Set Log Group Retention Policy

Sets a 30-day log retention on the ECS log group so old log streams are automatically deleted to control CloudWatch Logs storage costs.

Example · bash
aws logs put-retention-policy \
  --log-group-name /ecs/api \
  --retention-in-days 30

Tail Live Container Logs

Finds the most recent ECS log stream and follows it in real time, equivalent to tailing a local log file for a running container.

Example · bash
LOG_STREAM=$(aws logs describe-log-streams \
  --log-group-name /ecs/api \
  --order-by LastEventTime \
  --descending \
  --max-items 1 \
  --query 'logStreams[0].logStreamName' --output text)

aws logs tail /ecs/api --log-stream-names "$LOG_STREAM" --follow

Discussion

  • Be the first to comment on this lesson.