Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get stack output from AWS SAM?

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.

like image 745
Mahdi Avatar asked Aug 04 '18 14:08

Mahdi


People also ask

How do I find my aws stacks?

On the Stacks page of the CloudFormation console, select the stack name. CloudFormation displays the stack details for the selected stack. Select a stack details pane to view the related information about your stack.

How do I export aws stacks?

To export a stack's output value, use the Export field in the Output section of the stack's template. To import those values, use the Fn::ImportValue function in the template for the other stacks. For a walkthrough and sample templates, see Walkthrough: Refer to resource outputs in another AWS CloudFormation stack.

What is stack name in aws Sam?

The stack name is an identifier that helps you find a particular stack from a list of stacks. A stack name can contain only alphanumeric characters (case-sensitive) and hyphens. It must start with an alphabetic character and can't be longer than 128 characters.

How do I check stack logs?

You can view logs, such as /var/log/cloud-init. log or /var/log/cfn-init. log , to help you debug the instance launch. You can retrieve the logs by logging in to your instance, but you must disable rollback on failure or else AWS CloudFormation deletes the instance after your stack fails to create.


1 Answers

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
like image 194
Dunedan Avatar answered Sep 28 '22 07:09

Dunedan