无法查看Lambda函数的CloudWatch日志且执行超时,求调试方案
问题:API Gateway触发Lambda超时且无日志输出
问题背景
用CDK创建AWS API Gateway并关联searchLambda,发送GET请求/?search=something&criteria=city时,请求会集成到searchLambda,理论上Lambda应接收参数并执行工作流,但调用API时出现超时错误,且Lambda顶部添加的console.log未在CloudWatch中输出。
错误日志
API Gateway日志:
(e276cbf8-8445-437b-93f7-fab1b32d8a22) Lambda execution failed with status 200 due to customer function error: 2023-03-01T19:59:11.265Z 9e793aa6-6e64-41e8-8237-b08ee11512be Task timed out after 10.80 seconds. Lambda request id: 9e793aa6-6e64-41e8-8237-b08ee11512be
注:已将Lambda超时时间从3秒调至10秒,问题仍存在
Lambda CloudWatch日志:
2023-03-01T16:44:29.503-05:00 INIT_START Runtime Version: nodejs:18.v5 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:c869d752e4ae21a3945cfcb3c1ff2beb1f160d7bcec3b0a8ef7caceae73c055f INIT_START Runtime Version: nodejs:18.v5 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:c869d752e4ae21a3945cfcb3c1ff2beb1f160d7bcec3b0a8ef7caceae73c055f 2023-03-01T16:44:41.491-05:00 START RequestId: 6c70644b-6ceb-4b9a-83f3-b90b36aa6ebd Version: $LATEST START RequestId: 6c70644b-6ceb-4b9a-83f3-b90b36aa6ebd Version: $LATEST 2023-03-01T16:44:41.652-05:00 2023-03-01T21:44:41.652Z 6c70644b-6ceb-4b9a-83f3-b90b36aa6ebd Task timed out after 10.21 seconds 2023-03-01T21:44:41.652Z 6c70644b-6ceb-4b9a-83f3-b90b36aa6ebd Task timed out after 10.21 seconds 2023-03-01T16:44:41.652-05:00 END RequestId: 6c70644b-6ceb-4b9a-83f3-b90b36aa6ebd END RequestId: 6c70644b-6ceb-4b9a-83f3-b90b36aa6ebd 2023-03-01T16:44:41.652-05:00 REPORT RequestId: 6c70644b-6ceb-4b9a-83f3-b90b36aa6ebd Duration: 10208.91 ms Billed Duration: 10000 ms Memory Size: 128 MB Max Memory Used: 128 MB
Lambda配置
{ "FunctionName": "SearchFlowStack-searchLambda02D5D642-rXVh6Hop7PM3", "FunctionArn": "arn:aws:lambda:us-east-1:201682123230:function:SearchFlowStack-searchLambda02D5D642-rXVh6Hop7PM3", "Runtime": "nodejs18.x", "Role": "arn:aws:iam::201682123230:role/SearchFlowStack-searchLambdaServiceRole4ED34712-1HDDNCA114A09", "Handler": "index.handler", "CodeSize": 5479738, "Description": "", "Timeout": 10, "MemorySize": 128, "LastModified": "2023-03-01T19:57:32.000+0000", "CodeSha256": "HCyIEDuPx69arYaP+yKdkfRDmXLrVuiy/BO+AsfteDA=", "Version": "$LATEST", "VpcConfig": { "SubnetIds": [], "SecurityGroupIds": [], "VpcId": "" }, "Environment": { "Variables": { "STATE_MACHINE_ARN": "arn:aws:states:us-east-1:201682123230:stateMachine:cfnStepFunction-Iz9cXZoGzegj", "AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1" } }, "TracingConfig": { "Mode": "PassThrough" }, "RevisionId": "83cb39a5-4e5e-48ca-a838-cf28344b2e04", "State": "Active", "LastUpdateStatus": "Successful", "PackageType": "Zip", "Architectures": [ "x86_64" ] }
Lambda代码片段
import { Lambda, StepFunctions } from 'aws-sdk'; import { APIGatewayProxyEvent } from 'aws-lambda'; import { SearchFlowStack } from '../../infra/SearchFlow'; export const handler = async (event: APIGatewayProxyEvent): Promise<any> => { console.log(`#### Step00. Started!!`) const { search, criteria } = event.queryStringParameters || {}; const stepFunctions = new StepFunctions(); const lambda = new Lambda(); const { cfnStepFunction, cfnSearchResultsFunction } = SearchFlowStack; // Start the Step Function with the given criteria const params = { stateMachineArn: cfnStepFunction.attrArn, input: JSON.stringify({ searchTerm: search, criteria }) }; .... ....
调试思路与方法
- 排查代码初始化阻塞:Lambda未输出初始
console.log,说明代码可能在handler执行前就卡住。检查导入的SearchFlowStack是否存在同步阻塞操作,比如静态初始化时执行了耗时的CDK资源解析或网络请求。 - 提升内存配置:当前Lambda内存为128MB(最低配置),Lambda内存与CPU性能成正比,内存不足会导致CPU受限。尝试将内存提升至256MB或更高,观察是否仍超时。
- 检查IAM权限:确认Lambda角色拥有调用Step Functions的
states:StartExecution权限,若权限缺失,SDK调用会静默等待直到Lambda超时。 - 添加SDK调用超时:为StepFunctions和Lambda客户端设置超时时间,避免无限等待。示例:
const stepFunctions = new StepFunctions({ httpOptions: { timeout: 5000 } // 设置5秒超时 }); - 本地模拟测试:使用
aws-sdk-mock或serverless-offline在本地模拟API Gateway请求,检查代码是否能正常执行,定位初始化或StepFunction调用环节的问题。 - 检查CloudWatch日志权限:确认Lambda角色拥有
logs:CreateLogGroup、logs:CreateLogStream、logs:PutLogEvents权限,否则日志无法写入CloudWatch。 - 排查StepFunction状态:检查目标StepFunction是否正常(如是否处于暂停状态、资源是否存在),若目标资源不可用,
StartExecution调用会一直等待直到Lambda超时。
内容的提问来源于stack exchange,提问作者track_p54skate
相关产品推荐
相关产品推荐

