CodexBloom - Programming Q&A Platform

AWS Lambda Function Times Out When Invoked by API Gateway but Works Locally

👀 Views: 434 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-18
aws lambda api-gateway dynamodb JavaScript

I'm having trouble with I'm currently experiencing a timeout issue with my AWS Lambda function when it's invoked through API Gateway. The function works perfectly fine when I test it locally using the AWS SAM CLI, where it completes in under 200 milliseconds. However, when I call it via the API Gateway, it times out after 30 seconds. I've verified that the Lambda function is properly configured with the correct IAM roles and permissions, and I have set the timeout for both the Lambda function and the API Gateway to the maximum allowable values. The function is written in Node.js 14.x and uses the AWS SDK to interact with DynamoDB. Here's a simplified version of my Lambda function: ```javascript const AWS = require('aws-sdk'); const dynamoDB = new AWS.DynamoDB.DocumentClient(); exports.handler = async (event) => { const params = { TableName: 'MyTable', Key: { id: event.id } }; try { const data = await dynamoDB.get(params).promise(); return { statusCode: 200, body: JSON.stringify(data), }; } catch (error) { return { statusCode: 500, body: JSON.stringify({ message: error.message }), }; } }; ``` When I invoke the Lambda function via API Gateway, I receive the following error message: ``` { "statusCode": 504, "body": "Timeout" } ``` To troubleshoot, I've added logging using `console.log` statements, but the logs show that the function never gets executed when called from the API Gateway. I also checked CloudWatch logs and noticed that the invocation request is received but never processed. I've tried increasing the Lambda function's timeout to 30 seconds and ensured that the event payload structure from API Gateway matches what the function expects. I've also looked into the API Gateway logs, which indicate that the integration timeout is being hit. Is there something I'm missing in the configuration for API Gateway or Lambda? I'm unsure how to proceed to resolve this timeout issue. Any guidance would be greatly appreciated! Any ideas what could be causing this? Am I approaching this the right way?