Set "Request Validator" of API Gateway by serverless

I want to set Request Validator of API Gateway by serverless. I tried two different settings for the Request Validator. But, both methods have failed. I have summarized what I did, so please let me know if there is something wrong.

  1. I write the API specification in swagger(OAS3.0). Therefore I tried to realize the setting of Request Validator using OAS extension. After I did sls deploy using below swagger.yaml and serverless.yml, None of the validate patterns described in x-amazon-apigateway-request-validators were added to the Request Validator options.
    x-amazon-apigateway-request-validator property - Amazon API Gateway

enter image description here

swagger.yaml is below:

openapi: 3.0.0

info:
  description: xxx
  version: '0.1'
  title: xxx API
x-amazon-apigateway-request-validators:
  body-only:
    validateRequestBody: true,
    validateRequestParameters: false
  except-body:
    validateRequestBody: false,
    validateRequestParameters: true
  all:
    validateRequestBody: true,
    validateRequestParameters: true
tags:
  - name: auth
    description: xxx
paths:
  /login:
    post:
      tags:
        - auth
      summary: xxx
      description: ''
      x-amazon-apigateway-request-validator: all
      responses:
        '200':
          description: success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthResponse'
        '400':
          description: fail
          content:
            application/json
        '401':
          description: fail
          content:
            application/json
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AuthRequest'
        required: true
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
        uri: "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxx:function:xxx-api-dev-login/invocations"
        passthroughBehavior: "when_no_match"
        httpMethod: "POST"
        contentHandling: "CONVERT_TO_TEXT"
        type: "aws_proxy"

My serverless.yml is below:

resources:
  Resources:
    RestApi :
      Type : AWS::ApiGateway::RestApi
      Properties :
        Body : ${file(./swagger.yaml)}
    LoginApiToInvokeLambda:
      Type: AWS::Lambda::Permission
      DependsOn: LoginLambdaFunction
      Properties:
        FunctionName: xxx-ext-api-dev-login
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com
  1. I tried to realize the setting of Request Validator using AWS::ApiGateway::RequestValidator. After I did sls deploy using above swagger.yaml and below serverless.yml, the all described in RequestValidatorAll in severless.yml were added to the Request Validator options. But the default value of Request Validator was still NONE.
    enter image description here
resources:
  Resources:
    RestApi :
      Type : AWS::ApiGateway::RestApi
      Properties :
        Body : ${file(./swagger.yaml)}
    LoginApiToInvokeLambda:
      Type: AWS::Lambda::Permission
      DependsOn: LoginLambdaFunction
      Properties:
        FunctionName: xxx-ext-api-dev-login
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com
    RequestValidatorAll:
      Type: AWS::ApiGateway::RequestValidator
      Properties:
        Name: all
        RestApiId:
          Ref: RestApi
        ValidateRequestBody: true
        ValidateRequestParameters: true
2 Likes

Below code works for me

  RequestValidator:
    Type: AWS::ApiGateway::RequestValidator
    Properties:
      RestApiId: !Sub "${ServerlessRestApi}"
      ValidateRequestParameters: true
functions:

  hello:

    handler: handler.hello

    events:

      - http:

          path: /

          method: get

  likes:

    handler: handler.likes

    events:

      - http:

          path: /likes

          method: get

          integration: lambda

          reqValidatorName: xMyRequestValidator

          request:

            passThrough: NEVER

            parameters:

              querystrings:

                url: true

            template:

              application/json: '{ "userid":"$input.params(''userid'')","activityid":"$input.params(''activityid'')"}'

          response:

            headers:

              Content-Type: "'application/json'"

custom:

  wsgi:

    app: handler.app

    pythonBin: python # Some systems with Python3 may require this

    packRequirements: false

  pythonRequirements:

    dockerizePip: non-linux

resources:

  Resources:

    xMyRequestValidator:  

      Type: "AWS::ApiGateway::RequestValidator"

      Properties:

        Name: 'my-req-validator'

        RestApiId: 

          Ref: ApiGatewayRestApi

        ValidateRequestBody: true

        ValidateRequestParameters: true