From within a Beanstalk instance, how can I programmatically retrieve the URL for my environment?

I need to know this because the instances need to know whether they are running in production or

I couldn't find it using Platform Scripts /opt/elasticbeanstalk/bin/get-config ... or Instance Metadata with curl


As far as I know you can only get this from the Elastic Beanstalk API, as follows;

aws elasticbeanstalk describe-environments \
    --region <your-region> \
    --environment-names <your-environment-name> \
    --query "Environments[0].CNAME"

The above will output;

  • Yeah that kind of works - but not readily from within the instance though. Just get an empty array environments. Probably because it's not authenticated... Dec 7 '18 at 13:26
  • 1
    The IAM role that's associated to the EC2 instance needs to have the "elasticbeanstalk:DescribeEnvironments" permission.
    – dannosaur
    Dec 7 '18 at 13:28

Thanks to the hints from @dannosaur I found the solution. I'll post a new answer since there is a lot more to mention.

The Elastic Beanstalk API command aws elasticbeanstalk describe-environments --region <your-region> --environment-id <your-environment-id> gives you lots of good information. Or leave out the --environment-id to describe all your environments. To get the URL specifically we can do:

aws elasticbeanstalk describe-environments --region us-east-2 --environment-id e-sfmsdjhtwn --query "Environments[0].CNAME"

But, for this to work we need to grant an extra permission to the IAM role that is associated with the EC2 instance. So go to IAM > Roles > aws-elasticbeanstalk-ec2-role > Add inline policy. Select the following: Service=Eleastic Beanstalk, Actions=DescribeEnvironments, Resources=All resources. Complete creating the policy.

And also, this requires us to know the environment ID our current instance is in. But we can even find that programmatically in /etc/elasticbeanstalk/.aws-eb-stack.properties (requires root access).

So with the use of .ebextensions we can now act upon this in our deployment scripts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.