问题
I'm trying to use AWS SAM to link a request validator resource to a serverless API in a SAM template. I have created the request validator and referenced the API in its RestApiId but the validator doesn't get set as the API default validator option in the AWS console.
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
The validator gets created and if I click on the Request Validator drop down menu on the API I can see it. However, the Request Validator doesn't default to my defined validator. It just has None as an the selected option
回答1:
I searched through source code for api transformation functions and there is no way how to append request validation. SAM transform AWS::Serverless::Api to inline body swagger/openapi definition but has nothing for x-amazon-apigateway-request-validator. I don't understand why there is way to define model in lambda event when it only append schema section in api method
"paths": {
"/post": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/user"
}
}
},
"required": true
},
maybe add feature request in SAM github
来源:https://stackoverflow.com/questions/57860631/how-to-add-a-request-validator-in-a-aws-sam-template-for-awsserverlessapi