Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Sub and GetAtt functions at the same time in CloudFormation template?

I created CloudFormation yaml template and I need to use !GetAtt "TestLambda.Arn" as part of !Sub function in "AWS::ApiGateway::Method" Integration Uri:

Type: "AWS::ApiGateway::Method"   Properties:     RestApiId:       Ref: "RestApi"     ResourceId:       Ref: "TestResource"     HttpMethod: "GET"     AuthorizationType: "NONE"     Integration:       Type: "AWS_PROXY"       IntegrationHttpMethod: "POST"       Uri: !Sub "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/[[place where I want to use !GetAtt "TestLambda.Arn"]]/invocations" 

As result I want to get a value something like that:

"arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/my-endpoint-lambda/invocations" 

How can I use these functions together and get the desired result?

like image 310
Gleb Avatar asked Jun 22 '18 10:06

Gleb


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.

Which two types of syntax can be used within an AWS CloudFormation template?

You can author AWS CloudFormation templates in JSON or YAML formats. We support all AWS CloudFormation features and functions for both formats, including in AWS CloudFormation Designer.

What does FN :: sub do?

Fn::Sub. The intrinsic function Fn::Sub substitutes variables in an input string with values that you specify. In your templates, you can use this function to construct commands or outputs that include values that aren't available until you create or update a stack.

What is difference between ref and GetAtt in CloudFormation?

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

You don't need to use !GetAtt here, !Sub will automatically unpack values for you if you place them within the place holder:

Uri: !Sub arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${TestLambda.Arn}/invocations 

This is explained in the docs:

If you specify template parameter names or resource logical IDs, such as ${InstanceTypeParameter}, AWS CloudFormation returns the same values as if you used the Ref intrinsic function. If you specify resource attributes, such as ${MyInstance.PublicIp}, AWS CloudFormation returns the same values as if you used the Fn::GetAtt intrinsic function.

like image 135
Stefano Avatar answered Sep 20 '22 16:09

Stefano