Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing Resources between CloudFormation stacks

If I have two cloudformation stacks, how do I references a resource in one stack from the other stack?

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:

"Template format error: Unresolved resource dependencies"

I already tried the DependsOn clause but it didn't work. 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"
      }
    }
  }
}
like image 843
klall Avatar asked Apr 15 '15 23:04

klall


People also ask

How do you reference existing resources in CloudFormation?

To import existing resources into a CloudFormation stack, you need to provide: A template that describes the entire stack, including both the resources to import and (for existing stacks) the resources that are already part of the stack. Each resource to import must have a DeletionPolicy attribute in the template.

How do you reference parameters in CloudFormation?

You use the Ref intrinsic function to reference a parameter, and AWS CloudFormation uses the parameter's value to provision the stack. You can reference parameters from the Resources and Outputs sections of the same template.

How do I fix drifted CloudFormation stack?

Sign in to the AWS Management Console and open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation . On the Stacks page, choose the stack that has drifted. Choose Update, and then choose Replace current template from the stack details pane.

How do I call nested stack in CloudFormation?

Sign in to the AWS Management Console and open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation/ . Choose the name of the root stack whose nested stacks you want to view. Expand the Resources section. Look for resources of type AWS::CloudFormation::Stack.


1 Answers

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.

Edit, Mid-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.

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
like image 134
chris Avatar answered Sep 28 '22 06:09

chris