EXPRESS类型状态机不支持listExecutions,求替代API方案
Express类型状态机获取执行列表的替代方案
问题背景
已实现一个Express类型的状态机,尝试调用listExecutions API获取其执行列表,代码如下:
const stepFunction = new SFN({ region: REGION }); const listExecutionsInput = { stateMachineArn: STATE_MACHINE_ARN, statusFilter: ExecutionStatus.RUNNING, }; const runningStateMachines = await stepFunction.listExecutions(listExecutionsInput);
执行后抛出错误:
{ message: 'This operation is not supported by this type of state machine', error: { name: 'StateMachineTypeNotSupported', $fault: 'client', $metadata: { httpStatusCode: 400, requestId: 'ciefq5881-6713-4662-b8fe-e0ddqa9ef4d1', attempts: 1, totalRetryDelay: 0, }, __type: 'com.amazonaws.swf.service.v2.model#StateMachineTypeNotSupported', }, platform: 'Unknown', releaseId: 'Unknown', incomingHttpRequest: {}, fatal: true, }
经确认,listExecutions API不支持Express类型状态机,需寻找替代方案获取近期执行的名称。
替代方案
Express类型状态机的执行记录默认会同步到CloudWatch Logs,可通过以下方式获取执行信息:
1. 查询CloudWatch Logs提取执行名称
Express状态机的日志组通常格式为/aws/vendedlogs/states/<状态机名称>-Logs,也可在Step Functions控制台的状态机配置页查看具体日志组。通过CloudWatch Logs的filterLogEvents API过滤包含executionArn的日志条目,从中提取执行名称(Arn的最后一段)。
示例代码:
const cloudwatchlogs = new CloudWatchLogs({ region: REGION }); const params = { logGroupName: '/aws/vendedlogs/states/你的状态机名称-Logs', startTime: Date.now() - 86400000, // 查询最近24小时的日志 filterPattern: '"executionArn"' // 过滤包含executionArn的日志 }; const logEvents = await cloudwatchlogs.filterLogEvents(params).promise(); // 提取去重后的执行名称 const executionNames = [...new Set(logEvents.events.map(event => { const logData = JSON.parse(event.message); return logData.executionArn.split('/').pop(); }))];
2. 启动执行时主动记录Arn
调用startExecution启动Express状态机时,会返回对应的executionArn,可将该值存储到DynamoDB或其他存储服务中,后续查询时直接从存储服务读取。这种方式可自定义查询条件,且无日志延迟问题。
3. 针对运行中执行的补充说明
如果仅需获取正在运行的Express执行,可调用describeExecution API,但前提是你已持有目标执行的Arn,若未提前记录则无法直接使用。
注意事项
- CloudWatch Logs存在一定延迟,默认日志保留时长为90天,如需长期保存需调整日志组的保留策略。
- 使用CloudWatch Logs查询时,建议限定合理的时间范围,避免查询范围过大导致性能问题。
内容的提问来源于stack exchange,提问作者Ibrahim
相关产品推荐
相关产品推荐

