如何在 AWS CloudFormation 中的同一个父级堆栈内的嵌套堆栈之间传递值?

2 分钟阅读
0

我想要在 AWS CloudFormation 中的同一个父级堆栈内的两个嵌套堆栈之间传递或分享值。

简短描述

此解决方法假设如下:

  • 您有 NestedStackANestedStackB 两个嵌套堆栈,它们都属于同一个父级堆栈。
  • 您想要将 NestedStackA 的值用在 NestedStackB 中。

解决方法

1.FSP 在您的 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.FSP 在目标堆栈 (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.FSP 将 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": [
              "NestedStackA",
              "Outputs.SharedValueOutput"
            ]
          }
        }
      }
    }
  }
}

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

相关信息

使用嵌套堆栈

AWS::CloudFormation::Stack

AWS CloudFormation 模板代码段

AWS 官方
AWS 官方已更新 2 年前