Learn how to effortlessly centralize and monitor your ECS task and container logs by creating a dedicated AWS CloudWatch log group with Terraform.
This guide provides a step-by-step approach to configuring your Amazon Elastic Container Service (ECS) tasks to send logs to Amazon CloudWatch. By following these steps, you'll be able to centralize your ECS task logs, making it easier to monitor, troubleshoot, and analyze your application's behavior in a production environment.
Create a CloudWatch Log Group:
resource "aws_cloudwatch_log_group" "ecs_log_group" {
name = "/ecs/my-app"
}
Replace /ecs/my-app
with your desired log group name.
Define an IAM Role for ECS Tasks: This role allows ECS tasks to write logs to CloudWatch.
resource "aws_iam_role" "ecs_task_role" {
name = "ecs-task-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
Attach the AmazonECS_FullAccess
policy to the role:
resource "aws_iam_role_policy_attachment" "ecs_task_role_attachment" {
role = aws_iam_role.ecs_task_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonECS_FullAccess"
}
Configure the ECS Task Definition: Specify the log group and IAM role in your ECS task definition.
resource "aws_ecs_task_definition" "my_task_definition" {
family = "my-app"
# ... other task definition settings ...
container_definitions = <<EOF
[
{
"name": "my-container",
"image": "nginx:latest",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": aws_cloudwatch_log_group.ecs_log_group.name,
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "my-app"
}
}
}
]
EOF
execution_role_arn = aws_iam_role.ecs_task_role.arn
}
"nginx:latest"
with your container image."awslogs-group"
points to the CloudWatch Log Group created earlier."awslogs-region"
and "awslogs-stream-prefix"
as needed.Deploy your ECS service: Use the defined task definition when creating or updating your ECS service.
Now, logs from your ECS tasks will be sent to the specified CloudWatch Log Group.
This Terraform code defines infrastructure for running a Docker container on AWS ECS Fargate. It sets up a CloudWatch Log Group to store container logs, an IAM role with necessary permissions for ECS tasks, and an ECS task definition specifying the container image and log configuration. Finally, it creates an ECS cluster and service to run the defined task, ensuring logs are sent to the designated CloudWatch Log Group.
# Create a CloudWatch Log Group
resource "aws_cloudwatch_log_group" "ecs_log_group" {
name = "/ecs/my-app"
}
# Define an IAM Role for ECS Tasks
resource "aws_iam_role" "ecs_task_role" {
name = "ecs-task-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# Attach the `AmazonECS_FullAccess` policy to the role
resource "aws_iam_role_policy_attachment" "ecs_task_role_attachment" {
role = aws_iam_role.ecs_task_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonECS_FullAccess"
}
# Configure the ECS Task Definition
resource "aws_ecs_task_definition" "my_task_definition" {
family = "my-app"
container_definitions = <<EOF
[
{
"name": "my-container",
"image": "nginx:latest",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": aws_cloudwatch_log_group.ecs_log_group.name,
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "my-app"
}
}
}
]
EOF
execution_role_arn = aws_iam_role.ecs_task_role.arn
}
# Example ECS Service using the defined task definition
resource "aws_ecs_cluster" "example" {
name = "my-cluster"
}
resource "aws_ecs_service" "example" {
name = "my-service"
cluster = aws_ecs_cluster.example.id
task_definition = aws_ecs_task_definition.my_task_definition.arn
desired_count = 1
launch_type = "FARGATE"
network_configuration {
subnets = ["subnet-1234567890abcdef0"]
security_groups = ["sg-1234567890abcdef0"]
}
}
Explanation:
/ecs/my-app
to store logs from our ECS tasks.ecs-task-role
that allows ECS tasks to assume it.AmazonECS_FullAccess
policy to the role, granting it permissions to write logs to CloudWatch.my_task_definition
for our application.container_definitions
, we specify the logConfiguration
block:
logDriver
: Set to awslogs
to use CloudWatch Logs.options
:
awslogs-group
: Points to the CloudWatch Log Group created earlier.awslogs-region
: Specifies the AWS region.awslogs-stream-prefix
: Adds a prefix to the log stream names.execution_role_arn
to the created IAM role.Important:
"nginx:latest"
, "us-west-2"
, subnet IDs, and security group IDs with your actual values./ecs/my-app
log group in CloudWatch.General:
/ecs/my-app
is a common convention, you can choose any valid CloudWatch Log Group name. Consider organizing log groups by application, environment, or team.AmazonECS_FullAccess
, consider creating a custom IAM policy with only the necessary permissions for writing logs.Terraform Specific:
Troubleshooting:
awslogs
driver configuration in the task definition is accurate.Security:
This guide outlines the process of configuring AWS ECS tasks to send logs to a CloudWatch Log Group using Terraform.
Steps:
aws_cloudwatch_log_group
.aws_iam_role
and attach the AmazonECS_FullAccess
policy using aws_iam_role_policy_attachment
. This allows ECS tasks to write logs to CloudWatch.aws_ecs_task_definition
resource, specify the created log group name in the logConfiguration
section of your container definition.logDriver
to awslogs
.awslogs-region
and awslogs-stream-prefix
as needed.execution_role_arn
attribute.By following these steps, logs generated by your ECS tasks will be automatically sent and stored in the designated CloudWatch Log Group, enabling you to monitor and analyze your application logs effectively.
This Terraform code provides a robust and repeatable solution for configuring your ECS tasks to send logs to CloudWatch. By centralizing your logs, you gain improved observability into your application's behavior, simplifying troubleshooting, analysis, and ultimately contributing to a more reliable and maintainable production environment.