有没有一种方法可以使用CDK创建步骤函数图? [英] Is there a way to create step functions graph using CDK?
问题描述
当前,步骤函数图是使用ASL(亚马逊州语言)在json中定义的.没有好的方法可以对正在创建的图形进行单元测试,我们必须依靠在aws帐户上使用lambda函数的模拟/某些实现来运行图形.随着AWS CDK的发布,是否可以用高级语言定义步进函数图,并进行单元测试,模拟等?
Currently step functions graph are defined in json using ASL (Amazon States Language). There is no good way to to unit testing of the graphs being created and we have to rely on running the graph with mocked/some implementation of lambda functions on aws account. With launch of AWS CDK, is it possible to have the step function graphs also defined in high level language and do unit testing, mocking etc?
推荐答案
您是否已给 @ aws-cdk/aws-stepfunctions
库可以尝试吗?
Have you given the @aws-cdk/aws-stepfunctions
library a try?
为了后代,从此处的文档中复制示例代码:
Copying the example code from the documentation here for posterity:
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
});
这篇关于有没有一种方法可以使用CDK创建步骤函数图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!