AWS CDK Step Functions问题:Lambda任务返回Payload始终为Null
问题
使用AWS CDK构建Step Functions工作流时,第一个Lambda调用正常,第二个Lambda的输入符合预期,但Lambda任务输出的Payload始终为null。期望Lambda返回的output键中数据能传递给下一个任务。
Lambda处理代码
export const bulkSummaryHandler = (event) => { try { console.log('LAMBA SUMMARY!', event); return { output: { status: 'finished' } }; } catch (error) { return handleError(error); } };
CDK代码
const getUserCsvFileTask = new tasks.LambdaInvoke(ctx.stack, 'getUserCsvFileTask', { lambdaFunction: getUserCsvFileFn, comment: 'fetch user uploaded csv from csv-integration-service', inputPath: '$', resultPath: '$.taskResult', outputPath: '$.taskResult.Payload' }); const bulkSummaryTask = new tasks.LambdaInvoke(ctx.stack, 'bulkProcessingSummaryTask', { lambdaFunction: bulkSummaryFn, comment: 'summarise bulk processing', inputPath: '$' }); const definition = stepfunctions.Chain.start(getUserCsvFileTask).next(bulkSummaryTask).next(nextLambdaTask);
响应示例
{ "ExecutedVersion": "$LATEST", "Payload": null, "SdkHttpMetadata": { "AllHttpHeaders": { "X-Amz-Executed-Version": [ "$LATEST" ], "x-amzn-Remapped-Content-Length": [ "0" ], "Connection": [ "keep-alive" ], "x-amzn-RequestId": [ "fed8b1bd-d188-4425-ade7-ce2723aef4c8" ], "Content-Length": [ "4" ], "Date": [ "Wed, 21 Sep 2022 22:54:00 GMT" ], "X-Amzn-Trace-Id": [ "root=1-632b9607-0e451e4c5dd4c21c7a3eaa8b;sampled=1" ], "Content-Type": [ "application/json" ] }, "HttpHeaders": { "Connection": "keep-alive", "Content-Length": "4", "Content-Type": "application/json", "Date": "Wed, 21 Sep 2022 22:54:00 GMT", "X-Amz-Executed-Version": "$LATEST", "x-amzn-Remapped-Content-Length": "0", "x-amzn-RequestId": "fed8b1bd-d188-4425-ade7-ce2723aef4c8", "X-Amzn-Trace-Id": "root=1-632b9607-0e451e4c5dd4c21c7a3eaa8b;sampled=1" }, "HttpStatusCode": 200 }, "SdkResponseMetadata": { "RequestId": "fed8b1bd-d188-4425-ade7-ce2723aef4c8" }, "StatusCode": 200 }
解决思路
- 验证Lambda实际返回值:查看Lambda的CloudWatch日志,确认函数是否真的返回了
{ output: { status: 'finished' } }。重点检查handleError函数的返回值,如果它返回null,会直接导致Payload为null。 - 调整Lambda函数写法:将Lambda改为异步函数测试,Node.js环境下异步写法的返回值序列化更稳定:
export const bulkSummaryHandler = async (event) => { try { console.log('LAMBA SUMMARY!', event); return { output: { status: 'finished' } }; } catch (error) { return handleError(error); } }; - 配置Step Functions的结果路径:给第二个LambdaInvoke任务添加
resultPath和outputPath,确保返回的output能传递给下一个任务:const bulkSummaryTask = new tasks.LambdaInvoke(ctx.stack, 'bulkProcessingSummaryTask', { lambdaFunction: bulkSummaryFn, comment: 'summarise bulk processing', inputPath: '$', resultPath: '$.bulkSummaryResult', // 保留原有工作流数据,新增当前任务结果 outputPath: '$.bulkSummaryResult.Payload.output' // 仅传递Lambda返回的output内容给下一个任务 }); - 检查Lambda执行权限与运行环境:确认Lambda函数拥有足够权限(比如CloudWatch日志写入权限),同时验证Node.js版本与代码兼容,且部署的是最新版本的函数代码。
内容的提问来源于stack exchange,提问作者Outto320
相关产品推荐
相关产品推荐

