Search code examples
amazon-web-servicesamazon-elastic-beanstalkamazon-cloudwatchamazon-cloudwatchlogs

How to setup CloudWatch embedded metrics format (emf) in Elastic Beanstalk


I'm trying to use CloudWatch embedded metrics format (emf) inside a beanstalk environment running Docker image.

(Documentation on emf here and here )

To be able to emit emf metrics, the aws-cloudwatch-agent needs to have the following configuration:

{
  "logs": {
    "metrics_collected": {
      "emf": { }
    }
  }
}

However I'm not able to find the namespace definition to pass emf configuration to beanstalk, using the .ebextensions method described here. The available namespaces seem to just stream the existing logs.

How can I enable CloudWatch emf publishing in an elastic beanstalk environment?


Solution

  • To pass additional configuration use platform hooks described in this question. More detail on platform hooks: AWS documentation,

    1. I've included .platform/hooks/predeploy/emf-config.sh with content:
    #!/bin/bash
    
    echo '{
            "logs": {
              "metrics_collected": {
                "emf": { }
              }
            }
          }' > "/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/emf_metrics.json"
    /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a append-config
    
    1. If you use docker, make sure to include this files in your docker image. Using CDK pipeline I've included:
        (...)
        artifacts: {
            files: [
                "Dockerrun.aws.json",
                ".platform/hooks/**/*"
            ]
        },
    
    1. Finally, make sure to add execution permissions to the .sh file. Related question
    chmod +x path/to/emf-config.sh
    git update-index --chmod=+x path/to/emf-config.sh