12

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

例子是

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

Elastic Beanstalk 环境被命名为dev、stg、prd,或者我还在 Elastic Beanstalk 中定义了一个名为 ENVIRONMENT 的环境变量,它可以是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

我得到的错误是:

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 配置文件中引用环境变量的正确方法是什么?

4

3 回答 3

8

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

这意味着您需要回退到Fn::Joinand Ref,至少在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

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

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

于 2017-05-19T00:58:05.463 回答
7

Your .ebextensions config file was almost correct. Substituting the file name with environment variable or AWS resource name won't work, for that do as in Mark's answer to rename the file created in container_commands section.

The source option value trying to access AWS resource name using Ref was correct, it just had to be surrounded by single quote ', like below:

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

And to access environment variables use Fn::GetOptionSetting. Environment variables are in aws:elasticbeanstalk:application:environment namespace.

Below example access an environment variable ENVIRONMENT in source option of 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
于 2017-12-14T16:48:15.990 回答
2

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

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

于 2017-03-01T22:21:40.423 回答