How do I use Datadog with Amazon ECS?
In this guide, we will set up Datadog monitoring for an Amazon ECS cluster using Pulumi. We will create an ECS cluster, an ECS service, and integrate Datadog monitoring by setting up the necessary IAM roles and Datadog agent as a sidecar container.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
import * as datadog from "@pulumi/datadog";
// Create an ECS cluster
const cluster = new aws.ecs.Cluster("ecs-cluster");
// Create a VPC and subnets
const vpc = new awsx.ec2.Vpc("vpc", {});
// Create an IAM role for the ECS task
const taskRole = new aws.iam.Role("taskRole", {
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({ Service: "ecs-tasks.amazonaws.com" }),
});
// Attach the necessary policies to the task role
const taskRolePolicy = new aws.iam.RolePolicyAttachment("taskRolePolicy", {
role: taskRole,
policyArn: "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy",
});
// Define the ECS task definition
const taskDefinition = new aws.ecs.TaskDefinition("taskDefinition", {
family: "ecs-task-family",
cpu: "256",
memory: "512",
networkMode: "awsvpc",
requiresCompatibilities: ["FARGATE"],
executionRoleArn: taskRole.arn,
containerDefinitions: pulumi.output([
{
name: "app",
image: "nginx",
essential: true,
portMappings: [{
containerPort: 80,
hostPort: 80,
protocol: "tcp",
}],
},
{
name: "datadog-agent",
image: "datadog/agent:latest",
essential: true,
environment: [
{ name: "DD_API_KEY", value: "your-datadog-api-key" },
{ name: "ECS_FARGATE", value: "true" },
],
logConfiguration: {
logDriver: "awslogs",
options: {
"awslogs-group": "/ecs/datadog",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs",
},
},
},
]).apply(JSON.stringify),
});
// Create an ECS service
const service = new aws.ecs.Service("service", {
cluster: cluster.arn,
taskDefinition: taskDefinition.arn,
desiredCount: 1,
launchType: "FARGATE",
networkConfiguration: {
subnets: vpc.publicSubnetIds,
assignPublicIp: true,
},
});
// Create a CloudWatch Log Group for Datadog
const logGroup = new aws.cloudwatch.LogGroup("logGroup", {
name: "/ecs/datadog",
retentionInDays: 7,
});
Key Points
- ECS Cluster: We created an ECS cluster to run our tasks.
- IAM Role: An IAM role with the necessary policies was created for the ECS task.
- Task Definition: Defined an ECS task with two containers: the application container and the Datadog agent container.
- ECS Service: An ECS service was created to run the task definition.
- CloudWatch Log Group: A CloudWatch log group was created to store logs from the Datadog agent.
Summary
We set up an Amazon ECS cluster with Datadog monitoring by creating the necessary resources using Pulumi. The Datadog agent was added as a sidecar container in the ECS task definition to monitor the application container.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.