简体   繁体   English

在嵌套堆栈中访问父Cloudformation堆栈的名称

[英]Accessing name of parent Cloudformation stack in nested stack

I'm using a nested Cloudformation template structured like this: 我正在使用如下结构的嵌套Cloudformation模板:

masterTemplate -> childA -> childB masterTemplate - > childA - > childB

Each of the JSON template files is stored in S3 with a bucket name of "${masterStackName}-env-upload". 每个JSON模板文件都存储在S3中,其存储桶名称为“$ {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. 但是,当childA尝试执行相同的操作来启动childB模板时,“AWS :: StackName”将成为childA的生成名称 - 这意味着它正在尝试访问不存在的存储桶。

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). 我尝试将此作为参数执行,但参数值不允许使用“Ref”(因此我无法对“Ref”:“AWS :: StackName”执行该值)。

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 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 测试儿童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. 一种选择是将堆栈名称与S3存储桶名称解耦,并将S3存储桶指定为masterTemplate堆栈中的参数,然后在“ Outputs部分中引用它。 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. 在这种情况下, EnvUploadBucketName将是上传存储桶的名称,作为masterTemplate堆栈的输出传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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