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

风格不统一 提交于 2019-11-28 23:39:12

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

idrinkpabst

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

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 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())))'"

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!