如何为我的 api 添加有关 AWS SAM 模板的更多详细信息

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

我是 AWS 无服务器世界和 SAM 的新手。我刚刚做了一个实际上完全可用的小机器人,但是当我开始做一个 SAM 模板来定义它时,我怀疑我一直无法弄清楚。我有一个 api 网关,它有一个特定的映射模板。我需要 sam 模板包含这个,但它没有,检查模板:

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

所以在说完这些之后,我想展示一下我是如何管理 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
}

而且我需要弄清楚如何详细说明我的模板的映射,以便在它更新 CloudFormation 上的堆栈时创建它。

也许如果我的方法不好,请告诉我应该怎么做。

为此,您必须将 AWS::Serverless::Api 资源添加到您的 SAM 模板并使用其 Definition-属性 将您的 API 定义为 OpenAPI-模板,您可以在其中包含您的请求和响应映射。

AWS SAM 的 git-存储库包含一个 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