Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SAM application how to define body mapping templates

I am new to AWS and SAM. I am developing a dummy backend using AWS services. For that, I am using SAM application to write the code locally. I defined the structure of APIs and Lambda in that as

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
    sam-app

    Sample SAM Template for sam-app

Globals:
    Function:
        Timeout: 300
    Api:
        Cors:
            AllowHeaders: "'content-type, authorization'"
            AllowOrigin: "'*'"


Resources:

    HelloWorldFunction:
        Type: AWS::Serverless::Function
        Properties:
            CodeUri: hello_world
            Handler: app.lambda_handler
            Runtime: nodejs8.10
            Environment:
                Variables:
                    PARAM1: VALUE
            Events:
                HelloWorld:
                    Type: Api
                    Properties:
                        Path: /hello2
                        Method: get

Outputs:
    HelloWorldFunction:
      Description: "Hello World Lambda Function ARN"
      Value: !GetAtt HelloWorldFunction.Arn

This creates a small dummy application. But, I want to know how to use other utilities of AWS like Body Mapping, defining model etc. Please help me know these.

Thank you...

like image 214
Sai Avatar asked Jul 05 '18 05:07

Sai


People also ask

How do I set a mapping template in API gateway?

You must define the model in order to have API Gateway to generate a SDK or to enable basic request validation for your API. You don't have to define any model to create a mapping template. However, a model can help you create a template because API Gateway will generate a template blueprint based on a provided model.

How do I add a mapping template to AWS API gateway?

Navigate to the API Gateway console, choose the StoreFront API and open the GET method of the /orders resource. On the Method Execution details page, choose Integration Response. Expand the default response mapping (HTTP status 200), and expand the Mapping Templates section. Choose Add Mapping Template.

Which resources can be specified in AWS SAM template?

Note that a serverless application is more than just a Lambda function—it can include additional resources such as APIs, databases, and event source mappings. You can use AWS SAM to define your serverless applications.


1 Answers

You can define models, etc using an API Gateway Swagger definition. This can be embedded in the SAM template or hosted in S3 and referenced by the SAM template

Basic example looks like:

RestApi:
    Type: AWS::Serverless::Api
    Properties:
        DefinitionBody:       
            <add Swagger definition here>

See https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessapi for what the SAM API Gateway configuration options are.

Some sample SAM + API Gateway + Swagger examples are at:

  • https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/inline_swagger/template.yaml
  • https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_swagger_cors/template.yaml
  • https://docs.aws.amazon.com/apigateway/latest/developerguide/api-as-lambda-proxy-export-swagger-with-extensions.html
like image 176
Brian Winant Avatar answered Oct 21 '22 08:10

Brian Winant