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

Gleb picture Gleb · Jun 22, 2018 · Viewed 24.8k times · Source

I create 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 desired result?

Answer

Stefano picture Stefano · Jun 22, 2018

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.