Unique hostname for new nrsysmond heirloom on a resilient beanstalk
I am configuring nrsysmond to run in an Elastic Beanstalk container that hosts generic Docker containers.
Is there a way to get the index of the instance so I can combine this with a constant? Something like Production-1, Production-2, etc.
The config I'm using looks like this:
packages:
yum:
newrelic-sysmond: []
rpm:
newrelic: http://yum.newrelic.com/pub/newrelic/el5/x86_64/newrelic-repo-5-3.noarch.rpm
commands:
"01":
command: nrsysmond-config --set license_key=`/opt/elasticbeanstalk/bin/get-config environment | jq .NEW_RELIC_LICENSE_KEY | sed -e 's/"//g'`
"02":
command: echo hostname=`/opt/elasticbeanstalk/bin/get-config environment | jq .RAILS_ENV | sed -e 's/"//g'` >> /etc/newrelic/nrsysmond.cfg
"03":
command: usermod -a -G docker newrelic
"04":
command: /etc/init.d/newrelic-sysmond restart
This works great, but sets all hostnames to the same. I don't want to use the Elastic Beanstalk hostname as they change every time the instances scale. This clogs a new relic with dead specimens.
This is on 64 bit Amazon Linux 2015.03 v1.4.3 running Docker 1.6.2
+3
source to share
1 answer
I found a reliable way to determine the current elastic beanstalk index instance.
"02":
command: echo `ec2-describe-tags --filter key=Name | grep \`curl -sq http://169.254.169.254/latest/meta-data/instance-id\`` | awk '{print $5}' >> /etc/newrelic/environment-name
"03":
command: aws elasticbeanstalk describe-environment-resources --environment-name `cat /etc/newrelic/environment-name` --region us-east-1 | jq '.EnvironmentResources.Instances' | ruby -e "require 'json'; puts JSON.parse(ARGF.read).find_index({'Id' => '$(curl -sq http://169.254.169.254/latest/meta-data/instance-id)'})" >> /etc/newrelic/instance-index
"04":
command: echo hostname=`/opt/elasticbeanstalk/bin/get-config environment | jq .RAILS_ENV | sed -e 's/"//g'``cat /etc/newrelic/instance-index` >> /etc/newrelic/nrsysmond.cfg
The idea is this:
- Get environment name with elastic beanstalk
- Get a list of instances for this environment
- Find the current instance at this index
- Set the New Relic hostname to RAILS_ENV-index
Hope this helps someone else trying to figure out which Elastic Beanstalk instance you are using.
+3
source to share