New Relic Server Monitoring on Elastic Beanstalk

When I set out to deploy the excellent and free New Relic’s Server Monitoring agent on a couple of Elastic Beanstalk environments, I was expecting it to be an “easy-peasy copy/paste a couple of commands and it magically works” type of thing like it usually is when dealing with New Relic. What surprised me was that not only that there wasn’t a lot of official documentation for it, but I also couldn’t find a ready-made solution that did what I needed (crazy, I know). And what I needed seemed pretty simple; automatically deploy and configure the nrsysmond agent while having it read some info about the environment.

The environment part is important since nrsysmond by default reads the hostname of the server where it’s running and that’s not very useful on AWS EC2 where hostnames are random instance ids. So the first thing I did was to look for a Python/Boto script that was able to return the name of the EB Environment in which the instance was running, and found it I did along with a required IAM policy. What’s handy about this is that Elastic Beanstalk instances have Boto preinstalled so it saves us the trouble of doing it ourselves. Next thing I did was add the aforementioned IAM policy to the Instance Role used by Elastic Beanstalk (it’s named aws-elasticbeanstalk-ec2-role by default):

This will in effect allow all instances running inside Elastic Beanstalk will be able to retrieve data about all other EC2 resources, i.e. it will enable the Python script to run correctly and return the data we need. Note that it would be a good idea to tighten those permissions to only those resources that are necessary, but for the purposes of this post (as well as if you decide to experiment yourself), we’ll keep it simple. We also want to keep our New Relic license key in a safe place, so we’ll set it as an Elastic Beanstalk environment variable named NEW_RELIC_LICENSE_KEY.

Now that we have everything in place, we can put together our ebextensions config file which will look like this:

The files part will put the python script on a convenient location and make it executable. Note that the script can easily be modified to return some other EC2 instance tag, or pretty much anything else.
packages will set up the New Relic yum repository and install the required package.
commands will replace the hostname parameter in nrsysmond config file with the name of the EB Environment (provided by our script).
Finally, container_commands will set the license key (retrieved from EB environment variables) and start the New Relic monitoring agent.

Once you add the config file from above to your project and push to Elastic Beanstalk, provided that everything deployed correctly, new servers should appear in your New Relic dashboard.

Follow me

Dario Duvnjak

Throughout the years I've experienced working in all parts of the software development process on different tech stacks and with clients from all over the world. Always looking out for the next great tool in cutting edge open source technologies. I specialize in Cloud computing (AWS), Ruby, Node.js, DevOps, Docker and Puppet.
Follow me
Dario DuvnjakNew Relic Server Monitoring on Elastic Beanstalk