Error Medic

Troubleshooting AWS Lambda Timeout, 502 Bad Gateway, and Access Denied Errors

Fix AWS Lambda timeout, 502 Bad Gateway, 403 Forbidden, and throttling errors. Learn how to diagnose API Gateway integrations, IAM permissions, and concurrency.

Last updated:
Last verified:
1,596 words
Key Takeaways
  • Timeouts are often caused by unoptimized code, missing NAT Gateways in VPCs, or unconfigured HTTP client timeouts.
  • 502 Bad Gateway errors usually indicate a malformed proxy response from Lambda to API Gateway or an unhandled function crash.
  • 403 Forbidden and Access Denied errors stem from missing lambda:InvokeFunction resource policies or execution role IAM deficiencies.
  • Throttling (429/Rate Exceeded) requires implementing asynchronous SQS queues, requesting concurrency quota increases, or managing reserved concurrency.
  • Quick Fix: Check CloudWatch logs for 'Task timed out', verify VPC NAT configuration for external requests, and validate proxy integration JSON response formats.
Troubleshooting Approaches Compared
MethodWhen to UseTimeRisk
Increase Lambda TimeoutFunction genuinely needs more time for heavy processing5 minsLow (may increase cost)
Add NAT GatewayVPC Lambda failing to reach external internet APIs15 minsMedium (increases AWS network costs)
Configure Provisioned ConcurrencyCold starts causing intermittent API Gateway timeouts10 minsLow (fixed cost addition)
Update Resource PolicyAPI Gateway returns 403 Forbidden / Access Denied5 minsLow

Understanding AWS Lambda Timeouts and API Gateway Errors

AWS Lambda timeouts and related errors (like 502 Bad Gateway, 403 Forbidden, and Throttling) are among the most common hurdles encountered when deploying serverless architectures. When a Lambda function fails to return a response within its configured execution window, or when the API Gateway integration fails due to permission or concurrency limits, your end-users experience degraded performance or complete service outages.

The Core Symptoms

  1. AWS Lambda Timeout (Task timed out after X.XX seconds): This occurs when your function's execution exceeds the configured timeout value (default is 3 seconds, maximum is 900 seconds or 15 minutes). If invoked via API Gateway, you might see a 504 Gateway Timeout if the function takes longer than the API Gateway maximum integration timeout of 29 seconds.
  2. AWS Lambda 502 Bad Gateway: Often caused by the Lambda function returning an incompatible payload to an API Gateway proxy integration, or the Lambda function crashing/timing out before returning a properly formatted HTTP response.
  3. AWS Lambda 403 Forbidden / Access Denied: Typically an AWS Identity and Access Management (IAM) issue. This means the service invoking the Lambda function (like API Gateway, EventBridge, or S3) lacks the necessary lambda:InvokeFunction permission in the resource-based policy, or the Lambda execution role lacks permissions to access downstream resources (resulting in a crash or handled error).
  4. AWS Lambda Rate Limit / Throttling (429 Too Many Requests): Occurs when your account's concurrent execution limit is reached, or a specific function's reserved concurrency is exhausted. AWS Lambda service returns a Rate Exceeded or TooManyRequestsException.

Step 1: Diagnosing the Root Cause

Before changing configurations, you must identify whether the issue is a compute timeout, a network boundary issue, or an IAM permission denial.

Investigating Timeouts and 502s: Check Amazon CloudWatch Logs for your function. Look for the exact phrase: Task timed out after 3.00 seconds. If you see this, the function logic is taking too long. This could be due to:

  • Slow external API calls without HTTP client timeouts.
  • Unoptimized database queries or missing connection pooling.
  • Cold starts (especially in VPCs, though improved recently).
  • Infinite loops in your code.

If the function finishes successfully in CloudWatch but the client sees a 502 Bad Gateway, check the API Gateway execution logs. Ensure your Lambda response precisely matches the required format for Proxy Integrations:

{
  "isBase64Encoded": false,
  "statusCode": 200,
  "headers": { "Content-Type": "application/json" },
  "body": "{\"message\": \"Success\"}"
}

Investigating 403 Forbidden and Access Denied: If an internal service is getting a permission denied error, inspect AWS CloudTrail. Look for AccessDenied events associated with the assumed role of your Lambda function. If API Gateway is returning a 403, verify the API Gateway resource policy, WAF rules, or the Lambda resource-based policy using aws lambda get-policy.

