我想从CloudFormation模板中的参数存储中读取数据库的URL。这对于单个URL来说很简单,但是我不知道如何在不同的环境中更改URL。
我有四个环境(开发、集成、预生产和生产),它们的详细信息存储在Parameter Store中的四个不同路径上:
/database/dev/url
/database/int/url
/database/ppe/url
/database/prod/url现在,我希望在通过CloudFormation部署时选择正确的数据库URL。我该怎么做呢?
Parameters:
Environment:
Type: String
Default: dev
AllowedValues:
- dev
- int
- ppe
- prod
DatabaseUrl:
Type: 'AWS::SSM::Parameter::Value<String>'
# Obviously the '+' operator here won't work - so what do I do?
Default: '/database/' + Environment + '/url'发布于 2018-12-26 04:57:33
这个功能并不像人们希望的那样整洁。您必须实际传递要从参数存储中查找的每个参数的名称/路径。
模板:
AWSTemplateFormatVersion: 2010-09-09
Description: Example
Parameters:
BucketNameSuffix:
Type: AWS::SSM::Parameter::Value<String>
Default: /example/dev/BucketNameSuffix
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub parameter-store-example-${BucketNameSuffix}如果您没有向模板传递任何参数,则将使用存储在/example/dev/BucketNameSuffix中的值填充BucketNameSuffix。比方说,如果您想要使用一个value值(由/example/prod/BucketNameSuffix指向),那么您应该为参数BucketNameSuffix指定prod,但是您应该传递要使用的参数的替代名称,而不是传递实际的值,因此您应该传递/example/prod/BucketNameSuffix。
aws cloudformation update-stack --stack-name example-dev \
--template-body file://./example-stack.yml
aws cloudformation update-stack --stack-name example-prod \
--template-body file://./example-stack.yml \
--parameters ParameterKey=BucketNameSuffix,ParameterValue=/example/prod/BucketNameSuffix关于这一点的一篇不太好的AWS博客文章:https://aws.amazon.com/blogs/mt/integrating-aws-cloudformation-with-aws-systems-manager-parameter-store/
因为传递一百万个无意义的参数看起来很愚蠢,所以我实际上可能会生成一个特定于环境的模板,并在生成的模板中设置正确的Default:,因此对于prod环境,Default将为/example/prod/BucketNameSuffix,然后我可以在不传递任何参数的情况下更新prod堆栈。
发布于 2018-02-09 22:32:10
你可以在这里使用Fn::Join。
下面是一些伪代码。
您将不得不将Environment作为一个参数,您已经在这样做了。
在需要DatabaseUrl的资源中创建所需的字符串。
Resources :
Instance :
Type : 'AWS::Some::Resource'
Properties :
DatabaseURL : !Join [ "", [ "/database/", !Ref "Environment" , "/url ] ]希望这能有所帮助。
注意:您不能使用某些计算逻辑动态地为参数赋值。定义的参数的所有值都应作为输入给出。
发布于 2018-02-16 01:36:54
我喜欢Fn::Sub,它更干净,更容易阅读。
!Sub "/database/${Environment}/url"https://stackoverflow.com/questions/48705041
复制相似问题