gpt4 book ai didi

amazon-web-services - 将安全 SSM 参数传递到嵌套的 CloudFormation 堆栈

转载 作者:行者123 更新时间:2023-12-04 12:25:02 24 4
gpt4 key购买 nike

我有一个嵌套的 CloudFormation 堆栈模板,用于描述数据库及其相关资源。我需要为各种环境创建多个数据库(例如 stageqaprod )。

我将每个环境的数据库密码作为安全字符串存储在 SSM 参数存储中,例如:

  • /acme/stage/DB_PASSWORD
  • /acme/qa/DB_PASSWORD
  • /acme/prod/DB_PASSWORD

我尝试解析父模板中的 SSM 参数并将其传递到嵌套堆栈,但看起来不起作用。

# parent.template

StageDatabaseStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: "…/database.template"
Parameters:
DatabasePassword: '{{resolve:ssm-secure:/acme/stage/DB_PASSWORD:1}}'

QaDatabaseStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: "…/database.template"
Parameters:
DatabasePassword: '{{resolve:ssm-secure:/acme/qa/DB_PASSWORD:1}}'

ProdDatabaseStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: "…/database.template"
Parameters:
DatabasePassword: '{{resolve:ssm-secure:/acme/prod/DB_PASSWORD:1}}'


# database.template

AWSTemplateFormatVersion: "2010-09-09"

Parameters:
DatabasePassword:
Type: String

Resources:
Database:
Type: AWS::RDS::DBInstance
Properties:
MasterUserPassword: !Ref DatabasePassword

这种方法给了我一个错误:

An error occurred (ValidationError) when calling the CreateStack operation: SSM Secure reference is not supported in: [AWS::CloudFormation::Stack/Properties/Parameters/DatabasePassword]


根据the docs ,也可以将密码参数定义为特殊的 AWS::SSM::Parameter::Value<String>类型,但据说:

AWS CloudFormation does not support defining template parameters as SecureString Systems Manager parameter types.

我实际上尝试过使用它,但它给了我一个错误:

Parameters [/acme/stage/DB_PASSWORD] referenced by template have types not supported by CloudFormation.


那么,在这种情况下如何将安全数据库密码传递给数据库资源?我想使用单个 CloudFormation 模板来管理所有三个数据库及其相关资源。

最佳答案

将安全 SSM 参数传递到我发现的嵌套堆栈的唯一方法是将其作为字符串传递,而不是尝试使用更明智的 AWS::SSM::Parameter::Value<String>然后通过使用 !Sub 构建动态引用来解析嵌套堆栈中的安全值功能。

这是一个工作示例:

# parent.template

StageDatabaseStack:
Type: AWS::CloudFormation::Stack
Properties:
Parameters:
DatabasePasswordSsmKey: "/acme/stage/DB_PASSWORD:1"

ProdDatabaseStack:
Type: AWS::CloudFormation::Stack
Properties:
Parameters:
DatabasePasswordSsmKey: "/acme/prod/DB_PASSWORD:1"
# database.template

Parameters:
DatabasePasswordSsmKey:
Description: SSM property key for database password
Type: String

Database:
Type: AWS::RDS::DBInstance
Properties:
MasterUserPassword: !Sub "{{resolve:ssm-secure:${DatabasePasswordSsmKey}}}"

关于amazon-web-services - 将安全 SSM 参数传递到嵌套的 CloudFormation 堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com