我正在开发一个SAM应用程序,其中包含一个具有API Gateway作为事件源的Lambda函数。API端点是一个POST方法,需要在请求正文中提供一组参数。API Gateway通过在AWS Console中指定请求模型来提供验证请求正文的功能。
请参考下面的AWS Console选项截图:
我需要通过SAM模板设置类似的选项,并且能够将模型与请求正文链接起来,但无法设置请求验证器选项,并且也找不到任何文档或示例。
以下是我的SAM模板
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SAM Template
Parameters:
Stage:
Type: String
Default: dev
Resources:
MyApiGateway:
Type: AWS::Serverless::Api
Properties:
Name: My AWS Serverless API
StageName: !Ref Stage
Models:
ExchangeRate:
$schema: "http://json-schema.org/draft-04/schema#"
properties:
base:
type: string
target:
type: string
required:
- base
- target
title: User
type: object
ExchangeRateFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/exchange-rate/
Handler: index.handler
Runtime: nodejs12.x
Description: Function to Get Currency Exchange Rate
MemorySize: 128
Timeout: 3
Policies:
- AWSLambdaBasicExecutionRole
Events:
HelloWorld:
Type: Api
Properties:
RestApiId: !Ref MyApiGateway
Path: /exchange
Method: POST
RequestModel:
Model: ExchangeRate
Required: true
Outputs:
ExchangeRateFunction:
Description: "Exchange Rate Lambda Function ARN"
Value: !GetAtt ExchangeRateFunction.Arn
MyApiGateway:
Description: "My Seed API EndPoint"
Value: !Sub "https://${MyApiGateway}.execute-api.${AWS::Region}.amazonaws.com/${Stage}"
相关文档请参考:
- https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-api.html
- https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html
请问如何在 SAM 模板中设置“请求验证器”为“验证正文”选项。感谢您的帮助。