简体   繁体   中英

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!

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

One option is to decouple the stack name from the S3 bucket name, and specify the S3 bucket as a parameter in the masterTemplate stack, and then reference it in the Outputs section. The

In the master:

"Outputs": {
                "EnvUploadBucketName" : {
                  "Value" : { "Ref" : "paramEnvUploadBucketName" }
                }

}

In the child:

"TemplateURL":  {
          "Fn::Join": [
            "",
            [
              "https://s3.amazonaws.com/",
              { "Fn::GetAtt" : [ "masterTemplate", "Outputs.EnvUploadBucketName" ] }
              "/device-specific-template-HALO-BUILD-VERSION.json"
            ]
          ]
        }

In this case, EnvUploadBucketName would be the name of the upload bucket, passed as an output from the masterTemplate stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM