【问题标题】:Referencing Resources between CloudFormation stacks在 CloudFormation 堆栈之间引用资源
【发布时间】:2018-10-11 13:53:33
【问题描述】:

如果我有两个 cloudformation 堆栈,我如何从另一个堆栈引用一个堆栈中的资源?

在下面的示例中,我有一个堆栈,它创建了一个 EBS 卷,并希望通过我的 EC2 实例的第二个堆栈中的 Ref: 键来引用它,但我不断得到回滚,因为它无法从第一个堆栈:

“模板格式错误:未解决的资源依赖关系”

我已经尝试过 DependsOn 子句,但它不起作用。我需要通过参数传递信息吗?

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "CubesNetworking": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "TemplateURL":     "https://s3.amazonaws.com/mybucket/cf_network.json"
      }
   },
   "CubesInstances": {
     "DependsOn": ["CubesNetworking"],
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "TemplateURL": "https://s3.amazonaws.com/mybucket/cf_instances.json"
      }
    }
  }
}

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    在每个嵌套堆栈中,您应该有一个输出部分。然后,您可以使用如下语法在调用堆栈(上面列出的那个)中获取这些值:

          { "Fn::GetAtt" : [ "CubesNetworking", "Outputs.VolumeID" ] }
    

    然后您通过参数将值传递到您的其他嵌套堆栈中:

      "Parameters" : {
          "VolumeId" : { "Fn::GetAtt" : [ "CubesNetworking", "Outputs.VolumeID" ] }
    

    您仍然需要 DependsOn,因为您需要在实例之前创建的卷。

    编辑,2017 年年中:

    CloudFormation 引入了从一个堆栈中导出值的功能,并在不必嵌套的其他堆栈中引用它们。

    所以你的输出可以指定一个导出:

    Outputs:
      Desc:
        Value: !Ref CubesNetworking.VolumeID
        Export:
          Name: some-unique-name
    

    然后在另一个堆栈中:

    Fn::ImportValue: some-unique-name
    

    【讨论】:

    • 澄清一下,我可以在什么级别使用 Fn::GetAtt。它在模板 cf_instances.json 中吗?那就是我可以做这样的事情吗? "instancei4867d39f": { "Type": "AWS::EC2::Instance", "Properties": { ... "Volumes": [{ "Device": "/dev/xvdh", "VolumeId": { " Ref": { "Fn::GetAtt" : [ "CubesNetworking", "Outputs.VolumeID" ] } } },或者我需要在主模板中将 Output.VolumeID 作为上一级参数传入吗?跨度>
    • 太好了。进行大型设置听起来相当痛苦。
    • 是的。我们正在从大型嵌套堆栈转移到应用程序每个部分的单独脚本。
    猜你喜欢
    • 2019-05-09
    • 2021-04-05
    • 2016-03-10
    • 2021-10-31
    • 2020-11-09
    • 2021-03-09
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多