即使将超时配置为2分钟,AWS Lambda也会在30秒后超时

时间:2020-09-17 01:53:33

标签: c# amazon-web-services aws-lambda amazon-sqs

我有一个用C#编写的AWS Lambda函数,该函数:

  • 由SQS队列上的消息触发
  • 对外部(非AWS)服务进行2次(慢速/长时间)HTTP REST调用
  • 将消息发送到SQS队列

我已将Lambda基本设置超时配置为2分钟。 enter image description here

但是,如果2个HTTP REST调用花费的时间超过30秒,则Lambda超时: enter image description here

这是相关的代码,您可以在代码和日志中看到对齐的日志语句:

src= C:\Users\ekrem\Desktop\16-17\26231043_1686575684735993_7548330554586169888_n.jpg

此服务http://slowwly.robertomurray.co.uk/delay/35000/url/http://www.google.co.uk仅延迟35000毫秒,然后提供来自“ http://www.google.co.uk”的响应。

如果HTTP REST调用花费的时间少于30秒,则Lambda完成并向输出SQS队列写入消息。在此示例中,我将延迟/睡眠持续时间更改为5秒而不是35秒,因此总执行时间少于30秒: enter image description here

万一该问题与C#GetAsync / task.Wait()的使用有关,我只是进行了测试并发现了相同的超时行为,如果我调用:

        static void get1()
        {
            using var client = new HttpClient();
            Console.WriteLine("Before get1");
            var task = Task.Run(() => client.GetAsync("http://slowwly.robertomurray.co.uk/delay/35000/url/http://www.google.co.uk"));
            Console.WriteLine("get1 initiated, about to wait");
            task.Wait();
            Console.WriteLine("get1 wait complete");

            var result = task.Result;
            Console.WriteLine("After get1, result: " + result.StatusCode);
        }

哪个给我日志输出: enter image description here

我想知道是否应该在Lambda中使用AWS开发工具包API记录控制台以配置配置的超时,以证明我配置的超时是“活动/有效/注意”等。

如果需要的话,这里的完整的端到端编排是:

  • 邮递员测试客户端->
  • AWS API GW->
  • AWS Lambda1->
  • AWS SQS->
  • AWS Lambda2->
    • REST API调用
  • AWS SQS

AWS Lambda2是过早超时的,并显示在日志中。

我似乎只有一个版本: enter image description here

和一个别名: enter image description here

1 个答案:

答案 0 :(得分:0)

虽然Lambda本身有2分钟的超时时间,但您看到的超时时间实际上可能是由于30秒的AWS API Gateway限制所致。 https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html