# AWS Lambda 502 Bad Gateway

> A Lambda 502 is usually the function

Canonical: https://scaplet.com/errors/cdn/aws-lambda-502

Guide AWS Error · CDN and hosting errors

A Lambda 502 is usually the function's return value breaking the proxy contract, a response over 1 MB, a timeout or an error. See the exact shapes and test them.

4 min

read

5

sources

Oct 31, 2026

updated

Scaplet Editorial Team

author

In one line

A 502 in front of a Lambda function means the proxy could not use what the function returned. API Gateway returns 502 when the output is not in its JSON format. An ALB lists three Lambda causes: a response body over 1 MB, a timeout, and a function error or throttling. Return the documented shape and keep it small.

30-second check

## Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?

Summarize this page with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fcdn%2Faws-lambda-502%20(%22AWS%20Lambda%20502%20Bad%20Gateway%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fcdn%2Faws-lambda-502%20(%22AWS%20Lambda%20502%20Bad%20Gateway%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fcdn%2Faws-lambda-502%20(%22AWS%20Lambda%20502%20Bad%20Gateway%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fcdn%2Faws-lambda-502%20(%22AWS%20Lambda%20502%20Bad%20Gateway%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

On this page

## 5 parts, 4 minutes.

1. [A 502 here is about the return value 01](#a-502-here-is-about-the-return-value)
2. [The shape the function must return 02](#the-shape-the-function-must-return)
3. [Check it in 2 minutes 03](#check-it-in-2-minutes)
4. [Fixes by symptom 04](#fixes-by-symptom)
5. [What a Lambda 502 means for SEO 05](#what-a-lambda-502-means-for-seo)

_01_What each caller documents as a Lambda 502

## A 502 here is about the return value

Lambda itself does not speak HTTP. A proxy in front of it translates the function's return value into a response, and a 502 means the translation failed. Which proxy you use changes the rules, so start there. The general AWS 502 causes for load balancers and CloudFront live on the [AWS 502 page](/errors/cdn/aws-502-bad-gateway); this page covers only what is specific to Lambda.

Caller

### API Gateway, Lambda proxy integration

502 trigger in the AWS docs

The function output is in a different format from the one API Gateway requires

Caller

### Application Load Balancer, Lambda target

502 trigger in the AWS docs

The response body exceeds 1 MB

Caller

### Application Load Balancer, Lambda target

502 trigger in the AWS docs

The function did not respond before its configured timeout was reached

Caller

### Application Load Balancer, Lambda target

502 trigger in the AWS docs

The function returned an error, or the Lambda service throttled it

Where the translation happens

```
client --> API Gateway or ALB --> Lambda function ^ | '---- return value ----' not a valid response object -> 502
```

_02_Fields in the AWS docs

## The shape the function must return

Both callers want a JSON object with a [status code](/glossary/http-status-codes), headers and a body, but they differ on what may be left out. API Gateway documents all five keys and says `headers` and `multiValueHeaders` can be unspecified. An ALB says the response must include the Base64 encoding status, status code and headers, and that you can omit the body. The safest habit is to return every field every time.

Field

### `isBase64Encoded`

API Gateway proxy

In the required output format; set `true` for binary

ALB Lambda target

Required; the load balancer decodes Base64 content when `true`

Field

### `statusCode`

API Gateway proxy

In the required output format

ALB Lambda target

Required

Field

### `statusDescription`

API Gateway proxy

Not part of the format

ALB Lambda target

Shown in AWS's example, for example `200 OK`

Field

### `headers`

API Gateway proxy

Can be unspecified; single-value headers only

ALB Lambda target

Required

Field

### `multiValueHeaders`

API Gateway proxy

Can be unspecified; carries multi-value and single-value headers

ALB Lambda target

Use it if multi-value headers are enabled on the target group

Field

### `body`

API Gateway proxy

Marshalled to the client as the payload

ALB Lambda target

Can be omitted

A handler that returns the documented shape

```
export const handler = async (event) => { const body = JSON.stringify({ ok: true, path: event.path ?? '/' }); return { isBase64Encoded: false, statusCode: 200, statusDescription: '200 OK', headers: { 'Content-Type': 'application/json' }, body, }; };
```

The ALB also ignores hop-by-hop headers such as `Connection` and `Transfer-Encoding`, and you can omit `Content-Length` because the load balancer computes it. For binary bodies, Base64-encode the content and set `isBase64Encoded` to `true`. For API Gateway that also needs `*/*` configured as a Binary Media Type.

_03_Fix it on your stack

## Check it in 2 minutes

Call the function directly, bypassing the proxy, and read what it returns. If the direct result is not a response object, the proxy has nothing valid to translate. The first command is a public test endpoint for the status shape; the second runs a handler on your machine; the third needs your AWS account.

Public test endpoint: read a 502 status
Public test endpoint: read a 502 status

```
curl -sS -o /dev/null -w "%{http_code}\n" https://httpbin.org/status/502 # observed: 502
```

Local run of a good and a broken handler under Node (run on 2026-11-01)
Local run of a good and a broken handler under Node (run on 2026-11-01)

```
{"isBase64Encoded":false,"statusCode":200,"statusDescription":"200 OK","headers":{"Content-Type":"application/json"},"body":"{\"ok\":true,\"path\":\"/health\"}"} "hello"
```

Your account: invoke the function directly (AWS CLI v2; needs your function)
Your account: invoke the function directly (AWS CLI v2; needs your function)

```
aws lambda invoke response.json \ --function-name my-function \ --cli-binary-format raw-in-base64-out \ --payload '{ "path": "/health" }' cat response.json # A response object, like the first line above, is correct. # A bare string like "hello" has no statusCode and is not the documented shape.
```

The first line is the object a proxy can use. The second is what a handler returns when it forgets to wrap the value: a bare string. Based on the AWS docs, that is the case API Gateway rejects with 502. The `aws lambda invoke` form is taken from the AWS CLI reference, and AWS notes `--cli-binary-format` is required on CLI version 2. If the invoke result contains a `FunctionError`, the function itself failed, which is the other documented route to a 502 on an ALB.

_04_Symptom, cause and fix

## Fixes by symptom

Symptom

### Direct invoke returns a string, array or object with no `statusCode`

Cause

The handler returns data, not a response

Fix

Wrap the value as in the handler above

Symptom

### Works for small requests, 502 for large ones through an ALB

Cause

Maximum response JSON from the function is 1 MB

Fix

Return a link to the data, for example an object in S3, instead of the data itself

Symptom

### 502 after a fixed delay

Cause

The function ran to its configured timeout

Fix

Shorten the work or raise the timeout. See [Lambda timeout](/errors/cdn/lambda-timeout)

Symptom

### 502 with `FunctionError` in the invoke result, or [throttling](/glossary/rate-limiting)

Cause

The function returned an error or was throttled

Fix

Fix the exception from the logs, or raise concurrency

Symptom

### Binary file arrives corrupted

Cause

`isBase64Encoded` is false, or `*/*` is not a Binary Media Type on API Gateway

Fix

Encode the body, set the flag, and configure the media type

**Client errors are not exceptions**AWS's API Gateway page shows returning `{"statusCode": 400, "body": "Missing parameters of ..."}` for a client-side problem such as a missing parameter, instead of throwing.

A response object is right when
- The function sits behind API Gateway Lambda proxy or an ALB
- You want to control status and headers
- The body is under 1 MB

Use another route when
- The payload is large or binary and changes rarely
- You need streaming or long-running work
- A caller other than a proxy invokes the function and wants plain data

_05_Explained

## What a Lambda 502 means for SEO

When a function renders pages, a bad return value makes each of those URLs answer 502 at once. Search engines read that as a server fault: Google says its crawlers slow down on 5xx, and URLs it already indexed are kept only until the errors have lasted long enough. A format bug shows up the moment you deploy it, so put the direct invoke from the section above into your release checks. See [5xx server errors](/seo-issues/server-errors-5xx) and [API Gateway 504](/errors/cdn/api-gateway-504).

Questions

## Asked a lot.

3 questions people ask next, answered from the sources on this page.

_01_ Why does API Gateway return 502 for my Lambda function?

AWS documents one cause for the proxy integration: the function output is in a different format from the one API Gateway requires. Return an object with `statusCode`, and optionally `headers`, `multiValueHeaders`, `body` and `isBase64Encoded`.

_02_ What is the Lambda response size limit behind an ALB?

1 MB. The ALB docs give the maximum size of the response JSON the function can send as 1 MB, and list a response body over 1 MB as a 502 cause.

_03_ Can the response body be empty?

Behind an ALB, yes: AWS says the body can be omitted, while the Base64 encoding status, status code and headers must be present. Behind API Gateway, return the documented format and include `body` when you have content.

Sources · 5

## Every fact, linked and dated.

Official docs first. The date is when we last opened each page.

- amazon.com
- google.com

1. 01 [AWS: Lambda functions as targets for Application Load Balancers](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html) docs.aws.amazon.com · checked 2026-11-01
2. 02 [AWS: Set up Lambda proxy integrations in API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html) docs.aws.amazon.com · checked 2026-11-01
3. 03 [AWS: Troubleshoot your Application Load Balancers](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-troubleshooting.html) docs.aws.amazon.com · checked 2026-11-01
4. 04 [AWS CLI reference: lambda invoke](https://docs.aws.amazon.com/cli/latest/reference/lambda/invoke.html) docs.aws.amazon.com · checked 2026-11-01
5. 05 [How HTTP status codes affect Google's crawlers](https://developers.google.com/crawling/docs/troubleshooting/http-status-codes) developers.google.com · checked 2026-11-01

## Keep learning

[All errors](/errors)

[Learn ALB 504 Gateway Timeout CDN and hosting errors](/errors/cdn/alb-504-gateway-timeout) [Related 5xx server errors Status codes and links](/seo-issues/server-errors-5xx) [Read next AWS 502 Bad Gateway CDN and hosting errors](/errors/cdn/aws-502-bad-gateway) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 502 errors
[502 Bad Gateway on Azure](/errors/http/502/azure) [502 Bad Gateway on Cloudflare](/errors/http/502/cloudflare) [AWS 502 Bad Gateway](/errors/cdn/aws-502-bad-gateway) [Azure Front Door 502](/errors/cdn/azure-front-door-502) [Google Cloud 502 Bad Gateway](/errors/cdn/google-cloud-502)

More AWS errors
[AWS EC2: Connection timed out](/errors/cdn/aws-ec2-connection-timed-out)

More in CDN and hosting errors
[Vercel build failed](/errors/cdn/vercel-build-failed) [Vercel domain not working](/errors/cdn/vercel-domain-not-working) [Vercel FUNCTION_INVOCATION_TIMEOUT](/errors/cdn/vercel-function-invocation-timeout) [Vercel: too many redirects](/errors/cdn/vercel-too-many-redirects) [Akamai reference number](/errors/cdn/akamai-reference-number) [ALB 503 Service Unavailable](/errors/cdn/alb-503) [ALB 504 Gateway Timeout](/errors/cdn/alb-504-gateway-timeout) [API Gateway 403 Forbidden](/errors/cdn/api-gateway-403-forbidden) [API Gateway 504 Gateway Timeout](/errors/cdn/api-gateway-504) [AWS 502 Bad Gateway](/errors/cdn/aws-502-bad-gateway) [AWS Amplify 404 Not Found](/errors/cdn/amplify-404) [AWS EC2: Connection timed out](/errors/cdn/aws-ec2-connection-timed-out) [AWS Lambda 502 Bad Gateway](/errors/cdn/aws-lambda-502) [AWS Lambda timeout](/errors/cdn/lambda-timeout) [Azure Front Door 502](/errors/cdn/azure-front-door-502) [Cloud Run 503 error](/errors/cdn/cloud-run-503) [Cloudflare 504 Gateway Timeout](/errors/cdn/cloudflare-504) [Cloudflare Pages 404 Not Found](/errors/cdn/cloudflare-pages-404) [Cloudflare too many redirects (ERR_TOO_MANY_REDIRECTS)](/errors/cdn/cloudflare-too-many-redirects) [Cloudflare verify you are human loop](/errors/cdn/cloudflare-verify-you-are-human-loop) [CloudFront 400: Bad Request](/errors/cdn/cloudfront-400) [CloudFront 404 Not Found](/errors/cdn/cloudfront-404) [CloudFront 503: Service Unavailable](/errors/cdn/cloudfront-503) [Firebase Hosting 404](/errors/cdn/firebase-hosting-404)

Terms on this page
[HTTP status codes](/glossary/http-status-codes) [Rate limiting](/glossary/rate-limiting)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

Run the free audit, then approve the fixes. Every change can be undone.

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
