10

I would like to perform automatic integration tests on my serverless projects. To do that, I need to get the api endpoints somehow. There is already the plugin serverless-stack-output for Serverless framework that serves the purpose. But I'm wondering how can I achieve similar thing by AWS SAM after I deploy my application?

Meanwhile, If I can somehow get my api's base url as well as individual endpoints, then I'm able to connect them and and perform tests against them.

Mahdi
  • 1,089
  • 1
  • 14
  • 27

1 Answers1

14

As AWS SAM builds upon AWS CloudFormation you can use CloudFormation's Outputs-feature.

How to define such outputs is pretty straight forward. You can e.g. refer to the "hello world" template in the SAM-template-repository. The relevant section is the definition of the outputs:

Outputs:
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"

You then still need a way to get the outputs after you deployed the CloudFormation stack. For that you can e.g. use the AWS CLI:

aws cloudformation describe-stacks --stack-name mystack \
    --query 'Stacks[0].Outputs[?OutputKey==`HelloWorldApi`].OutputValue' \
    --output text
Dunedan
  • 7,848
  • 6
  • 42
  • 52
  • 2
    Thanks. Yes, this works but then I should define separate "output" for each function. This can easily lead to a mess when there are many functions in the app. Is there any way to make it shorter syntax? Also by following this example, each api endpoint (e.g. in this case its "/resource") should be written and updated manually. Which can is annoying. Is there any way or any !REF that can get URL endpoint? – Mahdi Aug 04 '18 at 21:52