简体   繁体   English

在CloudFormation堆栈之间引用资源

[英]Referencing Resources between CloudFormation stacks

If I have two cloudformation stacks, how do I references a resource in one stack from the other stack? 如果我有两个cloudformation堆栈,我如何从另一个堆栈中引用一个堆栈中的资源?

In the example below I have a stack that creates an EBS volume and want to reference that via the Ref: key in the second stack for my EC2 instance but I keep getting a rollback since it can't see that resource from the first stack: 在下面的示例中,我有一个堆栈,它创建一个EBS卷,并希望通过我的EC2实例的第二个堆栈中的Ref:键引用它,但我不断收到回滚,因为它无法从第一个堆栈中看到该资源:

"Template format error: Unresolved resource dependencies" “模板格式错误:未解决的资源依赖性”

I already tried the DependsOn clause but it didn't work. 我已经尝试过DependsOn子句,但它没有用。 Do I need to pass information via Parameters? 我需要通过参数传递信息吗?

{
  "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"
      }
    }
  }
}

In each of your nested stacks, you should have an output section. 在每个嵌套堆栈中,您应该有一个输出节。 Then you can get those values in your calling stack (the one you have listed above) with syntax like: 然后你可以使用以下语法在调用堆栈(上面列出的那个)中获取这些值:

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

You then pass the values into your other nested stacks via Parameters: 然后通过参数将值传递到其他嵌套堆栈:

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

You still want the DependsOn since you need the volume created before the instance. 您仍然需要DependsOn,因为您需要在实例之前创建的卷。

Edit, Mid-2017: 编辑,2017年中:

CloudFormation has introduced the ability to export values from one stack, and reference them in other stacks that do not have to be nested. CloudFormation引入了从一个堆栈导出值的功能,并在不必嵌套的其他堆栈中引用它们。

So your output can specify an export: 所以你的输出可以指定一个导出:

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

Then in another stack: 然后在另一个堆栈中:

Fn::ImportValue: some-unique-name

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

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