How to set an environment variable in Amazon Elastic Beanstalk (Python)

Option 1:

You can set environment variables using eb setenv FOO=bar

You can view the environment variables using eb printenv

Option 2:

You can create a config file in your .ebextensions directory, for example 00_environment.config. Then, add your environment variables like this:

option_settings: - option_name: MY_FIRST_ENV_VAR value: abc - option_name: ANOTHER_ENV_VAR value: 123

However, if you have multiple environments, I have found that it is more useful to set the environment variables directly, using option #1.

I also have found the eb config commands to be helpful: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-config.html

These commands allow you to get, put, list, or delete configuration files on your eb environment.

The command eb config get will save your config, including environment variables, to a local file in .elasticbeanstalk/saved_configs.


I was having the same problem.

Believe it or not, you have to commit the .ebextensions directory and all *.config files to version control before you deploy in order for them to show up as environment variables on the server.

In order to keep sensitive information out of version control, you can use a config file like this:

option_settings:
  - option_name: API_LOGIN
    value: placeholder
  - option_name: TRANS_KEY
    value: placeholder
  - option_name: PROVIDER_ID
    value: placeholder

Then edit the configuration in the AWS admin panel (Configuration > Software Configuration > Environment Properties) and update the values there.

You may also find this answer helpful.


I've checked using a modern (i.e., non legacy) container, and found it under /opt/elasticbeanstalk/deploy/configuration/containerconfiguration as a json file.

The Behaviour seems to be Platform-Dependent: I remember in PHP in particular, it also creates some shell scripts with the values.

Regardless of that, look into /opt/elasticbeanstalk/hooks/configdeploy.

Java case again, it runs this python script, which looks quite handy for you:

https://gist.github.com/19c1e4b718f9a70a4ce1


I did the following to also get my environment variables that I configure in cloudformation in the non-container phase, eg the regular commands

/opt/elasticbeanstalk/bin/get-config environment | python -c "import json,sys; obj=json.load(sys.stdin); f = open('/tmp/eb_env', 'w'); f.write('\n'.join(map(lambda x: 'export ' + x[0] + '=' + x[1], obj.iteritems())))"

Once you execute this command you will have a file in /tmp/eb_env with all your environment variables. Just execute the following before a command that needs the environment variables

source /tmp/eb_env

Example

source /tmp/eb_env && echo $MY_CUSTOM_ENV

In the config file of elastic beanstalk, it looks like this:

commands:
    02-make-sure-we-can-get-our-env-in-the-instance-itself:
        command: "/opt/elasticbeanstalk/bin/get-config environment | python -c 'import json,sys; obj=json.load(sys.stdin); f = open(\'/tmp/eb_env\', \'w\'); f.write(\'\n\'.join(map(lambda x: \'export \' + x[0] + \'=\' + x[1], obj.iteritems())))'"