You can deploy a sample python application that demonstrates how you can use the ECS CloudWatch logs driver with the Embedded Metric Format. This will enable you to capture custom CloudWatch metrics for your container based workloads running on ECS. This example uses the aws-embedded-metrics library for python.
You will need to build and push a docker image for the application using the provided Dockerfile The image is based on the public python 3 image and simply copies the python code to the image and installs the aws-embedded-metrics python library.
-
Clone this directory and then build the docker image:
docker build -t ecs-cw-python-emf-demo
-
Tag the image so it can be pushed to your docker image repository, for example, for an ECR repo named ecs-firelens-python-emf-demo, your command will look like:
docker tag ecs-cw-python-emf-demo:latest <your account number>.dkr.ecr.<region>.amazonaws.com/ecs-cw-python-emf-demo:latest
-
Push the image to the repository:
docker push <account number>.dkr.ecr.<region>.amazonaws.com/ecs-cw-python-emf-demo:latest
You can use the AWS CLI to create a task definition for an ECS cluster with EC2 container instances.
-
Use the AWS CLI with your AWS account credentials applied and run the following command:
aws ecs register-task-definition --cli-input-json file://ecs-task-python-cloudwatch-emf.json --region <your aws region>
Configure AWS Systems Manager Parameter Store to store the CloudWatch configuration in cwagentconfig
The task definition you created retrieves the CloudWatch configuration in the AWS Systems Manager Parameter Store parameter named cwagentconfig. Login to the AWS Systems Manager console and create a new parameter named cwagentconfig with the string value:
{
"logs": {
"metrics_collected": {
"emf": { }
}
}
}
Launch a new task into an existing ECS cluster using the task definition you created. The task definition that is provided is compatible with ECS clusters with EC2 container instances. You can modify it to work with FARGATE by specifying an awsvpc for the networkMode.