首先,需要确保您的 AWS CDK Step Functions 定义中的每个状态都设置了正确的返回值。其次,您需要使用正确的 step function 调用方式来返回这些返回值。
下面是一个示例:
import * as cdk from 'aws-cdk-lib';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
const stack = new cdk.Stack(app, 'MyStack');
const stateMachine = new sfn.StateMachine(stack, 'StateMachine', {
definition: new tasks.LambdaInvoke(stack, 'Task', {
lambdaFunction,
// Set the task's input and output path
outputPath: '$.Payload',
}),
});
const task = new sfn.Task(stack, 'Task', {
task: new tasks.LambdaInvoke(stack, 'MyLambda', {
lambdaFunction: fn,
outputPath: '$.Payload',
}),
// Define the state's output path
outputPath: '$.Payload',
});
const definition = new sfn.Choice(stack, 'Make a choice')
.when(sfn.Condition.stringEquals('$.status', 'success'), task)
.when(sfn.Condition.stringEquals('$.status', 'failure'), sfn.Fail(stack, 'Failed', { error: 'The task failed!' }))
.otherwise(sfn.Fail(stack, 'Invalid status!', { error: 'The status is not valid!' }));
const stateMachine = new sfn.StateMachine(stack, 'MyStateMachine', {
definition,
});
// Invoke the state machine
const invoker = new sfn.StateMachine(stack, 'MyInvoker', {
stateMachine,
});
// Call the state machine using Lambda
const lambda = new lambda.Function(stack, 'MyLambdaFn', {
code,
handler: 'handler.handler',
runtime: lambda.Runtime.NODEJS_12_X,
environment: {
STATE_MACHINE_ARN: stateMachine.stateMachineArn,
},
});
// Grant permission to invoke the state machine from this Lambda function
stateMachine.grantStartExecution(lambda);
请注意,上述示例中的每个状态都设置了正确的输出路径,并且使用正确的 step function 调