Search code examples
amazon-web-servicestwilio

"Task timed out after X seconds" error with Lambda AWS and Twilio for WhatsApp message


I was trying to implement a Lambda function to Send WhatsApp messages with Twilio service.

I have already uploaded the twilio npm package (I was getting the "cannot fin twilio module", but I added the layer and I don't get the error anymore). I'm using node 14 and my zipped npm package is with the nodejs/node_modules... structure (not the node14, but I understood it can work with both). Maybe this is why it's not working?

I got stuck after that. I keep getting the "taks timed out". I changed from the default 3 to 5 seconds, but it still gets errors.

What am I missing or doing wrong? This is my code:

'use strict';
console.log('Trying to send a WhatsApp message...');

exports.handler = async (event) => {
    
const accountSid = 'ACa4818d82a4d6----------'; //The hyphens is to hide credentials or phone numbers
const authToken = '7e5d8205968af11----------'; 

const client = require('twilio')("ACa4818d------", "7e5d8205968af11-------");
//I event passed the parameters like this to troubleshoot
 
client.messages 
      .create({ 
         body: 'Hi, there!', 
         from: 'whatsapp:+14------',       
         to: 'whatsapp:+1-------' 
       }) 
      .then(message => console.log(message.sid))
      .done();
};

This is the response in Lambda console:

Test Event Name
TestCon

Response
{
  "errorMessage": "2021-12-05T04:39:26.463Z 74eb5536-7da6-4d96-bf8e-824230c85089 Task timed out after 5.01 seconds"
}

Function Logs
START RequestId: 74eb5536-7da6-4d96-bf8e-824230c85089 Version: $LATEST
2021-12-05T04:39:21.452Z    undefined   INFO    Trying to send a WhatsApp message...
END RequestId: 74eb5536-7da6-4d96-bf8e-824230c85089
REPORT RequestId: 74eb5536-7da6-4d96-bf8e-824230c85089  Duration: 5005.62 ms    Billed Duration: 5000 ms    Memory Size: 128 MB Max Memory Used: 86 MB  Init Duration: 176.11 ms    
2021-12-05T04:39:26.463Z 74eb5536-7da6-4d96-bf8e-824230c85089 Task timed out after 5.01 seconds

Request ID
74eb5536-7da6-4d96-bf8e-824230c85089

Solution

  • I solved this by increasing the timeout time.

    I changed from 5 seconds to 1 minute. Looks like the first request in a while takes around 15 seconds. Request afters that take miliseconds.