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
To set variables on a local run, you can do the following:
This also works with Docker MultiContainers, which otherwise will not see your environment.
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 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 environmental variables on the server.
In order to keep your sensitive information out of version control you can use a config file like this:
and then later edit the AWS admin panel (Environment Details -> Edit Configuration -> Container) and update the values there
https://stackoverflow.com/a/14491294/274695
I did the following to also get my environment variables that I configure in cloudformation in the non-container phase, eg the regular commands
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
Example
In the config file of elastic beanstalk, it looks like this:
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.htmlThese 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
.