Share outputs from CloudFormation nested stack

Viewed 1256

I have one parent stack which calls to 2 nested stacks and I need to import values of these nested to the parent. Example:

NestedStack:

 "Outputs": {
    "TargetGroup":{ 
      "Value": {
        "Ref": "ggTG"
       },
       "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-TargetGroup"
                }
        }
    },
}

When I execute all nested stacks I get these output in the child stack but I would like to get this output in the parent stack to access from another independent stack.

The reason of that is because if I import in another independent stack I cant use the name of the nested because it is created at runtime.

StackImporting:

"TargetGroupARN" : {"Fn::ImportValue" : {"Fn::Sub" : "${StackName}-TargetGroup"}}

As I said, I only know the name of parent stack, so I must to export from parent and not in the child stack.

1 Answers

From AWS CloudFormation Template Snippets - AWS CloudFormation, it appears that you can reference outputs of nested stacks like this:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Resources" : {
        "myStack" : {
           "Type" : "AWS::CloudFormation::Stack",
           "Properties" : {
              "TemplateURL" : "https://s3.amazonaws.com/cloudformation-templates-us-east-1/S3_Bucket.template",
              "TimeoutInMinutes" : "60"
           }
        }
    },
    "Outputs": {
       "StackRef": {"Value": { "Ref" : "myStack"}},
       "OutputFromNestedStack" : {
             "Value" : { "Fn::GetAtt" : [ "myStack", "Outputs.BucketName" ] }
       }
    }
}

So, just use a normal Output in the nested stack (no need to Export), then reference it as above from the top-level stack.

Related