有没有办法使用 CDK 创建阶跃函数图?

Is there a way to create step functions graph using CDK?

目前阶跃函数图是在 json 中使用 ASL(亚马逊状态语言)定义的。 没有很好的方法来对正在创建的图进行单元测试,我们必须依赖 运行 具有 mocked/some 在 aws 帐户上实现 lambda 函数的图。 随着 AWS CDK 的推出,是否可以用高级语言定义步骤函数图并进行单元测试、模拟等?

您试过 @aws-cdk/aws-stepfunctions 库了吗?

为后代复制文档中的示例代码:

const submitLambda = new lambda.Function(this, 'SubmitLambda', { ... });
const getStatusLambda = new lambda.Function(this, 'CheckLambda', { ... });

const submitJob = new stepfunctions.Task(this, 'Submit Job', {
    resource: submitLambda,
    // Put Lambda's result here in the execution's state object
    resultPath: '$.guid',
});

const waitX = new stepfunctions.Wait(this, 'Wait X Seconds', { secondsPath: '$.wait_time' });

const getStatus = new stepfunctions.Task(this, 'Get Job Status', {
    resource: getStatusLambda,
    // Pass just the field named "guid" into the Lambda, put the
    // Lambda's result in a field called "status"
    inputPath: '$.guid',
    resultPath: '$.status',
});

const jobFailed = new stepfunctions.Fail(this, 'Job Failed', {
    cause: 'AWS Batch Job Failed',
    error: 'DescribeJob returned FAILED',
});

const finalStatus = new stepfunctions.Task(this, 'Get Final Job Status', {
    resource: getStatusLambda,
    // Use "guid" field as input, output of the Lambda becomes the
    // entire state machine output.
    inputPath: '$.guid',
});

const definition = submitJob
    .next(waitX)
    .next(getStatus)
    .next(new stepfunctions.Choice(this, 'Job Complete?')
        // Look at the "status" field
        .when(stepfunctions.Condition.stringEquals('$.status', 'FAILED'), jobFailed)
        .when(stepfunctions.Condition.stringEquals('$.status', 'SUCCEEDED'), finalStatus)
        .otherwise(waitX));

new stepfunctions.StateMachine(this, 'StateMachine', {
    definition,
    timeoutSec: 300
});