# How to prevent large responses to trigger 502 errors

When using Laravel as your preferred framework, you might want to scale your application by transition it to a serverless platform like AWS Lambda.

The easier way of doing this is either by using [Bref PHP](https://bref.sh/) or [Laravel Vapor](https://vapor.laravel.com/) (which is more a SaaS).

Either way, if you are using Laravel in your application you should know a couple of things.

## Cloudflare is not a solution

Cloudflare is a very good service that brings a lot of good performance and security things out of the box and for free.

The issue with Cloudflare is that **it doesn't solve the problem** when an Amazon Lambda response payload coming from a function call is **bigger than 10 megabytes**, API Gateway which is the service in between your Lambda function and the user will reject that response returning a 502 error to the user.

[This cannot be increased from the AWS console](https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#http-api-quotas), therefore **you should consider other solutions than Cloudflare's**.

## Decreasing response size doesn't always work

As [Laravel Vapor describes on its documentation pages](https://docs.vapor.build/1.0/projects/troubleshooting.html#_502-bad-gateway-or-internal-server-error) this error can be multiple things, be aware of that.

Doing as they say by searching by *"Task timed out"* in your CloudWatch's function log stream will help you identify if this is a problem with response size or something else.

## TL;DR

Solving this problem is easier than you think, but sometimes can become a pain, specially if your responses normally are way bigger than 10MB, **then you must consider other solution**.

For this purpose is that we've [developed a Laravel package](https://github.com/open-southeners/laravel-vapor-response-compression) that acts as a middleware, to compress the response before leaving the Laravel request lifecycle.

Hope this helps your projects.
