Question: How can I troubleshoot 5xx errors for API Gateway?

When I call my Amazon API Gateway API, I get an 5xx error.

Short descriptionHTTP 5xx response codes indicate server errors. API Gateway 5xx errors include the following cases:500 internal server502 bad gateway503 service unavailable504 endpoint request timed out

ResolutionBefore you begin, follow the steps to turn on Amazon CloudWatch Logs for troubleshooting API Gateway errors.Use the CloudWatch logs to find 5xx errors from API Gateway. The API Gateway metric 5XXError counts the number of server-side errors that are captured in a given period.Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you're using the most recent AWS CLI version.500 error: internal server errorThis error can occur because of any of the following scenarios:Errors in the AWS Lambda function codeMissing permissions for using a stage variableIncorrect or missing HTTP status code mappingThrottling issuesUndefined HTTP method of POSTLambda permissionsLambda function JSON format issueBackend payload size exceeding 10 MBPrivate endpoint integrationInternal service failuresErrors in the Lambda function codeAPI endpoint 500 errors that integrate with Lambda might indicate that the Lambda function has an error in the code. For more information and troubleshooting, see Error handling patterns in Amazon API Gateway and AWS Lambda.Missing permissions for using a stage variableIf you use a stage variable to set up an API Gateway to invoke a Lambda function, then you might receive an Internal server error. To resolve this error, see I defined my Lambda integration in API Gateway using a stage variable. Why do I get an "Internal server error" and a 500 status code when I invoke the API method?Incorrect or missing HTTP status code mappingIncorrect or missing HTTP status code mapping can also result in 500 errors. To resolve this issue, set up mock integrations in API Gateway.Throttling issuesIf a high number of requests is throttling the backend service, then the API Gateway API might return an Internal server error. To resolve this issue, activate an exponential backoff and retry mechanism, and then try the request again. If the issue persists, then check your API Gateway quota limit. If you exceed the service quota limit, then you can request a quota increase.Undefined HTTP method of POSTFor Lambda integration, you must use the HTTP method of POST for the integration request.Run the AWS CLI command put-integration to update the method integration request:aws apigateway put-integration \    --rest-api-id id \    --resource-id id \    --http-method ANY \    --type AWS_PROXY \    --integration-http-method POST \    --uri arn:aws:apigateway:us-east-2:lambda:path//2015-03-31/functions/arn:aws:lambda:us-east-2:account_id:function:helloworld/invocationsThen, use the AWS CLI command create-deployment to deploy the REST API:aws apigateway create-deployment \    --rest-api-id id \    --stage-name <value>Lambda permissionsMake sure that the integrated Lambda function's or Lambda authorizer's resource-based policy includes permissions for your API to invoke the function. Follow the instructions to update your Lambda function's resource-based policy.Lambda function JSON format issueThe integrated Lambda function isn't returning output according to the predefined JSON format for REST APIs and HTTP APIs. Update your Lambda function or Lambda authorizer in JSON format:REST API:{    "isBase64Encoded": true|false,    "statusCode": httpStatusCode,    "headers": { "headerName": "headerValue", ... },    "multiValueHeaders": { "headerName": ["headerValue", "headerValue2", ...], ... },    "body": "..."}HTTP API:{    "isBase64Encoded": true|false,    "statusCode": httpStatusCode,    "headers": { "headername": "headervalue", ... },    "multiValueHeaders": { "headername": ["headervalue", "headervalue2", ...], ... },    "body": "..."}Backend payload size exceeding 10 MBThe maximum backend payload size is 10 MB. You can't increase the size. Make sure that the backend payload size doesn't exceed the 10 MB default quota.Private endpoint integrationIf you're using a private API endpoint, you must also configure API Gateway private integration. Follow the instructions to set up API Gateway private integrations.Internal service failuresIf AWS experiences internal service problems, then you might receive a 500 error. Wait for the issue to resolve within AWS or the API Gateway service, and then retry the request with exponential backoff.502 error: bad gatewayA 502 error code is related to the AWS service that your API Gateway integrates with, such as an AWS Lambda function. API Gateway can't process the response as a gateway or proxy.To troubleshoot this issue, see How do I resolve HTTP 502 errors from API Gateway REST APIs with Lambda proxy integration?Note: When API Gateway interprets the response from the backend service, it uses mapping templates to map the format in the integration response section. For more information, see Set up an integration response in API Gateway.503 error: service unavailableA 503 error code is related to the backend integration and if the API Gateway API can't receive a response.This error might occur in the following scenarios:The backend server is overloaded beyond capacity and can't process new client requests.The backend server is under temporary maintenance.To resolve this error, consider provisioning more resources to the backend server and activating an exponential backoff and retry mechanism on the client. Then, try the request again.504 error: endpoint request timed outIf an integration request takes longer than your API Gateway REST API maximum integration timeout parameter, API Gateway returns an HTTP 504 status code.To resolve this error, see How can I troubleshoot API HTTP 504 timeout errors with API Gateway?Related informationSecurity best practices in Amazon API GatewayMonitoring REST API execution with Amazon CloudWatch metrics