How to set an environment variable in Amazon Elast

2019-01-23 06:28发布

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

5条回答
We Are One
2楼-- · 2019-01-23 06:49

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.

查看更多
别忘想泡老子
3楼-- · 2019-01-23 06:59

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

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-23 07:02

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:

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

and then later edit the AWS admin panel (Environment Details -> Edit Configuration -> Container) and update the values there

https://stackoverflow.com/a/14491294/274695

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-23 07:04

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())))'"
查看更多
做个烂人
6楼-- · 2019-01-23 07:11

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.

查看更多
登录 后发表回答