我想从 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'
最佳答案
这个功能并不像人们希望的那么简洁。您必须实际传递要从参数存储中查找的每个参数的名称/路径。
模板:
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}
如果您不向模板传递任何参数,BucketNameSuffix
将使用存储在 /example/dev/BucketNameSuffix
中的值填充。例如,如果您想使用 prod
值(由 /example/prod/BucketNameSuffix
指向),那么您应该为参数 BucketNameSuffix
指定值code>,但您应该传递要使用的参数的替代名称,而不是传递实际值,因此您需要传递 /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
环境默认
将是/example/prod/BucketNameSuffix
,然后我可以更新prod
堆栈而不传递任何参数。
关于amazon-web-services - 云信息和参数存储 : How to select parameter for the environment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48705041/