Home > Mobile >  Send a lot of SQS messages in a lambda without getting timed out
Send a lot of SQS messages in a lambda without getting timed out

Time:03-18

I have an array, lets say with a length of 9999. Now I want to send a message to SQS with the contents of each object in the array.

Here is how I want to send the messages:

const promises = tokens.map((token) => {
    const params = {
      Body: JSON.stringify(data),
    };

    return sqs.sendMessage({
      MessageBody: JSON.stringify(params),
      QueueUrl: 'my-queue-url',
    }).promise();
  });

await Promise.all(promises);

Now, the problem is that I trigger this function via API Gateway, which times out after 30 seconds. How can I avoid this? Is there a better way to do this?

CodePudding user response:

Based on your response in the comments I suggest you do two things:

  1. Use the SendMessageBatch API to reduce the number of API calls and speed up the process as @Marc B suggests
  2. Set up asynchronous invocation of the backend Lambda function. This means the API Gateway will just send the Event to your Lambda and not wait for the response. That means the Lambda functions is free to run up to 15 minutes. If you have more messages than can be handled in that period of time, you may want to look into StepFunctions.
  • Related