5

我们正在尝试在s3中存储环境特定的应用程序配置文件。 这些文件存储在以环境命名的不同子目录中,并且还将环境作为文件名的一部分。AWS Elastic Beanstalk:如何在ebextensions中使用环境变量?

实例是

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

弹性豆茎环境被命名为dev的,STG,PRD以及或者我也有在弹性豆茎名为环境中定义的环境变量可以是dev的,STG或PRD

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

我试图部署在使用中.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. 

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

回答

4

我努力得到这个工作,直到我发现子功能不会出现在ebextensions可用:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions-functions.html

这意味着您需要回退到Fn::JoinRef,至少在支持Sub引入ebextensions之前。它也似乎是文件属性需要一个固定的路径(我不能在这种情况下使用Fn::Join)。

我对这个整体解决方案如下:在旁部署的应用程序实例的config目录

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文件(不包括环境名称预选赛)。

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

1

你几乎有.ebextensions使用YAML格式,而你试图使用JSON。使用Ref: AWSEBEnvironmentName。 此外,你可以采取Sub功能的优势,避免讨厌的Join

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

1

.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 

以及访问环境变量使用Fn::GetOptionSetting。环境变量在aws:elasticbeanstalk:application:environmentnamespace

下面示例访问可变ENVIRONMENT的环境中source选项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 
相关问题