Skip to content

Instantly share code, notes, and snippets.

@cdechery
Created May 16, 2018 19:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdechery/992ad242e2a7491e27100fb4baa1bd59 to your computer and use it in GitHub Desktop.
Save cdechery/992ad242e2a7491e27100fb4baa1bd59 to your computer and use it in GitHub Desktop.
Retreive Beanstalk Application name from within EC2 instance
packages:
yum:
jq: []
container_commands:
01-getenv-info:
command: |
ENV_ID=`{"Ref": "AWSEBEnvironmentId" }`
ENV_NAME=`{"Ref": "AWSEBEnvironmentName" }`
echo "env.id="$ENV_ID > /tmp/awseb.properties
echo "env.name="$ENV_NAME >> /tmp/awseb.properties
EC2_REGION=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/[a-z]$//'`
APP_NAME=`aws elasticbeanstalk describe-environments --environment-ids $ENV_ID --region=$EC2_REGION | jq -r '.Environments[0].ApplicationName'`
echo "app.name="$APP_NAME >> /tmp/awseb.properties
chmod 644 /tmp/awseb.properties
@cdechery
Copy link
Author

I know this solution is SUPER ugly, but it works. It took me hours of searching online for the various pieces that composed this horrible piece of code, that in the end produces the file with all the info I need.

@danimality
Copy link

I know it doesn't make it /much/ less ugly but you can avoid installing jq if you use the --query flag in the AWS CLI which essentially does the same thing as jq (just remove the leading dot I think for this case). Otherwise this is super useful, thanks!

@sagimann
Copy link

Is there a permission requirement here?
describe-environments seems to return an empty array..

@FilipDusek
Copy link

FilipDusek commented Apr 28, 2020

Is there a permission requirement here?
describe-environments seems to return an empty array..

I had the same problem and adding AWSElasticBeanstalkReadOnlyAccess permission to the user fixed the problem. Something more restrictive would probably be better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment