Reputation: 100
I am sending an array of objects to an AWS Lambda function so it can fire off a POST request for each object in the array but the lambda function keeps giving me an error in the API Gateway log:
Execution failed due to a timeout error
After 30 seconds of:
Sending request to https://lambda.ap-southeast-1.amazonaws.com/2015-03-31/functions/...
I am also getting duplicate POST request because Lambda keeps retrying up to 5 times after every failed attempt and starts over on the array each time.
Here is the lambda function:
module.exports.shipments = async (event) => {
const axios = require("axios");
let data = JSON.parse(event.body);
let url = data.apiURL + "/api/1.1/wf/bulkshipments";
let patchURL = data.apiURL + "/api/1.1/obj/company/" + data.companyID;
let good = [];
let bad = [];
let promises = data.shipments.map(item =>
axios.post(url, {
batchID: data.batchID,
companyID: data.companyID,
shipment: item
})
//.then( res => good.push({"Order Reference": item.order_reference, "result": res.data}))
//.catch( error => bad.push({"Order Reference": item.order_reference, "result": error.response.data}))
);
await Promise.all(promises);
return {
statusCode: 200,
body: JSON.stringify({
message: 'success',
totalShipments: data.shipments.length,
created: good.length,
errors: bad.length,
result: {"completed": good, "failed": bad}
}, null, 2),
};
};
Upvotes: 1
Views: 3706
Reputation: 2045
You are hitting the API Gateway timeout limit (30 seconds), rather than timing out on the Lambda function.
This 30 second API Gateway timeout is a hard limit and cannot be increased. For long running processes, consider making the function async, perhaps using a queuing mechanism such as SQS.
Note: even after the API Gateway times out after 30 seconds (returning the HTTP call), that does not necessarily mean the Lambda function gets terminated at the same time. The Lambda function may continue processing past the 30 second mark, up to its own timeout limit.
So the Lambda function could conceivably complete the remainder of its POST calls even after the client API call has returned with an error. This may be another reason why you are seeing many duplicates -- each time the API is called, the function possibly runs to completion despite the error message.
(n.b., unless the function is idempotent, generally it is good practice for Lambda functions to detect if they are called multiple times with the same data and handle such scenarios gracefully to prevent duplicates.)
Upvotes: 1