如何在 AWS CloudFormation 的同一個父堆疊中的巢狀堆疊之間傳遞值?

2 分的閱讀內容
0

我想在 AWS CloudFormation 的同一個父堆疊中的兩個巢狀堆疊之間傳遞或共用一個值。

簡短說明

解決方法假設如下:

  • 您有兩個巢狀堆疊,NestedStackANestedStackB,它們是相同的父堆疊一部分。
  • 您想要在 NestedStackB 中使用來自 NestedStackA 值。

解決方法

  1. 在 FSPIn 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.
  1. 在目的地堆疊的 FSPCreate 參數 ( 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.FSPPass 從 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 年前