Fre*_*rik 9 amazon-web-services aws-cloudformation amazon-systems-manager
我想从我的 CloudFormation 模板中的参数存储中读取我的数据库的 URL。这对于单个 URL 来说很容易,但我不知道如何在不同的环境中更改 URL。
我有四个环境(开发、集成、预生产和生产),它们的详细信息存储在 Parameter Store 中的四个不同路径中:
/database/dev/url
/database/int/url
/database/ppe/url
/database/prod/url
Run Code Online (Sandbox Code Playgroud)
我现在想在通过 CloudFormation 部署时选择正确的数据库 URL。我怎样才能做到这一点?
/database/dev/url
/database/int/url
/database/ppe/url
/database/prod/url
Run Code Online (Sandbox Code Playgroud)
此功能并不像人们希望的那样简洁。您必须实际传递要从参数存储中查找的每个参数的名称/路径。
模板:
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}
Run Code Online (Sandbox Code Playgroud)
如果您不向模板传递任何参数,BucketNameSuffix
则将使用存储在/example/dev/BucketNameSuffix
. 例如,如果您想使用一个prod
值(指向 by /example/prod/BucketNameSuffix
),那么您应该为 parameter 指定值BucketNameSuffix
,但不是传递实际值,您应该传递要使用的参数的替代名称,因此您将传递/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
Run Code Online (Sandbox Code Playgroud)
一篇不太好的 AWS 博客文章:https : //aws.amazon.com/blogs/mt/integrating-aws-cloudformation-with-aws-systems-manager-parameter-store/
由于传递亿个无意义的参数看起来愚蠢的,我实际上可能产生的环境,特定的模板,并设置正确Default:
的所以生成的模板prod
环境Default
会/example/prod/BucketNameSuffix
,然后我可以更新prod
不传递任何参数堆栈。
您可以在此处使用Fn::Join。
这是一些伪代码。
您必须将环境作为参数,您已经这样做了。
在需要 DatabaseUrl 的资源中创建所需的字符串。
Resources :
Instance :
Type : 'AWS::Some::Resource'
Properties :
DatabaseURL : !Join [ "", [ "/database/", !Ref "Environment" , "/url ] ]
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
注意:您无法使用某些计算逻辑动态为参数赋值。定义参数的所有值都应作为输入给出。
归档时间: |
|
查看次数: |
7109 次 |
最近记录: |