问题
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.
回答1:
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 api_backend example in the SAM-repository. The relevant section is the definition of the outputs:
Outputs:
ApiURL:
Description: "API endpoint URL for Prod environment"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/resource/"
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==`ApiURL`].OutputValue' \
--output text
来源:https://stackoverflow.com/questions/51686580/how-to-get-stack-output-from-aws-sam