简体   繁体   English

WhatsApp 消息的 Lambda AWS 和 Twilio 出现“X 秒后任务超时”错误

[英]"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.我试图实现 Lambda function 以使用 Twilio 服务发送 WhatsApp 消息。

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 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).我正在使用节点 14 和我的压缩 npm package 与 nodejs/node_modules... 结构(不是 node14,但我知道它可以与两者一起使用)。 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.我将默认的 3 秒更改为 5 秒,但仍然出现错误。

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:这是 Lambda 控制台中的响应:

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

I solved this by increasing the timeout time.我通过增加超时时间解决了这个问题。

I changed from 5 seconds to 1 minute.我从 5 秒变成了 1 分钟。 Looks like the first request in a while takes around 15 seconds.看起来一段时间内的第一个请求大约需要 15 秒。 Request afters that take miliseconds.请求之后需要几毫秒。

The same error also happens, do you know why the first request takes so long?同样的错误也会发生,你知道为什么第一个请求需要这么长时间吗?

let response = await this.instance.post("/messages", parameters, {
            headers: {
                Authorization: `Bearer ${tokenDev}`,
            },
        });

In my case I use the WhatsApp API directly.就我而言,我直接使用 WhatsApp API。 In local it works fine for me but when uploading it to a lambda the error occurs.在本地它对我来说很好,但是在将它上传到 lambda 时会发生错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM