Is it possible to get metadata about the Elastic Beanstalk environment from the EC2 instance

6,537

Solution 1

I've figured something out. For those of you that are curious:

I can grab EC instance information via http://169.254.169.254/latest/meta-data/. In particular I want my EC2 instance id

I can determine a list of all my beanstalk environments via describe-environments

For each environment I can run describe-environment-resources. This call returns a list of instances, which I can match the current instance's instance id against. Thus I can figure out my environment name.

Finally, I can refer to the result of describe-environments to also determine a version label for the currently deployed code.

Before I can do any of this, I need to configure my ec2 instances to have access to the elastic beanstalk information. I can do this by assigning the right access policy to the role associated with my ec2 instances, and grabbing the authentication information, again via the instance metadata at http://169.254.169.254/latest/meta-data/

Since I'm using the python boto library, all of the operations i've described above already have pre-baked library functions to perform them for me.

guh

I haven't coded it yet, but if I can get it to work I'll post a snipped here

Edit working code

Solution 2

After a bit of digging around on the internet I found. I know the question is an old one, but I thought that I'd give this answer.

$ /opt/elasticbeanstalk/bin/get-config container -k environment_name

Solution 3

It's not documented, so this might break without warning, but EB's /opt/elasticbeanstalk/bin/get-config command reads from a file located at /opt/elasticbeanstalk/deploy/configuration/containerconfiguration that includes more data than is exposed by get-config.

Since it's formatted as JSON, and EB images include jq out of the box, this command will extract the environment name (may be access-restricted, so sudo or other access control mechanisms may be needed):

$ jq .system.environment_name /opt/elasticbeanstalk/deploy/configuration/containerconfiguration

One reason to prefer an offline approach over using describe-environments or describe-tags (EB defines a tag that includes the environment name) is that at scale your deployments may fail due to AWS API rate-limiting, which is not a fun way to wake up at 3am.

Share:
6,537

Related videos on Youtube

wjin
Author by

wjin

Updated on September 18, 2022

Comments

  • wjin
    wjin over 1 year

    I'd like to be able to extract information about the current elastic beanstalk environment from my EC2 instance, so that I can include this information in error emails sent from my servers.

    For instance, knowing the current environment's name, and the version label of the deployed code would all be useful. Does anyone know away to do this programatically? I know there is already an API for retrieving EC2 info such as public hostname and AMI-id etc, but I can't find anything for elastic beanstalk.

    Currently my solution is to manually set some environment variables that my app can read, but obviously this is cumbersome.

  • skeggse
    skeggse over 4 years
    This doesn't seem to be universally available, sadly. What EB platform did you discover that on?
  • pioto
    pioto almost 4 years
    This appears to work on Tomcat 8.5 w/ Java 8, at least.
  • skplunkerin
    skplunkerin about 2 years
    I don't see that file anymore, but found /opt/elasticbeanstalk/deployment/cfn-metadata-cache.json with Environment Name being found via jq '.EbResource."AWS::ElasticBeanstalk::Metadata".EnvironmentNa‌​me' /opt/elasticbeanstalk/deployment/cfn-metadata-cache.json