1. Answers
  2. Adding Fluentd Sidecar to ECS Task Definition

How do I add a Fluentd sidecar to an ECS task definition?

In this guide, we’ll walk you through adding a Fluentd sidecar container to an ECS (Elastic Container Service) task definition. This approach is commonly used to enhance logging and monitoring capabilities for your primary application container.

Resources

  1. aws_ecs_task_definition: Defines the ECS task definition with a container for your application and a Fluentd sidecar.

Here’s the code example:

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const appWithFluentd = new aws.ecs.TaskDefinition("app_with_fluentd", {
    family: "app_with_fluentd",
    requiresCompatibilities: ["FARGATE"],
    networkMode: "awsvpc",
    cpu: "256",
    memory: "512",
    containerDefinitions: JSON.stringify([
        {
            name: "app",
            image: "my-app:latest",
            essential: true,
            portMappings: [{
                containerPort: 80,
                hostPort: 80,
            }],
            logConfiguration: {
                logDriver: "awslogs",
                options: {
                    "awslogs-group": "/ecs/app_with_fluentd",
                    "awslogs-region": "us-west-2",
                    "awslogs-stream-prefix": "ecs",
                },
            },
        },
        {
            name: "fluentd",
            image: "fluent/fluentd:latest",
            essential: false,
            environment: [{
                name: "FLUENTD_STREAM",
                value: "fluentd",
            }],
            logConfiguration: {
                logDriver: "awslogs",
                options: {
                    "awslogs-group": "/ecs/app_with_fluentd",
                    "awslogs-region": "us-west-2",
                    "awslogs-stream-prefix": "fluentd",
                },
            },
        },
    ]),
    tags: {
        Name: "app_with_fluentd",
    },
});
export const taskDefinitionArn = appWithFluentd.arn;

In the code above:

  • aws_ecs_task_definition specifies the task definition with the necessary properties for Fargate.
  • The container_definitions parameter is a list of containers:
    • The app container runs the primary application.
    • The fluentd container runs Fluentd.
  • Both containers are configured with log drivers. The Fluentd container collects and forwards the logs for enhanced log management and monitoring purposes.

Summary

We’ve successfully added a Fluentd sidecar to the ECS task definition. This configuration runs both the application and Fluentd containers, with Fluentd responsible for enhanced logging of the application. This setup helps in efficient and centralized log management.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up