Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cloudformation Fn::ImportValue inside Fn::GetAtt

Is it possible to use Fn::ImportValue inside Fn::GetAtt. Currently, I'm trying to do the following

    "ParentId": {
       "Fn::GetAtt": [
          {
            "Fn::ImportValue": {
               "Fn::Sub": "${ParentStack}:RestApi"
             }
          },
          "RootResourceId"
       ]
    }

But I'm facing an error. "Template error: every Fn::GetAtt object requires two non-empty parameters, the resource name and the resource attribute".

like image 374
Mikhail Matvienko Avatar asked Nov 26 '17 14:11

Mikhail Matvienko


People also ask

What is FN :: GetAtt in CloudFormation?

The Fn::GetAtt intrinsic function returns the value of an attribute from a resource in the template. For more information about GetAtt return values for a particular resource, refer to the documentation for that resource in the Resource and property reference.

What is ImportValue in CloudFormation?

The intrinsic function Fn::ImportValue returns the value of an output exported by another stack. You typically use this function to create cross-stack references. In the following example template snippets, Stack A exports VPC security group values and Stack B imports them.

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.

What is the difference between GetAtt and ref?

GetAtt is essentially the same as the 2nd function of Ref above, it also returns an attribute of the resource that you created within your resource, but while ref returns only a default attribute, GetAtt allows you to choose from different attributes to return.


1 Answers

As it currently stands, it's not possible to have "Fn::ImportValue" inside of "Fn::GetAtt" block. The best explanation I have for this is that resource for which you want to get attribute value is not in a scope of your current template.

What you can try to do is to export all attribute values you are interested in from 'parent' template.

So your parent template would look like this:

"Outputs" : {
  "RestApi": {
    "Value" : { "Ref": "RestApi" },
    "Export" : { "Name": "RestApi" }
  },
  "RestApiRootResourceId": {
    "Value" : { "Fn::GetAtt": ["RestApi", "RootResourceId"] },
    "Export" : { "Name" : "RestApiRootResourceId" }
  }
}

And now in your child template you can reference your API root resource id from parent template:

"Resources" : {
  "XApiResource": {
    "Type": "AWS::ApiGateway::Resource",
    "Properties": {
      "RestApiId": {"Fn::ImportValue" : "RestApi"},
      "ParentId": {"Fn::ImportValue" : "RestApiRootResourceId"},
      "PathPart": "apiPath"
    }
  }
}
like image 137
Miso Solaja Avatar answered Sep 21 '22 21:09

Miso Solaja