NewRelic Server Monitor for Amazon Elastic Beanstalk application

September 27, 2014 | | Tags : AWS NewRelic Monitoring Elastic Beanstalk

Recently we moved our web applications to Amazon Elastic Beanstalk platform. To monitor our running instances we decided to try NewRelic Server Monitoring. How to install and configure applications on Windows platform was not an issue - this is fully described in the AWS documentation. The problem was that by default right after the installation of NewRelic agent, it was reporting home the server’s hostname which in the case of Elastic Beanstalk was the same for all instances! Plus just a random windows server name wouldn’t be the most convenient way to tell which server data you’re looking at the moment. I discovered that NewRelic agent could be started with a -host parameter, which allows to pass any string to be used as a hostname in the agent’s communication with the base. In turn, this value is stored in the registry and when it is modified, and the agent service is restarted, the new host name is used.

So - here is the contents of my /.ebextensions/01-install-newrelic.config file:

   source: ""  
   content: |  
    #stop newrelic service  
    Stop-Service -Name nrsvrmon;  
    $longName = Invoke-RestMethod -uri  
    $displayName = $longName.Split('.')[0].Replace("ip-", "");  
    $displayName = [string]::Format("{0}-{1}", $env:NewRelic_display_prefix, $displayName).Replace(" ", "-");  
    Set-ItemProperty -Path "HKLM:\SOFTWARE\New Relic\Server Monitor" -Name Host -Value $displayName  
    #start newrelic service  
    Start-Service -Name nrsvrmon;  
   command: "msiexec.exe /q /i c:\\newrelic\\newrelic.msi NR_LICENSE_KEY=%NewRelic_license_key% INSTALLLEVEL=1"  
   waitAfterCompletion: 0  
   command: powershell -ExecutionPolicy RemoteSigned -File .\\rename_newrelic_display_name_if_necessary.ps1  
   cwd: c:\\init_scripts  
   waitAfterCompletion: 0  

Please note that this script uses 2 environment variables: NewRelic_license_key and NewRelic_display_prefix. You should set these variables in your elastic beanstalk environment configuration. First is used during the newrelic installation, second is used to prefix the display names for easier identification. For example for your production web site you can set it to “Prod-Web” and in the NewRelic web site each instance from this environment will look like “Prod-Web-10-nnn-nnn-nnn”, where nnn - is the numeric part of the instance’s internal IP address.