-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
integ.invoke.ts
78 lines (68 loc) · 2.5 KB
/
integ.invoke.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as cdk from '@aws-cdk/core';
import { LambdaInvoke } from '../../lib';
/*
* Creates a state machine with a task state to invoke a Lambda function
* The state machine creates a couple of Lambdas that pass results forward
* and into a Choice state that validates the output.
*
* Stack verification steps:
* The generated State Machine can be executed from the CLI (or Step Functions console)
* and runs with an execution status of `Succeeded`.
*
* -- aws stepfunctions start-execution --state-machine-arn <state-machine-arn-from-output> provides execution arn
* -- aws stepfunctions describe-execution --execution-arn <state-machine-arn-from-output> returns a status of `Succeeded`
*/
const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-stepfunctions-tasks-lambda-invoke-integ');
const submitJobLambda = new Function(stack, 'submitJobLambda', {
code: Code.fromInline(`exports.handler = async () => {
return {
statusCode: '200',
body: 'hello, world!'
};
};`),
runtime: Runtime.NODEJS_14_X,
handler: 'index.handler',
});
const submitJob = new LambdaInvoke(stack, 'Invoke Handler', {
lambdaFunction: submitJobLambda,
outputPath: '$.Payload',
});
const checkJobStateLambda = new Function(stack, 'checkJobStateLambda', {
code: Code.fromInline(`exports.handler = async function(event, context) {
return {
status: event.statusCode === '200' ? 'SUCCEEDED' : 'FAILED'
};
};`),
runtime: Runtime.NODEJS_14_X,
handler: 'index.handler',
});
const checkJobState = new LambdaInvoke(stack, 'Check the job state', {
lambdaFunction: checkJobStateLambda,
resultSelector: {
status: sfn.JsonPath.stringAt('$.Payload.status'),
},
});
const isComplete = new sfn.Choice(stack, 'Job Complete?');
const jobFailed = new sfn.Fail(stack, 'Job Failed', {
cause: 'Job Failed',
error: 'Received a status that was not 200',
});
const finalStatus = new sfn.Pass(stack, 'Final step');
const chain = sfn.Chain.start(submitJob)
.next(checkJobState)
.next(
isComplete
.when(sfn.Condition.stringEquals('$.status', 'FAILED'), jobFailed)
.when(sfn.Condition.stringEquals('$.status', 'SUCCEEDED'), finalStatus),
);
const sm = new sfn.StateMachine(stack, 'StateMachine', {
definition: chain,
timeout: cdk.Duration.seconds(30),
});
new cdk.CfnOutput(stack, 'stateMachineArn', {
value: sm.stateMachineArn,
});
app.synth();