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

Last updated: 2020-06-12

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.    In 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.    Create 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.    Pass the output value from NestedStackA as the parameter value for NestedStackB. To access this value in the parent stack, use the Fn::GetAtt function with 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

Did this article help you?

Anything we could improve?


Need more help?