AWS StepFunction Using CDK

AWS StepFunction Using CDK

AWS Step Function service is used to orchestrate a workflow consisting of serverless Functions and integrating with other AWS services to bundle applications.

In this blog post, we will look at how to orchestrate lambda functions to State Machine using AWS Step Functions.

Create a new directory on your system.

mkdir cdk-step && cd cdk-step

We will use CDK init to create a new Javascript CDK project:

cdk init --language javascript

The CDK init command creates a number of files and folders inside the cdk-step directory to help us organize the source code for your AWS CDK app.

We can list the stacks in our app by running the below command. It will show CdkStepStack.

_$ cdk ls
CdkStepStack

Let us install AWS lambda, AWS step functions and AWS step-functions task dependencies

npm install @aws-cdk/aws-lambda @aws-cdk/aws-stepfunctions @aws-cdk/aws-stepfunctions-tasks

Create an AWS Lambda Function Hello-Function with inline code:

const helloFunction = new lambda.Function(this, "MyLambdaFunction", {
  functionName: "Hello-Function",
  code: lambda.Code.fromInline(`
          exports.handler = (event, context, callback) => {
              callback(null, "Hello World!");
          };
      `),
  runtime: lambda.Runtime.NODEJS_12_X,
  handler: "index.handler",
  timeout: cdk.Duration.seconds(25),
});
  • The functionName is the name of our lambda function.
  • The code is where we put the inline code for our lambda function.
  • Nodejs 12.x is used as a runtime.
  • The handler will create a file index.js and exports the handler method.
  • Timeout duration will be 25 seconds.

Create the Step definitions:

const definition = new tasks.LambdaInvoke(this, "MyLambdaTask", {
  lambdaFunction: helloFunction,
}).next(new sfn.Succeed(this, "GreetedWorld"));
  • The task is to invoke Hello-Function lambda
  • The lambda just passes into the Succeed Step which is an inbuilt step
  • It returns output as GreetedWorld

Let us create a StateMachine with AWS Step Function.

const stateMachine = new sfn.StateMachine(this, "MyStateMachine", {
  stateMachineName: "Hello-World-Step",
  definition,
});

Before deploying the AWS resource, we can take a look at what resources will be getting created by using the below command.

cdk diff

NOTE: If we have multiple profiles set in our system, we need to tell CDK to look into the particular profile. This can be done, by adding the below key-value in cdk.json which was generated when we created a CDK project.

"profile": "<YOUR_PROFILE_NAME>"

Now, once we are ok with the resources which will be created, we can deploy them using the below command

cdk deploy

Let us open the AWS Lambda console. We can see that StateMachine, Step Function, and Lambda has been created.

StateMachine.png

step-Function.png

lambda-function.png

Now Go to the Step Function Hello-World-Step and click on Start execution button. We can see the execution details of the Step Function.

step-function-output.png

Conclusion

In this blog post, we saw how to create a StateMachine and Step Functions using AWS Cloud Development Kit. We also saw various commands related to CDK for initiating projects, deploying the resources to AWS. The associated Git Repository is here

Did you find this article valuable?

Support Rahul Lokurte’s Blog by becoming a sponsor. Any amount is appreciated!