serverless-apigateway-service-proxy icon indicating copy to clipboard operation
serverless-apigateway-service-proxy copied to clipboard

Is there any way to set Request Validator for the Method Request for Kinesis Integration?

Open kunhuangau opened this issue 4 years ago • 5 comments

image Can't find the place to set Request Validator in Method Request as above screenshoot.

kunhuangau avatar Feb 11 '21 05:02 kunhuangau

Hi. Would it be possible if you could also advise on how i could do this with SQS as a service proxy ?

pb321 avatar Feb 23 '21 10:02 pb321

@pb321 @kunhuangau - I dug into and I got it working for my use case. I'm leveraging serverless-apigateway-service-proxy for a SQS proxy.

First I have a pretty straight forward definition for the sqs service proxy:

apiGatewayServiceProxies:
    - sqs:
        path: /intake
        method: post
        queueName: { 'Fn::GetAtt': ['SQSQueue', 'QueueName'] }
        cors: true
        response:
          template:
            # `success` is used when the integration response is 200
            success: |-
              { "message": "accepted" }
            # `clientError` is used when the integration response is 400
            clientError: |-
              { "message": "there is an error in your request" }
            # `serverError` is used when the integration response is 500
            serverError: |-
              { "message": "there was an error handling your request" }

Per the documentation I have the SQS resource defined:

SQSQueue:
      Type: 'AWS::SQS::Queue'

Below are the additional resources for a AWS::ApiGateway::Model, AWS::ApiGateway::RequestValidator.

ApiGatewayRestMethodModel:
    Type: AWS::ApiGateway::Model
    Properties:
      ContentType: application/json
      Description: "Intake Request Model"
      RestApiId: { Ref: 'ApiGatewayRestApi' }
      Schema: ${file(intake.schema.json)}
ApiGatewayRequestValidator:
    Type: AWS::ApiGateway::RequestValidator
    Properties:
      Name: IntakeValidator
      RestApiId: { Ref: 'ApiGatewayRestApi' }
      ValidateRequestBody: true
      ValidateRequestParameters: false

I then create the following resource to set the Model and RequestValidator. This updates the APIGateway Resource/Method that I defined above (POST /intake)

ApiGatewayMethodIntakePost:
      Type: AWS::ApiGateway::Method
      Properties:
        RequestModels:
          application/json: { Ref: 'ApiGatewayRestMethodModel' }
        RequestValidatorId: { Ref: 'ApiGatewayRequestValidator' }

After testing a successful request, which is valid against my defined model json schema I receive a successful response:

{
  "message": "accepted"
}

After testing a malformed request body, I receive a 400 error with the message:

{
  "message": "Invalid request body"
}

Validating both requests, for the correct request body I see the message get enqueued correctly and processed by a lambda on the other end. The invalid requests, the message isn't enqueued and the lambda isn't invoked

Success! 🎉 I hope this helps

mikeluby avatar Mar 07 '21 21:03 mikeluby

Hi Mike. thank you so much for this solution, its very much appreciated !! i will try it out later and will get back to you on the outcome.

pb321 avatar Mar 08 '21 08:03 pb321

thanks for the answer @mikeluby . I am going to try this a well. I wish this was supported out of the box with something like

apiGatewayServiceProxies:
    - sqs:
        path: /intake
        method: post
        queueName: { 'Fn::GetAtt': ['SQSQueue', 'QueueName'] }
        cors: true
        request:
          schema:
            application/json: ${file(schemas/intake.schema.json)}
        response:
          template:
            # `success` is used when the integration response is 200
            success: |-
              { "message": "accepted" }
            # `clientError` is used when the integration response is 400
            clientError: |-
              { "message": "there is an error in your request" }
            # `serverError` is used when the integration response is 500
            serverError: |-
              { "message": "there was an error handling your request" }

pankajanand18 avatar Dec 17 '21 06:12 pankajanand18

@mikeluby can you explain where and how did you define ApiGatewayRestApi resources?

Also, when I try to deploy your code, it says, HttpMethod, ResourceId and RestApiId properties are mandatory for ApiGatewayMethodIntakePost. I got a reference to that as well.

Even after adding all properties and deploying on serverless, it throws error as below:

An error occurred: ApiGatewayMethodIntakePost - 1 validation error detected: Value null at 'putMethodInput.authorizationType' failed to satisfy constraint: Member must not be null (Service: AmazonApiGateway; Status Code: 400; Error Code: ValidationException; Request ID: 102ce957-81d5-4101-8f3e-b2a2007b3eab; Proxy: null)

Hope so you got a solution for validating the body for SQS proxy through serverless-apigateway-service-proxy plugin.

For information, I am trying to make a secure proxy to SQS FIFO Queue which also validates the body and sends a customized response.

devksx avatar Jan 11 '22 11:01 devksx