How to get stack output from AWS SAM?

大憨熊 提交于 2019-12-23 16:43:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!