FG
☁️ Cloud & DevOpsAmazon

Add support for AWS API Gateway Basic Request Validation

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

This is a Feature Proposal Description AWS API GW officially supports this now - http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-request-validation.html For example, it could be set as a new property somewhere in `service.function.events.http.validation` config object. Blog post: https://aws.amazon.com/blogs/compute/how-to-remove-boilerplate-validation-logic-in-your-rest-apis-with-amazon-api-gateway-request-validation/

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Implement Basic Request Validation in AWS API Gateway

Medium Risk

AWS API Gateway has introduced support for method request validation, which allows for automatic validation of incoming requests against specified models. This feature is not currently implemented in the existing configuration, leading to potential issues with request handling and data integrity.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update API Gateway Configuration

    Modify the serverless configuration to include a validation property under the http event for the desired function. This will enable request validation based on the defined models.

    yaml
    events:
      - http:
          path: /your-endpoint
          method: post
          request:
            schema:
              application/json:
                type: object
                properties:
                  key:
                    type: string
                required:
                  - key
          validation:
            request:
              validateRequestBody: true
  2. 2

    Define Request Models

    Create JSON schema models that define the structure of the expected request body. This schema will be used by API Gateway to validate incoming requests.

    json
    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "key": {
          "type": "string"
        }
      },
      "required": ["key"]
    }
  3. 3

    Deploy Changes

    Deploy the updated configuration to AWS using the serverless framework or AWS CLI to apply the new request validation settings.

    bash
    serverless deploy
  4. 4

    Test Request Validation

    Send test requests to the API endpoint with both valid and invalid payloads to ensure that the request validation is functioning as expected. Monitor the responses for validation errors.

    bash
    curl -X POST https://your-api-endpoint/your-endpoint -H 'Content-Type: application/json' -d '{"key": "value"}'
    curl -X POST https://your-api-endpoint/your-endpoint -H 'Content-Type: application/json' -d '{"invalidKey": "value"}'

Validation

Confirm that valid requests are processed successfully while invalid requests return appropriate validation error messages. Check the API Gateway logs for any validation errors.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

serverlesslambdaawshelp-wantedcat/aws-event-api-gateway