Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing name of parent Cloudformation stack in nested stack

I'm using a nested Cloudformation template structured like this:

masterTemplate -> childA -> childB

Each of the JSON template files is stored in S3 with a bucket name of "${masterStackName}-env-upload". This works fine from the parent template, as I can simply do:

"TemplateURL":  {
          "Fn::Join": [
            "",
            [
              "https://s3.amazonaws.com/",
              {
                "Ref": "AWS::StackName"
              },
              "-env-upload/device-specific-template-HALO-BUILD-VERSION.json"
            ]
          ]
        },

However, when childA attempts to do the same thing to launch the childB template, "AWS::StackName" becomes the generated name for childA - meaning that it is trying to access a non-existent bucket.

My question is: how can I pass down the name of the master/parent stack to the child stacks? I attempted to do this as a Parameter, but "Ref" is not allowed for parameter values (so I couldn't do "Ref" : "AWS::StackName" for the value).

Any help is appreciated!

like image 449
user1617942 Avatar asked Dec 24 '22 11:12

user1617942


1 Answers

It is in fact possible to pass the parent stack name to the child stacks using parameters:

Here's an exemple:

parent.yml

---
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  ChildA:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: ...test/test-child-a.yml
      Parameters:
        ParentStackName: !Ref AWS::StackName

test-child-a.yml

---
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  ParentStackName:
    Type: String
Resources:
  TestBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref ParentStackName
like image 63
Laurent Jalbert Simard Avatar answered Mar 31 '23 13:03

Laurent Jalbert Simard