- Newest
- Most votes
- Most comments
When a Lambda function times out, API Gateway will return a 200 HTTP status code by default. This behavior occurs because the Lambda function successfully completed its execution from API Gateway's perspective, even though it might have timed out internally.
To return a different status code, such as 408 for request timeout, you can implement error handling within your Lambda function and explicitly return the desired status code along with the response. Here's how you can modify your Lambda function to return a 408 status code for timeouts:`
def lambda_handler(event, context): try: # Your login logic here # If the operation takes too long and times out, the following line will cause a timeout error # Simulating a timeout error import time time.sleep(5)
# If login successful, return success response
return {
"statusCode": 200,
"body": "Login successful"
}
except Exception as e:
# If an exception occurs, check if it's a timeout error
if "Task timed out" in str(e):
# Return 408 status code for request timeout
return {
"statusCode": 408,
"body": "Request timed out"
}
else:
# For other exceptions, return 500 status code
return {
"statusCode": 500,
"body": "Internal server error"
}
`
Hi
Suggestions: https://aws.amazon.com/blogs/compute/error-handling-patterns-in-amazon-api-gateway-and-aws-lambda/
- Increase the Lambda timeout if your function genuinely needs more time (up to 15 minutes).
- Consider asynchronous processing (e.g., SQS queues) for long-running tasks to avoid timeouts altogether.
Map the response status codes for API Gateways:https://repost.aws/knowledge-center/api-gateway-status-codes-http-api
This Video Some More clear to you --> https://www.youtube.com/watch?v=uC2IQwlMJQU
appreciated thank you!
Relevant content
- Accepted Answerasked a year ago
- asked 6 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
appreciated thank you!