Есть ли способ создать график пошаговых функций с использованием CDK? - PullRequest
0 голосов
/ 03 декабря 2018

В настоящее время граф пошаговых функций определяется в json с использованием ASL (Amazon States Language).Нет хорошего способа модульного тестирования создаваемых графов, и мы должны полагаться на запуск графа с ложной / некоторой реализацией лямбда-функций на аккаунте aws.С запуском AWS CDK можно ли определять графики пошаговых функций также на языке высокого уровня и выполнять модульное тестирование, макетирование и т. Д.?

1 Ответ

0 голосов
/ 04 декабря 2018

Вы уже попробовали библиотеку @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
});
...