Investigating Throttling: In CloudWatch Metrics, look at the Throttles metric for your Lambda function. If this metric is above zero, your invocations are being rejected because there is no available concurrency. Check if you have a Reserved Concurrency limit set too low, or if a noisy neighbor function is consuming your entire AWS account's unreserved concurrency pool.

Step 2: Implementing the Fixes

Fix 1: Resolving Timeouts

If your function legitimately needs more time (e.g., processing a large file), increase the timeout via the AWS CLI or Infrastructure as Code (Terraform/CloudFormation). However, if your function is hanging on external network requests, ensure you are setting explicit timeouts on your HTTP clients (e.g., axios, requests, fetch) so they fail fast rather than hanging the entire Lambda execution.

VPC Network Timeouts: If your Lambda is in a VPC and needs internet access, it MUST be placed in a Private Subnet with a route to a NAT Gateway. Lambdas in Public Subnets do not get public IP addresses and will silently drop outbound internet traffic, leading to 100% timeout rates on third-party API calls.

Fix 2: Resolving 502 Bad Gateway

Ensure your error handling catches exceptions and explicitly returns a formatted 500 status code JSON object to API Gateway instead of letting the Node.js or Python runtime crash. A crashed runtime returns an empty response to API Gateway, which translates to a 502.

Fix 3: Fixing Access Denied and Permissions

To fix lambda:InvokeFunction access denied errors for API Gateway, you must grant API Gateway permission to trigger your function. Use the AWS CLI to add the necessary statement to the resource-based policy. Also, verify that the Lambda's Execution Role has policies attached (like AWSLambdaBasicExecutionRole) to write logs to CloudWatch, and explicit Allow statements for DynamoDB, S3, or other accessed resources.

Fix 4: Mitigating Throttling and Rate Limits

If you are hitting rate limits:

  1. Request a quota increase for concurrent executions from AWS Support (default is 1000 per region).
  2. Implement a dead-letter queue (DLQ) or an asynchronous invocation pattern using SQS so that throttled events are queued and retried automatically rather than dropped.
  3. Use Provisioned Concurrency to keep execution environments initialized and ready to respond immediately, entirely bypassing cold starts and ensuring a dedicated pool of concurrency for critical APIs.

Advanced Debugging with AWS X-Ray

For complex architectures where Lambda calls DynamoDB, which triggers another Lambda, identifying exactly where the timeout or permission denial occurred is impossible with just CloudWatch. Enable AWS X-Ray Active Tracing on your Lambda function. This provides a visual service map showing the latency and HTTP status codes of every downstream AWS SDK call your function makes. If your Lambda timeout is caused by a slow DynamoDB query, X-Ray will highlight the exact segment taking 9 seconds.

Frequently Asked Questions

bash
# 1. Check current Lambda function timeout and concurrency configuration
aws lambda get-function-configuration --function-name MyLambdaFunction

# 2. View recent CloudWatch logs specifically looking for timeout messages
aws logs filter-log-events --log-group-name /aws/lambda/MyLambdaFunction \
  --filter-pattern "Task timed out" --limit 5

# 3. Increase the Lambda function timeout to 15 seconds (max 900)
aws lambda update-function-configuration --function-name MyLambdaFunction \
  --timeout 15

# 4. Grant API Gateway permission to invoke the Lambda (fixes 403 Forbidden / Access Denied)
aws lambda add-permission \
  --function-name MyLambdaFunction \
  --statement-id apigateway-invoke-permission \
  --action lambda:InvokeFunction \
  --principal apigateway.amazonaws.com \
  --source-arn "arn:aws:execute-api:us-east-1:123456789012:api-id/*/*/*"

# 5. Set reserved concurrency to prevent noisy neighbor throttling
aws lambda put-function-concurrency \
  --function-name MyLambdaFunction \
  --reserved-concurrent-executions 100
E

Error Medic Editorial

Error Medic Editorial consists of senior Site Reliability Engineers and DevOps architects with over a decade of experience operating high-scale serverless architectures on AWS. We specialize in demystifying cloud errors and building resilient distributed systems.

Sources

Related Articles in AWS Lambda

Explore More Cloud Infrastructure Guides