Search code examples
amazon-web-servicesaws-api-gatewayaws-serverlessserverless-application-model

How to add more details on a AWS SAM template for my api


I'm new to AWS serverless world and also to SAM. I just did a small bot that is actually totally functional but when I start doing a SAM Template to define it I have a doubt that I haven't been able to figure out. I have a api gateway, and it have an specific mapping template. I need that the sam template include this, and it doesn't, check the template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
  certainty:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: index.handler
      Runtime: nodejs8.10
      CodeUri: ./certainty-function
      Description: >-
        This lambda monitors the ssl certificates expirations
        and communite with slack.
      MemorySize: 128
      Timeout: 20
      Role: 'arn:aws:iam::116738426468:role/ssl_cert_alerter'
      Events:
        Schedule1:
          Type: Schedule
          Properties:
            Schedule: rate(1 day)
        Api1:
          Type: Api
          Properties:
            Path: /
            Method: POST
  certaintyassistant:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: index.handler
      Runtime: nodejs8.10
      CodeUri: ./certainty-assistant-function
      Description: >-
        This lambda invoke Certainty and answer to the slack
        user.
      MemorySize: 1152
      Timeout: 300
      Role: 'arn:aws:iam::116738426468:role/ssl_cert_alerter'
      Events:
        Api1:
          Type: Api
          Properties:
            Path: /show-all
            Method: POST
      Environment:
        Variables:
          SLACK_TOKEN: oGprdUe0br93yH62fuezDHQh

So after saying this, I want to show how I manage the mapping on the api:

## designed just for post format.
{
    #foreach( $token in $input.path('$').split('&') )
        #set( $keyVal = $token.split('=') )
        #set( $keyValSize = $keyVal.size() )
        #if( $keyValSize >= 1 )
            #set( $key = $util.urlDecode($keyVal[0]) )
            #if( $keyValSize >= 2 )
                #set( $val = $util.urlDecode($keyVal[1]) )
            #else
                #set( $val = '' )
            #end
            "$key": "$val"#if($foreach.hasNext),#end
        #end
    #end
}

And I need to figure how to detail that mapping for my template to create it when it update the stack on CloudFormation.

Maybe if I'm having a bad approach, please show me how it should be done.


Solution

  • To achieve that you'll have to add an AWS::Serverless::Api resource to your SAM template and use its Definition-property to define your API as OpenAPI-template, where you can include your request and response mappings.

    The git-repository of AWS SAM contains an example how to include inline swagger into your template and the documentation of API Gateway contains information about a set of OpenAPI extensions used to define details like requestTemplates.