How do I pass values between nested stacks within the same parent stack in AWS CloudFormation?

2 minute read
0

I want to pass or share a value between two nested stacks within the same parent stack in AWS CloudFormation.

Short description

The resolution assumes the following:

  • You have two nested stacks, NestedStackA and NestedStackB, that are part of the same parent stack.
  • You want to use a value from NestedStackA in NestedStackB.

Resolution

1.FSPIn your AWS CloudFormation template, pass the value that you want to share as an output in your source stack ( NestedStackA). See the following JSON and YAML examples.

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.FSPCreate a parameter in the destination stack ( NestedStackB). See the following JSON and YAML examples.

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 the output value from NestedStackA as the parameter value for NestedStackB. To access this value in the parent stack, use the Fn::GetAtt function. Use the logical name of NestedStackA and the name of the output value in Outputs.NestedStackOutputName format. See the following JSON and YAML examples.

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

Related information

Working with nested stacks

AWS::CloudFormation::Stack

AWS CloudFormation template snippets

AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago