我们正在尝试将特定于环境的应用程序配置文件存储在s3中。这些文件存储在以环境命名的不同子目录中,并且将环境作为文件名的一部分。
例子是
dev/application-dev.properties
stg/application-stg.properties
prd/application-prd.properties
弹性豆茎环境被命名为dev、stg、PRD或者我也有一个在弹性豆茎中定义的环境变量,名为ENVIRONMENT,它可以是dev、stg或PRD。
我现在的问题是,当从. eb扩展中的配置文件下载配置文件时,如何引用环境名称或环境变量?
我尝试在. ebextsions/myapp.config中使用{"Ref":"AWSEB环境名称"}
引用,但在部署时出现语法错误。
. ebextsions/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.
什么是正确的方式引用一个环境变量在一个. eb扩展配置文件在AWS弹性豆茎?
您的配置文件几乎是正确的。将文件名替换为环境变量或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::GetOptionSsettings。环境变量位于aws: elasticbeanstatk:application:环境
命名空间中。
下面的示例访问files
的source
选项中的环境变量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
我努力让这个工作,直到我发现Sub函数似乎在eb扩展中不可用:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions-functions.html
这意味着您需要回退到Fn::Join
和Ref
,至少在ebExsionsions引入对Sub
的支持之前。似乎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
这将在部署的应用程序实例旁边的config
目录中生成一个application.properties
文件(没有环境名称限定符)。
如果要使用此方法将环境名称保留为文件名的一部分,则需要调整移动文件的命令以使用另一个Fn::Join
表达式来控制文件名。
您几乎就在那里。ebExsionsions使用YAML格式,而您尝试使用JSON。使用Ref: AWSEB环境名称
。此外,您可以利用Sub
函数来避免讨厌的Join
:
! Sub"/config/application-${AWSEB环境名称}.properties"