8

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...

Sai M.
  • 2,548
  • 4
  • 29
  • 46

1 Answers1

9

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:

Brian Winant
  • 2,915
  • 15
  • 17
  • 1
    can the mapping template be applied locally? – Chazt3n Jul 25 '19 at 21:43
  • 1
    I tried it locally and it didn't work for me. Which defeats the whole purpose of AWS SAM. If anyone has a working example, please please please post it. – Cameron Hudson Aug 06 '20 at 21:45
  • @CameronHudson. See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-definitionuri. Please note that if you need to use intrinsic fn.s, use the DefinitionBody property with the Include Transform to import an OpenApi definition into the template. – Afzal S.H. Feb 04 '21 at 04:43
  • @AfzalS.H. are u saying that mapping templates can work in sam? – mihaa123 Mar 30 '21 at 14:08