CodexBloom - Programming Q&A Platform

AWS Lambda Timing Out When Invoked via API Gateway with VPC Access

👀 Views: 36 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-11
AWS Lambda APIGateway VPC Python

I'm following best practices but I've been struggling with this for a few days now and could really use some help. I'm working with an scenario where my AWS Lambda function is timing out after 30 seconds when invoked through API Gateway. The Lambda function is configured to run inside a VPC, and I've made sure to attach the necessary security group that allows it to access the internet through a NAT gateway. However, the function works perfectly when invoked directly from the AWS console. The behavior I receive when invoking it through the API Gateway is: ``` { "statusCode": 504, "body": "Timeout" } ``` I've tried increasing the timeout setting of my Lambda function to 60 seconds and also checked the CloudWatch logs, which indicate that the function execution starts but then just hangs and never completes when called via the API. Here is the relevant portion of my Lambda function code: ```python import json def lambda_handler(event, context): # Simulating a long-running process time.sleep(35) return {"statusCode": 200, "body": json.dumps('Success!')} ``` The API Gateway is set to use the default timeout of 29 seconds, which is less than what my function is set to, so I'm wondering if there's a misconfiguration somewhere. I've also checked the VPC configurations, route tables, and security groups, and they all seem correct. Is there something specific I should look into regarding Lambda execution inside VPCs that could cause this timeout when accessed via API Gateway? Any suggestions would be greatly appreciated! I'm working on a application that needs to handle this. This is for a microservice running on Windows 11. What are your experiences with this?