如何在 AWS CloudFormation 中的同一个父级堆栈内的嵌套堆栈之间传递值?
上次更新时间:2020 年 6 月 12 日
我想要在 AWS CloudFormation 中的同一个父级堆栈内的两个嵌套堆栈之间传递或分享值。
简短描述
此解决方法假设如下:
您有 NestedStackA 和 NestedStackB 两个嵌套堆栈,它们都属于同一个父级堆栈。 您想要将 NestedStackA 的值用在 NestedStackB 中。解决方法
1.在您的 AWS CloudFormation 模板中,传递您想要作为源堆栈 (NestedStackA) 中的输出分享的值。请参阅以下 JSON 和 YAML 示例。
JSON:
"Outputs": {
"SharedValueOutput": {
"Value": "yourValue",
"Description": "You can refer to any resource from the template."
}
}
YAML:
Outputs:
SharedValueOutput:
Value: yourValue
Description: You can refer to any resource from the template.
2.在目标堆栈 (NestedStackB) 中创建参数。请参阅以下 JSON 和 YAML 示例。
JSON:
"Parameters": {
"SharedValueParameter": {
"Type": "String",
"Description": "The shared value will be passed to this parameter by the parent stack."
}
}
YAML:
Parameters:
SharedValueParameter:
Type: String
Description: The shared value will be passed to this parameter by parent stack.
3.将 NestedStackA 中的输出值作为 NestedStackB 的参数值传递。要在父级堆栈中访问此值,请使用 Fn::GetAtt 函数与 NestedStackA 的逻辑名称和 Outputs.NestedStackOutputName 格式的输出值名称。请参阅以下 JSON 和 YAML 示例。
JSON:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"NestedStackA": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "<S3 URL for the template>"
}
},
"NestedStackB": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "<S3 URL for the template>",
"Parameters": {
"SharedValueParameter": {
"Fn::GetAtt":
}
}
}
}
}
}
YAML:
AWSTemplateFormatVersion: 2010-09-09
Resources:
NestedStackA:
Type: 'AWS::CloudFormation::Stack'
Properties:
TemplateURL: <S3 URL for the template>
NestedStackB:
Type: 'AWS::CloudFormation::Stack'
Properties:
TemplateURL: <S3 URL for the template>
Parameters:
SharedValueParameter:
Fn::GetAtt:
- NestedStackA
- Outputs.SharedValueOutput