AWS Cloudformation Fn::ImportValue inside Fn::GetAtt

Viewed 7554

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".

2 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"
    }
  }
}
Related