'How to make API Gateway to return any status code if no query parameters are detected?

I need to make API Gateway return any of the following status codes if any query parameter is detected:

  • 400 Bad Request
  • 403 Forbidden
  • 404 Not found
  • 416 Range Not Satisfiable
  • 500 Internal server error
  • 502 Bad gateway
  • 503 Service unavailable
  • 504 Gateway timeout

I'm working on an image service that has a CloudFront Distribution and some Lambdas behind it that process incoming images based on query parameters. Images are fetched from an S3 Bucket and then returned by Lambda with edits if any valid query parameter is passed through.

If no query parameter is passed I would like to just return the original image but without uselessly triggering the Lambda function, since there are no edits to be applied.

Since I have my S3 Bucket public and has Static Site Hosting enabled I'm looking for a way to bypass Lambda triggering and return the original image from S3 directly in case no query parameters are coming with the request.

According to AWS Docs here and here I can accomplish this with Origin groups on the CloudFront side by setting up a failover origin to S3 in case API Gateway response status is included within the list I mentioned above.

Forgot to mention even though might not be relevant that I'm using Lambda Proxy integration with API Gateway

But I can't find any way to make it respond with any of those when query parameters are passed. Do you have any idea how to manage such a situation?



Solution 1:[1]

You can mark query parameter(s) of your API method as required and assign the request validator to your method, https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-request-validation-set-up.html#api-gateway-request-validation-setup-in-console API returns 400 in case any required parameter is not provided.

AFAIK it is not possible to fail request if no query parameter is passed on Gateway level.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Milan Gatyas