AWS StepFunction Using CDK

I am a Lead Engineer from India. Love to blog about serverless and help teams design and develop serverless architecture. An AWS cloud practitioner. Blogs about AWS Services utilising AWS CDK, CloudFormation, Terraform.
Search for a command to run...

I am a Lead Engineer from India. Love to blog about serverless and help teams design and develop serverless architecture. An AWS cloud practitioner. Blogs about AWS Services utilising AWS CDK, CloudFormation, Terraform.
No comments yet. Be the first to comment.
This series will cover provisioning Aws resources using Cloud Development Kit.
Infrastructure as code has become a go-to process to automatically provision and manage cloud resources. AWS provides two options for infrastructure as code. AWS CloudFormation AWS Cloud Development Kit With CloudFormation, we have to write a lot o...
Transformers, the backbone of many state-of-the-art NLP models such as BERT, GPT has revolutionized the way we approach natural language understanding tasks. One key innovation in transformers is their ability to handle entire sequences of tokens sim...

When an User prompts the questions to the LLM, it produces the response based on the training data. The pre-trained LLM data is its parametric data. It might not have an context specific to our query. LLM still produces an answer confidently. It is t...

In the fast-evolving field of artificial intelligence, Large Language Models (LLMs) have rapidly transformed how we approach language processing. These models, such as ChatGPT, Claude, and Bard, have proven their utility across a range of application...

Amazon S3 is a powerful, scalable storage service, but when multiple clients or processes attempt to write to the same S3 object simultaneously, things can get tricky. Since S3 doesn’t natively handle concurrent writes or provide object locking, it’s...

When working with distributed systems in a microservice architecture, we must always architect our services with failure in mind. When the transaction happens between a microservice, we have to handle the failures. The Saga pattern can be used in a f...

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),
});
Create the Step definitions:
const definition = new tasks.LambdaInvoke(this, "MyLambdaTask", {
lambdaFunction: helloFunction,
}).next(new sfn.Succeed(this, "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.



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.

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