12

I'm using elastic beanstalk to deploy a Django app. I'd like to SSH on the EC2 instance to execute some shell commands but the environment variables don't seem to be there. I specified them via the AWS GUI (configuration -> environment properties) and they seem to work during the boot-up of my app.

I tried activating and deactivating the virtual env via:

source /var/app/venv/*/bin/activate

Is there some environment (or script I can run) to access an environment with all the properties set? Otherwise, I'm hardly able to run any command like python3 manage.py ... since there is no settings module configured (I know how to specify it manually but my app needs around 7 variables to work).

Xen_mar
  • 8,330
  • 11
  • 51
  • 74

3 Answers3

16

During deployment, the environment properties are readily available to your .platform hook scripts.

After deployment, e.g. when using eb ssh, you need to load the environment properties manually.

One option is to use the EB get-config tool. The environment properties can be accessed either individually (using the -k option), or as a JSON or YAML object with key-value pairs.

For example, one way to export all environment properties would be:

export $(/opt/elasticbeanstalk/bin/get-config --output YAML environment | 
         sed -r 's/: /=/' | xargs)

Here the get-config part returns all environment properties as YAML, the sed part replaces the ': ' in the YAML output with '=', and the xargs part fixes quoted numbers.

Note this does not require sudo.

Alternatively, you could refer to this AWS knowledge center post:

Important: On Amazon Linux 2, all environment properties are centralized into a single file called /opt/elasticbeanstalk/deployment/env. You must use this file during Elastic Beanstalk's application deployment process only. ...

The post describes how to make a copy of the env file during deployment, using .platform hooks, and how to set permissions so you can access the file later.

You can also perform similar steps manually, using SSH. Once you have the copy set up, with the proper permissions, you can source it.

Beware:

Note: Environment properties with spaces or special characters are interpreted by the Bash shell and can result in a different value.

djvg
  • 11,722
  • 5
  • 72
  • 103
  • 1
    Also see these answers: https://stackoverflow.com/a/62888738, https://stackoverflow.com/a/62212223 – djvg Aug 03 '21 at 08:35
  • What is the solution for env vars with special characters? Does wrapping them in quotes work? – Daniel Sep 13 '21 at 22:23
  • @Daniel: Not sure. A quick search turned up some related discussions that mention e.g. [using \\\ or quotes to escape special characters](https://stackoverflow.com/q/37619164), or [using base64 encoding](https://stackoverflow.com/q/64519029), but I haven't tried any of those yet. – djvg Sep 14 '21 at 07:05
9

Try running the command /opt/elasticbeanstalk/bin/get-config environment after you ssh into the EC2 instance.

Zufra
  • 574
  • 1
  • 7
  • 16
0

If you are trying to access the environment variables in eb script elastic beanstalk Use this

$(/opt/elasticbeanstalk/bin/get-config environment -k ENVURL)
{ "Ref" : "AWSEBEnvironmentName" }
    $(/opt/elasticbeanstalk/bin/get-config environment -k ENVURL)