有没有一种使用CDK创建步骤函数图的方法?

时间:2018-12-03 03:31:09

标签: aws-step-functions aws-cdk

当前,步骤函数图是使用ASL(亚马逊州语言)在json中定义的。 没有好的方法可以对所创建的图形进行单元测试,我们必须依靠在aws帐户上使用lambda函数的模拟/某种实现来运行图形。 随着AWS CDK的发布,是否可以用高级语言定义步进函数图,并进行单元测试,模拟等?

1 个答案:

答案 0 :(得分:2)

您是否尝试过@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
});