【发布时间】:2020-05-23 08:36:15
【问题描述】:
我正在开发一个具有 lambda 函数的 SAM 应用程序,其中 API 网关作为事件源。 API Endpoint 是一种 POST 方法,需要在请求正文中设置一组参数。 API Gateway 为我们提供了通过使用 AWS 控制台指定请求模型来验证请求正文的能力。
请参阅以下 AWS 控制台选项的屏幕截图:
我需要通过 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 模板将“请求验证器”设置为“验证正文”选项。感谢您的帮助
【问题讨论】:
标签: amazon-web-services aws-lambda aws-api-gateway serverless aws-serverless