Using New Relic with Elastic Beanstalk and Java Spring Boot

FINE Legal Dev
2 min readApr 27, 2021

I will explain how I managed to get New Relic running on a Spring Boot application that runs on Elastic Beanstalk and gets deployed by CodePipeline.

Install New Relic on your ec2 instance via a config file

Since the ec2 instances get cleaned up on several occasions, it doesn't make sense to connect to an ec2 instance and install New Relic on it and then afterward use it. You have to install it via a script that gets executed at deployment.

I’m using the .ebextensions folder to place a config file which is doing the trick. This file is downloading and saving New Relic as well as replacing the license key and application name inside the yaml config file of New Relic.

Therefore I place the following file inside the root folder of my application (where you can also find your pom.xml ) inside a folder called .ebextensions #cptnobvious

commands:
01-download-java-agent:
command: "sudo curl --location https://download.newrelic.com/newrelic/java-agent/newrelic-agent/current/newrelic-java.zip > /var/newrelic-java.zip"
02-unzip-java-agent:
command: "unzip -o /var/newrelic-java.zip -d /var/"
03-give-permissions-to-newrelic:
command: "chmod a+rwx /var/newrelic"
04-replace-license-key:
command: "sed -i 's/<%= license_key %>/{your-licence-code}/g' /var/newrelic/newrelic.yml"
05-replace-application-name:
command: "sed -i 's/My Application/{your-application-name}/g' /var/newrelic/newrelic.yml"

Include .ebextensions folder in your build artifact so AWS is running your scripts when deploying your application on Elastic Beanstalk. Therefore you need to define these files as part of your artifacts. You can do this by adding the following lines into your buildspec.yml :

artifacts:
files:
- app.jar
- .ebextensions/**/*

You can find my complete buildspec.ymlfile below:

version: 0.2
phases:
install:
commands:
- echo Entered the install phase...
runtime-versions:
java: corretto11
pre_build:
commands:
- echo Entered the pre_build phase...
build:
commands:
- echo Entered the build phase...
post_build:
commands:
- echo Entered the post_build phase...
- mvn package
- mv target/*.jar app.jar
artifacts:
files:
- app.jar
- .ebextensions/**/*

After you have deployed these steps to your environment you should be able to validate that New Relic got installed into the defined folder and the newrelic.yml got adapted with your information.

Execute the application together with the java agent

The next step is to define the javaagent when executing our application.

Therefore we leverage a Procfile to define the command which will be used to execute our application.

Similar to the buildspec.yml the Procfile resides in the root of the application directory and simply contains the run argument for the application with the directory where we stored the New Relic jar.

web: java -javaagent:/var/newrelic/newrelic.jar -jar app.jar

Include the Procfile in your deployment build artifacts

This file needs to be added to our buildspec.yml similar to the .ebextensions folder:

artifacts:
files:
- Procfile
- app.jar
- .ebextensions/**/*

With these steps added the New Relic will be executed as a javaagent.

In the next part, I will cover how I took application variables into account when setting the application name inside the configuration file of New Relic.

--

--