AWS Elastic Beanstalk:如何在ebextensions中使用环境变量?

Ber*_*enz 9 environment-variables amazon-web-services amazon-elastic-beanstalk ebextensions

我们正在尝试在s3中存储特定于环境的应用程序配置文件.这些文件存储在不同的子目录中,这些子目录以环境命名,并且还将环境作为文件名的一部分.

例子是

dev/application-dev.properties
stg/application-stg.properties
prd/application-prd.properties
Run Code Online (Sandbox Code Playgroud)

Elastic Beanstalk环境名为dev,stg,prd,另外我还有一个名为ENVIRONMENT的Elastic Beanstalk中定义的环境变量,可以是dev,stg或prd.

我现在的问题是,当从.ebextensions中的配置文件下载配置文件时,如何引用环境名称或ENVIRONMENT变量?

我尝试{"Ref": "AWSEBEnvironmentName" }在.ebextensions/myapp.config中使用引用,但在部署时遇到语法错误.

.ebextensions/myapp.config的内容是:

files:
  /config/application-`{"Ref": "AWSEBEnvironmentName" }`.properties:
    mode: "000666"
    owner: webapp
    group: webapp
    source: https://s3.amazonaws.com/com.mycompany.mybucket/`{"Ref": "AWSEBEnvironmentName" }`/application-`{"Ref": "AWSEBEnvironmentName" }`.properties 
    authentication: S3Access

Resources:
  AWSEBAutoScalingGroup:
    Metadata:
      AWS::CloudFormation::Authentication:
        S3Access:
          type: S3
          roleName: aws-elasticbeanstalk-ec2-role
          buckets: com.mycompany.api.config
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

The configuration file .ebextensions/myapp.config in application version
manualtest-18 contains invalid YAML or JSON. YAML exception: Invalid Yaml: 
mapping values are not allowed here in "<reader>", line 6, column 85: 
... .config/stg/application-`{"Ref": "AWSEBEnvironmentName" }`.prop ... ^ , 
JSON exception: Invalid JSON: Unexpected character (f) at position 0.. 
Update the configuration file.
Run Code Online (Sandbox Code Playgroud)

在AWS Elastic Beanstalk中引用.ebextensions配置文件中的环境变量的正确方法是什么?

小智 6

我努力让这个工作,直到我发现Sub函数似乎在ebextensions中不可用:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions-functions.html

这意味着你需要回落至Fn::JoinRef,至少要等到支持Sub引入ebextensions.似乎files属性需要一个固定的路径(我不能Fn::Join在这个上下文中使用).

我对此的总体解决方案如下:

Resources:
  AWSEBAutoScalingGroup:
    Metadata:
      AWS::CloudFormation::Authentication:
        S3Auth:
          type: S3
          buckets: arn:aws:s3:::elasticbeanstalk-xxx
          roleName: aws-elasticbeanstalk-ec2-role

files:
  "/tmp/application.properties" :
    mode: "000644"
    owner: root
    group: root
    source: { "Fn::Join" : ["", ["https://s3-xxx.amazonaws.com/elasticbeanstalk-xxx/path/to/application-", { "Ref" : "AWSEBEnvironmentName" }, ".properties" ]]}
    authentication: S3Auth

container_commands:
  01-apply-configuration:
    command: mkdir -p config && mv /tmp/application.properties config
Run Code Online (Sandbox Code Playgroud)

这将导致在已部署的应用程序实例旁边application.propertiesconfig目录中生成一个文件(没有环境名称限定符).

如果要使用此方法将环境名称保留为文件名的一部分,则需要调整移动文件的命令以使用另一个Fn::Join表达式来控制文件名.


Cha*_*hew 6

您的.ebextensions配置文件几乎正确。将文件名替换为环境变量或AWS资源名称将无法正常工作,因为这样做的方法与Mark的答案中一样重命名本container_commands节中创建的文件。

source尝试使用Ref访问AWS资源名称的选项值是正确的,只需用单引号将其括起来',如下所示:

files:
  /config/application.properties:
    mode: "000666"
    owner: webapp
    group: webapp
    source: 'https://s3.amazonaws.com/com.mycompany.mybucket/`{"Ref": "AWSEBEnvironmentName" }`/application-`{"Ref": "AWSEBEnvironmentName" }`.properties'
   authentication: S3Access
Run Code Online (Sandbox Code Playgroud)

要访问环境变量,请使用Fn :: GetOptionSetting。环境变量位于aws:elasticbeanstalk:application:environment 名称空间中

下面的示例ENVIRONMENTsource选项中访问环境变量files

files:
  "/tmp/application.properties" :
    mode: "000666"
    owner: webapp
    group: webapp
    source: 'https://s3.amazonaws.com/com.mycompany.mybucket/`{"Ref": "AWSEBEnvironmentName" }`/application-`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "ENVIRONMENT ", "DefaultValue": "dev"}}`.properties'
    authentication: S3Auth
Run Code Online (Sandbox Code Playgroud)