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

AWS Elastic Beanstalk: How to use environment variables in ebextensions?

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

例子是

dev/application-dev.properties
stg/application-stg.properties
prd/application-prd.properties

Elastic Beanstalk 环境被命名为 dev, stg, prd 或者我也在 E​​lastic Beanstalk 中定义了一个名为 ENVIRONMENT 的环境变量,它可以是 dev, stg 或 prd

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

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

.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

我得到的错误是:

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.

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

你快到了。ebextensions 正在使用 YAML 格式,而你正在尝试使用 JSON。使用 Ref: AWSEBEnvironmentName。 此外,您可以利用 Sub 函数来避免讨厌的 Join:

!Sub "/config/application-${AWSEBEnvironmentName}.properties"

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

这意味着您需要回退到 Fn::JoinRef,至少在 ebextensions 引入对 Sub 的支持之前。文件属性似乎也需要固定路径(我不能在这种情况下使用 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

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

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

您的 .ebextensions 配置文件几乎是正确的。用环境变量或 AWS 资源名称替换文件名是行不通的,因为在 Mark 的回答中重命名在 container_commands 部分中创建的文件。

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

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

并使用 Fn::GetOptionSetting. Environment variables are in aws:elasticbeanstalk:application:environment namespace.

访问环境变量

下面的示例在 filessource 选项中访问环境变量 ENVIRONMENT:

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