14

I have been working on a Django application lately, trying to get it to work with Amazon Elastic Beanstalk.

In my .ebextensions/python.config file, I have set the following:

option_settings:
  - namespace: aws:elasticbeanstalk:application:environment
    option_name:  ProductionBucket
    value: s3-bucket-name
  - namespace: aws:elasticbeanstalk:application:environment
    option_name:  ProductionCache
    value:  memcached-server.site.com:11211

However, whenever I look on the server, no such environment variables are set (and as such, aren't accessible when I try os.getenv('ProductionBucket')

I came across this this page which appears to attempt to document all the namespaces. I've also tried using PARAM1 as the option name, but have had similar results.

How can I set environment variables in Amazon Elastic Beanstalk?

EDIT:
I have also tried adding a command prior to all other commands which would just export an environment variable:

commands:
 01_env_vars:
  command: "source scripts/env_vars"

... This was also unsuccessful

Steffen Opel
  • 63,899
  • 11
  • 192
  • 211
NT3RP
  • 15,262
  • 9
  • 61
  • 97
  • Maybe the PARAM1 etc ones are used for this? Seems like a big limitation though. http://stackoverflow.com/questions/11211007/how-do-you-pass-environment-variable-on-amazon-elastic-beanstalk-php – Alex L Jan 08 '13 at 01:29
  • This might help too - http://grigory.ca/2012/09/getting-started-with-django-on-aws-elastic-beanstalk/ – Alex L Jan 08 '13 at 01:31
  • Have you tried using `aws:elasticbeanstalk:application:environment:varname`? – Alex L Jan 08 '13 at 01:33
  • I have tried those methods and have likewise been unsuccessful :S – NT3RP Jan 08 '13 at 02:12
  • I'll have a try later this week when I'm working on AWS EB, will let you know how I go. – Alex L Jan 08 '13 at 02:53
  • I am having the same problem on a Ruby on Rails platform – idrinkpabst Apr 12 '13 at 00:29

6 Answers6

19

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.

ordonezalex
  • 2,645
  • 1
  • 20
  • 33
idrinkpabst
  • 1,838
  • 23
  • 25
  • @idrinkpabst afaik, .ebextensions can be ignored by version control (it prob should in case you have some sensitive info in any config file) when you deploy if you use an .ebignore file. see https://forums.aws.amazon.com/message.jspa?messageID=621059#621059 – arturomp Oct 11 '17 at 20:16
6

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.

trex
  • 231
  • 3
  • 3
  • How does eb deploy know to use 00_environment.config and not 01_environment.config? – Learner Jan 21 '17 at 06:15
  • @Learner it loads all `.ebextensions/*.config` files in alphabetical order. See [the documentation](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options.html#configuration-options-precedence): `Configuration files are executed in alphabetical order. For example, .ebextensions/01run.config is executed before .ebextensions/02do.config.` – Brandon Aug 05 '17 at 20:26
5

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())))'"
user16423
  • 51
  • 1
  • 1
2

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

aldrinleal
  • 3,559
  • 26
  • 33
  • 1
    While that certainly sheds some light as to how the environment variables are set, I was hoping that there might be some information regarding doing this through the configuration files. – NT3RP Jan 09 '13 at 21:50
  • Look into the ```/hooks/configdeploy``` folder contents. This file is ran whenever you set the environment settings - including the configuration. You must log and look into your python-based environment, as its a different beast – aldrinleal Jan 09 '13 at 22:26
1

To set variables on a local run, you can do the following:

eb local setenv CONFIG=dev
eb local run

This also works with Docker MultiContainers, which otherwise will not see your environment.

Pieter Ennes
  • 2,301
  • 19
  • 21
1

I know this is an old question but for those who still have the same question like I did here is the solution from AWS documentation: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environments-cfg-softwaresettings.html

To configure environment properties in the Elastic Beanstalk console

  1. Open the Elastic Beanstalk console, and then, in the regions drop-down list, select your region.

  2. In the navigation pane, choose Environments, and then choose your environment's name on the list.

  3. In the navigation pane, choose Configuration.

  4. In the Software configuration category, choose Edit.

  5. Under Environment properties, enter key-value pairs.

  6. Choose Apply.
Anatol
  • 3,720
  • 2
  • 20
  • 40