如何在 AWS::Serverless::Api 的 AWS SAM 模板中添加请求验证程序?
How to add a request validator in a AWS SAM template for AWS::Serverless::Api?
我正在尝试使用 AWS SAM link 向 SAM 模板中的无服务器 API 请求验证程序资源。我创建了请求验证器并在其 RestApiId 中引用了 API,但验证器未在 AWS 控制台中设置为 API 默认验证器选项。
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Discription for the template
Globals:
Function:
Timeout: 30
Resources:
MyAPI:
Type: AWS::Serverless::Api
Properties:
Name: MyAPI
StageName: prod
Auth:
DefaultAuthorizer: MyAuthorizer
Authorizers:
MyAuthorizer:
FunctionPayloadType: REQUEST
FunctionArn: here goes the function Arn
Identity:
Context:
- identity.sourceIp
ReauthorizeEvery: 60
Models:
RequestModel:
$schema: 'http://json-schema.org/draft-04/mySchema#'
type: object
properties:
Format:
type: string
Name:
type: string
minLength: 3
Id:
type: string
required:
- Format
- Id
RequestValidator:
Type: AWS::ApiGateway::RequestValidator
Properties:
Name: RequestValidator
RestApiId: !Ref MyAPI
ValidateRequestBody: true
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: NameOfTheFunction
CodeUri: ./src/folder/
Handler: Project::nameSpace.class::Handler
Runtime: dotnetcore2.1
Environment:
Variables:
variableA : value
variableB : value
variableC : value
variableD : value
Events:
ApiEndpoint:
Type: Api
Properties:
RestApiId: !Ref MyAPI
Path: /path
Method: post
RequestValidatorId: !Ref RequestValidator
Auth:
Authorizer: MyAuthorizer
RequestModel:
Model: RequestModel
Required: true
验证器已创建,如果我单击 API 上的“请求验证器”下拉菜单,我可以看到它。但是,请求验证器不会默认为我定义的验证器。它只有 None 作为选定的选项
我在源代码中搜索了 api 转换函数,但无法附加请求验证。 SAM 将 AWS::Serverless::Api 转换为内联主体 swagger/openapi 定义,但对 x-amazon-apigateway-request-validator 没有任何影响。我不明白为什么只有在 api 方法
中追加模式部分时才能在 lambda 事件中定义模型
"paths": {
"/post": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/user"
}
}
},
"required": true
},
可能会在 SAM 中添加功能请求github
我试过以下代码。验证器被创建。当我点击 API 上的 Request Validator 下拉菜单时,我可以看到它,但默认值仍然是 None。你知道为什么请求验证器不默认为我定义的验证器吗?
RequestValidator:
Type: AWS::ApiGateway::RequestValidator
Properties:
Name: 'request-validator'
RestApiId:
Ref: RestApi
ValidateRequestBody: true
ValidateRequestParameters: false
我正在尝试使用 AWS SAM link 向 SAM 模板中的无服务器 API 请求验证程序资源。我创建了请求验证器并在其 RestApiId 中引用了 API,但验证器未在 AWS 控制台中设置为 API 默认验证器选项。
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Discription for the template
Globals:
Function:
Timeout: 30
Resources:
MyAPI:
Type: AWS::Serverless::Api
Properties:
Name: MyAPI
StageName: prod
Auth:
DefaultAuthorizer: MyAuthorizer
Authorizers:
MyAuthorizer:
FunctionPayloadType: REQUEST
FunctionArn: here goes the function Arn
Identity:
Context:
- identity.sourceIp
ReauthorizeEvery: 60
Models:
RequestModel:
$schema: 'http://json-schema.org/draft-04/mySchema#'
type: object
properties:
Format:
type: string
Name:
type: string
minLength: 3
Id:
type: string
required:
- Format
- Id
RequestValidator:
Type: AWS::ApiGateway::RequestValidator
Properties:
Name: RequestValidator
RestApiId: !Ref MyAPI
ValidateRequestBody: true
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: NameOfTheFunction
CodeUri: ./src/folder/
Handler: Project::nameSpace.class::Handler
Runtime: dotnetcore2.1
Environment:
Variables:
variableA : value
variableB : value
variableC : value
variableD : value
Events:
ApiEndpoint:
Type: Api
Properties:
RestApiId: !Ref MyAPI
Path: /path
Method: post
RequestValidatorId: !Ref RequestValidator
Auth:
Authorizer: MyAuthorizer
RequestModel:
Model: RequestModel
Required: true
验证器已创建,如果我单击 API 上的“请求验证器”下拉菜单,我可以看到它。但是,请求验证器不会默认为我定义的验证器。它只有 None 作为选定的选项
我在源代码中搜索了 api 转换函数,但无法附加请求验证。 SAM 将 AWS::Serverless::Api 转换为内联主体 swagger/openapi 定义,但对 x-amazon-apigateway-request-validator 没有任何影响。我不明白为什么只有在 api 方法
中追加模式部分时才能在 lambda 事件中定义模型"paths": {
"/post": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/user"
}
}
},
"required": true
},
可能会在 SAM 中添加功能请求github
我试过以下代码。验证器被创建。当我点击 API 上的 Request Validator 下拉菜单时,我可以看到它,但默认值仍然是 None。你知道为什么请求验证器不默认为我定义的验证器吗?
RequestValidator:
Type: AWS::ApiGateway::RequestValidator
Properties:
Name: 'request-validator'
RestApiId:
Ref: RestApi
ValidateRequestBody: true
ValidateRequestParameters: false