amazon-web-servicesaws-lambda

Intermittent 500 Internal Server Error in AWS Lambda API (Different endpoints fail randomly)


I’m facing an issue with my React frontend and AWS Lambda backend setup.

When my app makes multiple API calls at the same time, some of them randomly return a 500 Internal Server Error, while others succeed. On the next attempt, the previously failed APIs might work, and a few different ones fail instead there’s no consistent pattern.

Here’s an example response from the browser network tab:

Request URL: https://api.example.com/account/night-audit/business-config
Request Method: GET
Status Code: 500 Internal Server Error
x-amzn-errortype: InternalServerErrorException
x-amzn-requestid: ba6476cd-4310-4eaf-9333-d41123e6654f

I also checked the backend logs in AWS CloudWatch, and for these failed requests, the Lambda function was never triggered, meaning the request didn’t even reach the function handler.

Setup:

What I’ve observed:

My Question:


Solution

  • The api gateway might have encountered some issues and is the one sending the error with status code 500.

    It is hard to say what is going wrong on the api gateway, however with some additional logging, it becomes easier to figure what is going on.

    This article will help to log the error and its description when it happens: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-troubleshooting-lambda.html

    The idea is to log any integration errors which happens on the AWS Lambda level, not on the functions behind it. This is be done by creating a new log group for the api gateway and enriching the logging to include the $context.integrationErrorMessage or $context.integration.error.