Datadog integration with AWS Elastic Beanstalk for Spring Boot application

Posted at — Jul 6, 2018
Riekpil logo
Learn how to test real-world applications with the Testing Spring Boot Applications Masterclass. Comprehensive online course with 8 modules and 130+ video lessons to master well-known Java testing libraries: JUnit 5, Mockito, Testcontainers, WireMock, Awaitility, Selenium, LocalStack, Selenide, and Spring's Outstanding Test Support.

If you have an application deployed on AWS Elastic Beanstalk and you want to add Datadog integration to it, then these instructions might help you out.

I started out with the instructions at but they use the older version 5 of the datadog-agent and not the newer version 6.

In your source code, you normally have a .ebextensions folder to configure the Elastic Beanstalk deployment. Inside that folder, create the following files (Create directories as needed):


# .ebextensions/99datadog.config


"/tmp/" :

mode: "000700"

owner: root

group: root

content: |


sed 's/api_key:.*/api_key: YOUR_API_KEY' /etc/datadog-agent/datadog.yaml.example > /etc/datadog-agent/datadog.yaml

sed -i 's/# expvar_port:.*/expvar_port: 5005/' /etc/datadog-agent/datadog.yaml

sed -i 's/# cmd_port:.*/cmd_port: 5006/' /etc/datadog-agent/datadog.yaml

sed -i 's/.*logs_enabled:.*/logs_enabled: true/' /etc/datadog-agent/datadog.yaml



command: "chmod +x .ebextensions/datadog/hooks/*"


test: '[ ! -d /opt/elasticbeanstalk/hooks/appdeploy/post ]'

command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"


test: '[ ! -d /opt/elasticbeanstalk/hooks/configdeploy/post ]'

command: "mkdir /opt/elasticbeanstalk/hooks/configdeploy/post"


command: "cp .ebextensions/datadog/hooks/ /opt/elasticbeanstalk/hooks/appdeploy/pre/"


command: "cp .ebextensions/datadog/hooks/ /opt/elasticbeanstalk/hooks/appdeploy/post/"


command: "cp .ebextensions/datadog/hooks/ /opt/elasticbeanstalk/hooks/preinit"


command: "cp .ebextensions/datadog/hooks/ /opt/elasticbeanstalk/hooks/postinit"


command: "cp .ebextensions/datadog/hooks/ /opt/elasticbeanstalk/hooks/configdeploy/pre/"


command: "cp .ebextensions/datadog/hooks/ /opt/elasticbeanstalk/hooks/configdeploy/post/"


command: "cp .ebextensions/datadog/datadog.repo /etc/yum.repos.d/datadog.repo; yum -y makecache; yum -y install datadog-agent"


command: "/tmp/ ; rm /tmp/"


command: "cp .ebextensions/datadog/conf.d/java.yaml /etc/datadog-agent/conf.d/"

Don’t forget to replace YOUR_API_KEY with the actual Datadog API key.

Note that I also enable sending log entries to datadog. If you don’t need/want that, just leave out the 2nd sed line and remove the section 92_copy_java_log_config.

As a best practise, don’t actually commit your API key here. For our application, we use CloudFormation to be able to read this form an environment variable.

I also needed to change the expvar_port and cmd_port ports as they use 5000 and 5001 by default. However, my Spring Boot application already uses port 5000 as Elastic Beanstalk expects the application to use that port.



name = Datadog, Inc.

baseurl =






# .ebextensions/datadog/hooks/

STATUS=`sudo initctl status datadog-agent`

if [[ "$STATUS" == *"datadog-agent start/running"* ]]


echo "Agent already running"


echo "Agent starting..."

sudo initctl start datadog-agent




# .ebextensions/datadog/hooks/

STATUS=`sudo initctl status datadog-agent`

if [[ "$STATUS" == *"datadog-agent stop/waiting"* ]]


echo "Agent already stopped"


echo "Agent stopping..."

sudo initctl stop datadog-agent


The final file is only needed because I want to send logs from my Spring Boot application, so this is optional if you don’t need that:


#Log section


## - type : file (mandatory) type of log input source (tcp / udp / file)

## port / path : (mandatory) Set port if type is tcp or udp. Set path if type is file

## service : (mandatory) name of the service owning the log

## source : (mandatory) attribute that defines which integration is sending the logs

## sourcecategory : (optional) Multiple value attribute. Can be used to refine the source attribtue

## tags: (optional) add tags to each logs collected

- type: file

path: /var/app/current/myapplication-datadog.log

service: java

source: java

sourcecategory: sourcecode

# For multiline logs, if they start by the date with the format yyyy-mm-dd uncomment the following processing rule


# - type: multi_line

# name: new_log_start_with_date

# pattern: \d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])

This assumes you have setup logging in the Spring Boot application to write using the net.logstash.logback.encoder.LogstashEncoder to a file myapplication-datadog.log

The easiest to do that is using the a logback-spring.xml at the root of the classpath:

<?xml version="1.0" encoding="UTF-8"?>

    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <springProfile name="dev,localmysql">
        <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>

    <springProfile name="staging,prod">
        <include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
        <appender name="DATADOGFILE"
            <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">

        <root level="INFO">
            <appender-ref ref="FILE"/>
            <appender-ref ref="DATADOGFILE"/>

After deploying all this, metrics and log files should appear in your Datadog console.

If you want to be notified in the future about new articles, as well as other interesting things I'm working on, join my mailing list!
I send emails quite infrequently, and will never share your email address with anyone else.