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

Amazon Web-ServicesYamlAmazon Cloudformation

Amazon Web-Services Problem Overview


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?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

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.

Solution 2 - Amazon Web-Services

I know it's an old question, but still the first result in Google:

Parameters:
  path:
    Type: String
    Default: something/script.sh
Resources:
  Bucket:
    Type: AWS::S3::Bucket
Outputs:
  ScriptUrl:
    Description: Script Url
    Value:
      Fn::Sub:
        - ${url}${the_path}
        - {url: !GetAtt Bucket.WebsiteURL, the_path: !Ref path}

Solution 3 - Amazon Web-Services

We can use Fn:: for the nested intrinsic functions if using the ! short form first. So

!Sub "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/$(Fn::GetAtt:[TestLambda, Arn])/invocations"

Solution 4 - Amazon Web-Services

AWS CloudFormation provides several built-in functions that help you manage your stacks. Use intrinsic functions in your templates to assign values to properties that are not available until runtime.

[Source from AWS][1]

The Fn::GetAtt intrinsic function returns the value of an attribute from a resource in the template.

Declaration

  • JSON
    • { "Fn::GetAtt" : [ "logicalNameOfResource", "attributeName" ] }
  • YAML
  • Syntax for the full function name:
  • Fn::GetAtt: [ logicalNameOfResource, attributeName ]
  • Syntax for the short form:
  • !GetAtt logicalNameOfResource.attributeName

Note: Should not confuse with double colon: Fn::GetAtt is like Fn_GetAtt [1]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html?shortFooter=true

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHleb KastseikaView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesStefanoView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesCristian MarinView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesHleb KastseikaView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesPremrajView Answer on Stackoverflow