Search code examples
aws-lambdaaws-cloudformationaws-cliaws-samaws-sam-cli

AWS Lambda / SAM: How to get invocation URI from CLI?


As part of a AWS SAM template, I have a function with an HttpPost event trigger. Because I'm using the AWS SAM transform, I am not explicitly declaring the API Gateway that gets created to route this http post to trigger the function. Given that, is there any way to reference the generated URL endpoint, such as in a stack output or describe-stack-resources, so that I can programatically get the invocation URL for the function? I know I can get the endpoint by navigating to the stack in the console, finding the ApiGateway resource, and clicking around randomly until one of the pages shows it. But I'd like a method that my application code can reproduce.

Shortened template for reference:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
  SendJobUpdateFunction:
    Type: AWS::Serverless::Function
    Properties:
      ...
      Runtime: nodejs10.x
      Events:
        HttpPost:
          Type: Api
          Properties:
            Path: '/jobs'
            Method: post
     ...

I'm currently deploying using the sam CLI, which has I think a very similar syntax to aws cloudformtion.


Solution

  • According to the documentation and this previous question, you can get it with:

    !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}"
    

    Where ${Stage} is your own parameter containing the deployed stage.