Table of Contents
Install and configure packages
1. Install and Configure the AWS CLI by following the steps mentioned in the below link
2. Install NodeJS by the following the instructions in the following link
3. Install the AWS CDK by running the following command
1 2 3 |
npm install -g aws-cdk # to validate if the CDK has been installed correctly cdk --version |
4. Running the following commands to create a directory “aws-cdk-step-functions”, initialize the app “step-functions”, and install the required npm packages
1 2 3 4 5 |
mkdir aws-cdk-step-functions && cd aws-cdk-step-functions cdk init step-functions --language typescript npm install @aws-cdk/aws-lambda npm install @aws-cdk/aws-stepfunctions npm install @aws-cdk/aws-stepfunctions-tasks |
Setup Lambdas
5. Create a folder lambda in the root of your project
1 |
mkdir lambda && cd lambda |
6. Create the required lambdas (using your favorite IDE)
a. generateRandomNumber.js – to generate a random number in the given range
1 2 3 4 5 6 7 8 |
exports.handler = (event, context, callback) => { const generateRandom = (maxNumber) => Math.floor(Math.random() * maxNumber) + 1; callback(null, { "generatedRandomNumber": generateRandom(event.maxNumber), "maxNumber": parseInt(event.maxNumber), "numberToCheck": parseInt(event.numberToCheck) }); } |
b. greater.js – to send the message “greater”
1 2 3 |
exports.handler = (event, context, callback) => { callback(null, {"msg": "greater"}); }; |
c. lessOrEqual.js – to send message “lessOrEqual”
1 2 3 |
exports.handler = (event, context, callback) => { callback(null, {"msg": "lessOrEqual"}); }; |
7. Open the file bin/step-functions.ts and uncomment the following line
1 |
env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }, |
8. Now open the file lib/step-functions.ts and modify it so that it looks something like this
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 79 |
import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as lambda from 'aws-cdk-lib/aws-lambda'; import * as sfn from 'aws-cdk-lib/aws-stepfunctions'; import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks'; export class StepFunctionsStack extends cdk.Stack { public Machine: sfn.StateMachine constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); //Lambda to generate a random number const generateRandomNumber = new lambda.Function(this, 'GenerateRandomNUmber', { runtime: lambda.Runtime.NODEJS_18_X, code: lambda.Code.fromAsset('lambda'), handler: 'generateRandomNumber.handler', timeout: cdk.Duration.seconds(3) } ); //Lambda invocation for generating a random number const generateRandomNumberInvocation = new tasks.LambdaInvoke(this, 'Generate random number invocation', { lambdaFunction: generateRandomNumber, outputPath: '$.Payload', }); //Lambda function called if the generted number is greater than the expected number const functionGreaterThan = new lambda.Function(this, "NumberGreaterThan", { runtime: lambda.Runtime.NODEJS_18_X, code: lambda.Code.fromAsset('lambda'), handler: 'greater.handler', timeout: cdk.Duration.seconds(3) }); // Lambda invocation if the generated number is greater than the expected number const greaterThanInvocation = new tasks.LambdaInvoke(this, 'Get Number is greater than invocation', { lambdaFunction: functionGreaterThan, inputPath: '$', outputPath: '$', }); // Lambda function called if the generated number is less than or equal to the expected number const functionLessThanOrEqual = new lambda.Function(this, "NumberLessThan", { runtime: lambda.Runtime.NODEJS_14_X, code: lambda.Code.fromAsset('lambda'), handler: 'lessOrEqual.handler', timeout: cdk.Duration.seconds(3) }); // Lambda invocation if the generated number is less than or equal to the expected number const lessThanOrEqualInvocation = new tasks.LambdaInvoke(this, 'Get Number is less than or equal invocation', { lambdaFunction: functionLessThanOrEqual, inputPath: '$', outputPath: '$', }); //condition to wait 1 second const wait1Second = new sfn.Wait(this, "Wait 1 Second", { time: sfn.WaitTime.duration(cdk.Duration.seconds(1)), }); //Choice condition to workflow const numberChoice = new sfn.Choice(this, 'Job Complete?') .when(sfn.Condition.numberGreaterThanJsonPath('$.generatedRandomNumber', '$.numberToCheck'), greaterThanInvocation) .when(sfn.Condition.numberLessThanEqualsJsonPath('$.generatedRandomNumber', '$.numberToCheck'), lessThanOrEqualInvocation) .otherwise(lessThanOrEqualInvocation) //Create the workflow definition const definition = generateRandomNumberInvocation .next(wait1Second) .next(numberChoice); //create the statemachine this.Machine = new sfn.StateMachine(this, "StateMachine", { definition, stateMachineName: "randomNumberStateMachine", timeout: cdk.Duration.minutes(5), }); } } |
Deploy and Test
9. Bootstrap the CDK
1 |
cdk bootstrap |
10. Synthesize and Deploy the CDK by running the following commands
1 2 |
cdk synth cdk deploy |
11. Test the functionality by login into AWS Console > Step functions > randomNumberStateMachine > Start Execution, using the following payload
1 2 3 4 |
{ "maxNumber": 10, "numberToCheck": 7 } |
12. To delete all generated AWS resources, run the following command
1 |
cdk destroy |