如何在Amazon Elastic Beanstalk(Python)中设置环境变量

时间:2022-03-02 12:16:21

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

我最近一直在研究Django应用程序,试图让它与Amazon Elastic Beanstalk一起使用。

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

在我的.ebextensions / python.config文件中,我设置了以下内容:

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

但是,每当我查看服务器时,都没有设置这样的环境变量(因此,当我尝试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.

我遇到了这个页面似乎试图记录所有命名空间。我也尝试使用PARAM1作为选项名称,但也有类似的结果。

How can I set environment variables in Amazon Elastic Beanstalk?

如何在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 个解决方案

#1


1  

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

我已经使用现代(即非传统)容器进行了检查,并将其作为json文件在/ opt / elasticbeanstalk / deploy / configuration / containerconfiguration下找到。

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

行为似乎是平台依赖的:我特别记得在PHP中,它还创建了一些带有值的shell脚本。

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

无论如何,请查看/ opt / elasticbeanstalk / hooks / configdeploy。

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

Java案例,它运行这个python脚本,看起来非常方便你:

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

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

#2


15  

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.

信不信由你,你必须在部署之前将.ebextensions目录和所有* .config文件提交到版本控制,以便它们在服务器上显示为环境变量。

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

然后编辑AWS管理面板(环境详细信息 - >编辑配置 - >容器)并更新其中的值

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

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

#3


4  

Option 1:

选项1:

You can set environment variables using eb setenv FOO=bar

您可以使用eb setenv FOO = bar设置环境变量

You can view the environment variables using eb printenv

您可以使用eb printenv查看环境变量

Option 2:

选项2:

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

您可以在.ebextensions目录中创建配置文件,例如00_environment.config。然后,添加如下环境变量:

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

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

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

但是,如果您有多个环境,我发现使用选项#1直接设置环境变量更有用。

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

我也发现eb config命令很有帮助: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.

这些命令允许您在eb环境中获取,放置,列出或删除配置文件。

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

命令eb config get会将您的配置(包括环境变量)保存到.elasticbeanstalk / saved_configs中的本地文件中。

#4


2  

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

我做了以下操作来获取我在非容器阶段的cloudformation中配置的环境变量,例如常规命令

/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

执行此命令后,/ tmp / eb_env中将包含一个包含所有环境变量的文件。只需在需要环境变量的命令之前执行以下命令

source /tmp/eb_env

Example

source /tmp/eb_env && echo $MY_CUSTOM_ENV

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

在弹性beanstalk的配置文件中,它看起来像这样:

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

#5


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.

这也适用于Docker MultiContainers,否则它将无法看到您的环境。

#1


1  

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

我已经使用现代(即非传统)容器进行了检查,并将其作为json文件在/ opt / elasticbeanstalk / deploy / configuration / containerconfiguration下找到。

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

行为似乎是平台依赖的:我特别记得在PHP中,它还创建了一些带有值的shell脚本。

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

无论如何,请查看/ opt / elasticbeanstalk / hooks / configdeploy。

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

Java案例,它运行这个python脚本,看起来非常方便你:

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

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

#2


15  

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.

信不信由你,你必须在部署之前将.ebextensions目录和所有* .config文件提交到版本控制,以便它们在服务器上显示为环境变量。

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

然后编辑AWS管理面板(环境详细信息 - >编辑配置 - >容器)并更新其中的值

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

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

#3


4  

Option 1:

选项1:

You can set environment variables using eb setenv FOO=bar

您可以使用eb setenv FOO = bar设置环境变量

You can view the environment variables using eb printenv

您可以使用eb printenv查看环境变量

Option 2:

选项2:

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

您可以在.ebextensions目录中创建配置文件,例如00_environment.config。然后,添加如下环境变量:

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

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

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

但是,如果您有多个环境,我发现使用选项#1直接设置环境变量更有用。

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

我也发现eb config命令很有帮助: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.

这些命令允许您在eb环境中获取,放置,列出或删除配置文件。

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

命令eb config get会将您的配置(包括环境变量)保存到.elasticbeanstalk / saved_configs中的本地文件中。

#4


2  

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

我做了以下操作来获取我在非容器阶段的cloudformation中配置的环境变量,例如常规命令

/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

执行此命令后,/ tmp / eb_env中将包含一个包含所有环境变量的文件。只需在需要环境变量的命令之前执行以下命令

source /tmp/eb_env

Example

source /tmp/eb_env && echo $MY_CUSTOM_ENV

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

在弹性beanstalk的配置文件中,它看起来像这样:

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

#5


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.

这也适用于Docker MultiContainers,否则它将无法看到您的环境。