You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda Node.js扩展调用nextRuntime出现随机超时问题求助

AWS Lambda Node.js扩展随机超时问题排查

问题描述

我正在开发Node.js版AWS Lambda扩展,代码基于官方Node.js扩展示例。其中nextRuntime函数会向http://${process.env.AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next发送GET请求,next函数则向http://${process.env.AWS_LAMBDA_RUNTIME_API}/2020-01-01/extension/event/next发送GET请求。

移除nextRuntime调用时一切运行正常,但添加该函数调用后,每次触发Lambda函数都会出现随机结果:

  • 部分调用(如第1、2、3、7次)触发超时,返回null且日志显示任务超时;
  • 部分调用(如第4、5、6次)可正常执行。

相关代码

Lambda 处理代码

export const handler = (event: any) => {
  console.log('LAMBDA LAUNCHED');
  return {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda, deployed using AWS CDK!'),
  };
};

扩展代码

#!/usr/bin/env node
import { next, register } from './extension-api';
import { next as nextRuntime } from './runtime-api';


const EventType = {
  INVOKE: 'INVOKE',
  SHUTDOWN: 'SHUTDOWN',
};

function handleShutdown(event: any) {
  console.log('shutdown', { event });
  process.exit(0);
}

function handleInvoke(event: any) {
  console.log('invoke', event);
}
const timeout = (time: number) => new Promise((resolve) => setTimeout(resolve, time, 'timeout'));
void (async function main() {
  console.log('extension  started');
  process.on('SIGINT', () => handleShutdown('SIGINT'));
  process.on('SIGTERM', () => handleShutdown('SIGTERM'));
  const extensionId = await register();
  while (true) {
    const event: any = await next(extensionId as string);
    const eventRuntime = await Promise.race([
       nextRuntime(),
       timeout(5000),
    ]);
    console.log('eventRuntime', eventRuntime);
    
    switch (event.eventType) {
      case EventType.SHUTDOWN:
        handleShutdown(event);
        break;
      case EventType.INVOKE:
        handleInvoke(event);
        break;
      default:
        throw new Error('unknown event: ' + event.eventType);
    }
  }
})();

超时调用日志(第1、2、3、7次)

Test Event Name
Test

Response
null

Function Logs
Running custom-extension
extension  started
EXTENSION Name: custom-extension State: Ready Events: [SHUTDOWN,INVOKE]
START RequestId: 589720df-be2f-482d-a440-f181fe4bd181 Version: $LATEST
2022-11-18T17:38:52.967Z 589720df-be2f-482d-a440-f181fe4bd181 INFO LAMBDA LAUNCHED
event {
eventType: 'INVOKE',
deadlineMs: 1668793137965,
requestId: '589720df-be2f-482d-a440-f181fe4bd181',
invokedFunctionArn: 'arn:aws:lambda:ap-southeast-1::function:hello-world',
tracing: {
type: 'X-Amzn-Trace-Id',
value: 'Root=1-6377c32c-6ee917d7075c9c7655c22500;Parent=61cf916c0c5c3251;Sampled=0'
}
}
eventRuntime timeout
invoke {
eventType: 'INVOKE',
deadlineMs: 1668793137965,
requestId: '589720df-be2f-482d-a440-f181fe4bd181',
invokedFunctionArn: 'arn:aws:lambda:ap-southeast-1::function:hello-world',
tracing: {
type: 'X-Amzn-Trace-Id',
value: 'Root=1-6377c32c-6ee917d7075c9c7655c22500;Parent=61cf916c0c5c3251;Sampled=0'
}
}
event {
eventType: 'SHUTDOWN',
deadlineMs: 1668793139971,
shutdownReason: 'timeout'
}
shutdown {
event: {
eventType: 'SHUTDOWN',
deadlineMs: 1668793139971,
shutdownReason: 'timeout'
}
}
2022-11-18T17:38:57.987Z 589720df-be2f-482d-a440-f181fe4bd181 Task timed out after 5.02 seconds

END RequestId: 589720df-be2f-482d-a440-f181fe4bd181
REPORT RequestId: 589720df-be2f-482d-a440-f181fe4bd181 Duration: 5021.69 ms Billed Duration: 5000 ms Memory Size: 512 MB Max Memory Used: 68 MB Init Duration: 258.09 ms

Request ID
589720df-be2f-482d-a440-f181fe4bd181

正常调用日志(第4、5、6次)

Test Event Name
Test

Response
null

Function Logs
Running custom-extension
extension  started
EXTENSION Name: custom-extension State: Ready Events: [INVOKE,SHUTDOWN]
START RequestId: fc35da60-7019-4f1c-acd7-6575671f3ecb Version: $LATEST
event {
eventType: 'INVOKE',
deadlineMs: 1668793273802,
requestId: 'fc35da60-7019-4f1c-acd7-6575671f3ecb',
invokedFunctionArn: 'arn:aws:lambda:ap-southeast-1::function:hello-world',
tracing: {
type: 'X-Amzn-Trace-Id',
value: 'Root=1-6377c3b4-29e874313e329e1d60d0b457;Parent=42bf3d3b6e9b1f7d;Sampled=0'
}
}
eventRuntime { key1: 'value1', key2: 'value2', key3: 'value3' }
invoke {
eventType: 'INVOKE',
deadlineMs: 1668793273802,
requestId: 'fc35da60-7019-4f1c-acd7-6575671f3ecb',
invokedFunctionArn: 'arn:aws:lambda:ap-southeast-1::function:hello-world',
tracing: {
type: 'X-Amzn-Trace-Id',
value: 'Root=1-6377c3b4-29e874313e329e1d60d0b457;Parent=42bf3d3b6e9b1f7d;Sampled=0'
}
}
2022-11-18T17:41:09.557Z fc35da60-7019-4f1c-acd7-6575671f3ecb INFO LAMBDA LAUNCHED
END RequestId: fc35da60-7019-4f1c-acd7-6575671f3ecb
REPORT RequestId: fc35da60-7019-4f1c-acd7-6575671f3ecb Duration: 773.61 ms Billed Duration: 774 ms Memory Size: 512 MB Max Memory Used: 24 MB

Request ID
fc35da60-7019-4f1c-acd7-6575671f3ecb

内容的提问来源于stack exchange,提问作者Dmytro Filipenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 03:35:30