man*_*ngh 7 amazon-web-services aws-lambda aws-api-gateway serverless aws-serverless
我正在开发一个 SAM 应用程序,该应用程序具有 lambda 函数,并以 API 网关作为事件源。API 端点是一种 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}"
Run Code Online (Sandbox Code Playgroud)
参考文件
请告诉我如何使用 SAM 模板将“请求验证器”设置为“验证正文”选项。将不胜感激的帮助
小智 1
我遇到了同样的问题,显然 SAM 暂时缺乏此功能,正如您从上一个问题中看到的那样:
如何在 AWS::Serverless::Api 的 AWS SAM 模板中添加请求验证器?
此外,GitHub 上还公开了一些问题,最后一个是:
https://github.com/awslabs/serverless-application-model/issues/1403
我已经破解了一个解决方案,其中包含 SAM 规范中的两个附加属性来解决此问题,但我不希望它实际上成为 PR。如果您想使用我的分叉存储库从开发分支进行部署,我可以提供进一步的说明。
归档时间: |
|
查看次数: |
2036 次 |
最近记录: |