Teratip: How to monitor an ECS task running on Fargate with Datadog
Updated: Jul 11
Let’s say you already have Datadog configured to monitor your AWS workloads and want to get more insights from some ECS tasks running on Fargate. To do that, you must add the Datadog Agent to your task as a sidecar container -i.e. an additional container that runs alongside the application container.
Below is an example of the container definitions block of an ECS task definition. The first container is a custom application and the second one is the Datadog Agent:
[
{
"name": "post-migrations-production",
"cpu": ${cpu_units},
"memory": ${max_memory},
"memoryReservation": ${min_memory},
"image": "${ecr_repo}",
"essential": true,
"environment": [
{
"name": "DD_SERVICE_NAME",
"value": "post-migrations"
}
],
},
{
"name": "datadog-agent",
"image": "public.ecr.aws/datadog/agent:latest",
"environment": [
{
"name": "ECS_FARGATE",
"value": "true"
},
{
"name": "DD_API_KEY",
"value": "xxxxxxxxxxxxxxxxxxxxxxxxxx"
}
]
}
]
To enable monitoring on Fargate, you have to set two environment variables: ECS_FARGATE to true and DD_API_KEY with your Datadog API key.
This way, the next time the task runs, CPU, memory, disk, and network usage of your ECS Fargate cluster will be monitored on Datadog.
Collecting traces and APM data
Now if you want to collect traces and APM data from your application, you will have to allow the DD Agent to communicate on the container’s port 8126 and add the DD_APM_ENABLED and DD_APM_NON_LOCAL_TRAFFIC environment variables to the Agent container definition as well:
"containerDefinitions": [
{
"name": "datadog-agent",
"image": "public.ecr.aws/datadog/agent:latest",
"portMappings": [
{
"hostPort": 8126,
"protocol": "tcp",
"containerPort": 8126
}
],
"environment": [
{
"name": "ECS_FARGATE",
"value": "true"
},
{
"name": "DD_API_KEY",
"value": "xxxxxxxxxxxxxxxxxxxxxxxxxx"
},
{
"name": "DD_APM_ENABLED",
"value": "true"
},
{
"name": "DD_APM_NON_LOCAL_TRAFFIC",
"value": "true"
}
]
}
Visualizing APM data
Datadog uses flame graphs to display distributed traces. It means that it shows all the service calls that comprise a unique request.
Final words
By configuring the Datadog Agent as a carside container in the ECS task definition of your application running on Fargate, you can collect a lot of metrics, traces, and APM data that will help you when troubleshooting.
For more information, visit https://www.datadoghq.com/blog/aws-fargate-monitoring-with-datadog/
Lucas Valor
DevOps Engineer
If you want to learn more about our #TeraTips or our blog's content, we invite you to see all the content entries that we have created for your Cloud needs.