How to build a Serverless Workflow with AWS Step Functions?

Reading Time: 8 minutes

In our previous blog How to build a Serverless Workflow with AWS Step Functions? (Part 1), we have discussed AWS Step functions, its features, benefits, use cases, etc. In this blog, we will discuss a business scenario to configure and create a serverless workflow to handle the issue resolution statuses raised by each of the customers and follow a systematic process for the same.

Business Scenario:

Let’s say a company has a department of customer service that handles all the service requests raised by the customers. It’s getting difficult to handle the record of each issue raised by each of the customers. You are a developer at the same company and you have been addressed about this issue to be resolved as soon as possible. A senior in your team asks you to create a serverless application to automate the handling of the issue statuses raised by the customers. While you could have one Lambda function call the other, you worry that managing all of those connections will become challenging as the application becomes more sophisticated. Apart from this, if there are some same changes required in the application flow in multiple places, you could end up writing the same code over and over again. 

So, to solve this issue, you decide to use AWS Step Functions. 

To implement this, we will do the following:

  • Sign in to your AWS console and navigate to the Step Function dashboard.
  • Create a new Step function state machine.
  • Create a JSON-formatted workflow execution for the step function.
  • Navigate to the IAM console and attach a policy to the created IAM Step Function role to perform the execution of the Lambda functions.
  • Navigate to the AWS Lambda console.
  • Create 5 Lambda functions for the different types of states with the specific codes in them.
  • Navigate back to the Step Function console, edit the state machine, and attach each Lambda function ARN to the respective states in the JSON-formatted Step Function code.
  • Save the state machine code and create a new execution.
  • Check the workflow in the Graph inspector on the AWS Step Function dashboard.
  • Check the execution details.
  • Create multiple executions to see the different types of execution and follow the same steps as above to check the execution.

Sign in to your AWS account and navigate to the AWS Step Functions dashboard. In the left navigation pane, click on State Machines.

How to build a Serverless Workflow with AWS Step Functions?

On the dashboard click on Create state machine.

Select Author with code snippets and under Type select Standard.

How to build a Serverless Workflow with AWS Step Functions?

Scroll down and under the definition, make sure to add the code in the snippet below.

How to build a Serverless Workflow with AWS Step Functions?

Once done, hit refresh on the state machine workflow diagram on the other half of the screen as shown below.

Once done, you will see the state machine workflow based on the JSON code you have added in the code pane. Now click on Next.

How to build a Serverless Workflow with AWS Step Functions?

Specify a name for your state machine and under permissions, select Create a new role.

Scroll down to the bottom of the page and click on Create state machine.

How to build a Serverless Workflow with AWS Step Functions?

On success, you will see the message on top as shown below. Now click on the link below the IAM role ARN to navigate to the IAM console and attach a policy for Step Function to execute your Lambda function.

On the IAM role dashboard, click on Attach policies.

Search for AWSLambdaRole and select the checkbox for that role and once done click on Attach policy.

How to build a Serverless Workflow with AWS Step Functions?

On success, you will see the new policy attached for the same. Now navigate to the Lambda dashboard on the AWS console.

On the Lambda dashboard, click on Create function. You will have to create functions for all the cases mentioned in the state function workflow JSON code.

Select Author from scratch, a name for your function for the issue open case, and the runtime as Node.js 14.x.

How to build a Serverless Workflow with AWS Step Functions?

Under permissions, select Create a new role with basic lambda functions. Once done, click on Create function.

On success, you will see a message as shown below. Now scroll down to the editor pane of your function.

Add the code as shown in the below snippet for the Issue Open state.

Once done, hit deploy to deploy the function.

How to build a Serverless Workflow with AWS Step Functions?

On success, you will see the message as shown below. Now scroll up to the top of the page.

Click on Functions to go back to the functions dashboard.

Now follow the above steps and create a new function for the Assign Issue state and add the code as shown in the snippet below for that function.

How to build a Serverless Workflow with AWS Step Functions?
How to build a Serverless Workflow with AWS Step Functions?

Follow the above steps and create a new function for the Work On Issue state and add the code as shown in the snippet below for that function.

Follow the above steps and create a new function for the Close Issue state and add the code as shown in the snippet below for that function.

Follow the above steps and create a new function for the Escalate Issue state and add the code as shown in the snippet below for that function.

How to build a Serverless Workflow with AWS Step Functions?

Once done, navigate back to the Step Function dashboard. Click on Edit.

On the resource, click on the sample ARN and you will see all the functions you created in the Lambda console.

Select the appropriate function from the dropdown for the appropriate case in the JSON. Once done, click on Save.

How to build a Serverless Workflow with AWS Step Functions?

A modal appears for the IAM role warning. Click on Save anyway.

On success, you will see the message as shown below. Now click on Start execution to create the first test case.

A modal appears as shown below. You need to give input in the JSON format and click on Start execution.

How to build a Serverless Workflow with AWS Step Functions?

Enter a name for the execution and add the JSON code as shown in the below snippet and click on Start execution.

You will see the Execution status as Running. After a few seconds, it will give you the final result.

Scroll down to the graph inspector and you will get to see the execution workflow. The ones in green are the execution steps followed.

Now, on execution completion, scroll up and select the Execution Input tab. You will see the input passed.

In the Execution output tab, you will see the message of the workflow state machine followed for the execution.

How to build a Serverless Workflow with AWS Step Functions?

If you scroll down to the Graph Inspector section, to see the input and output at each step, click on a case in the workflow state machine. You will get to see the input and the message of the current status in that state.

Similarly, if you select the step output tab, you will get to see the output at that state.

To see the execution details, scroll to the bottom and expand any state to see the input and output for the same.

Now navigate back to the top and create a new execution. Enter a name and add the input with an ID like 2. Click on Start execution.

You will see that the execution is in process in the Graph inspector.

How to build a Serverless Workflow with AWS Step Functions?

This time for the second execution, the execution failed. You can see the same in the Graph inspector in the below image. That means that the issue did not get resolved by the customer care executive and hence, has been escalated.

You can select the Execution state to see the Step input and Step output at that state for more details.

The Step output provides the details as shown below.

How to build a Serverless Workflow with AWS Step Functions?

Conclusion

In this blog, you have seen how we can make use of AWS Step Functions to create and configure a serverless workflow for implementing a business scenario to handle the tracking of the resolution of issues raised by the customers. We will discuss more AWS Step Functions and their flexibility to integrate with the other AWS services in our upcoming blog. Stay tuned to keep getting all updates about our upcoming new blogs on AWS and relevant technologies.

Meanwhile …

Keep Exploring -> Keep Learning -> Keep Mastering

This blog is part of our effort towards building a knowledgeable and kick-ass tech community. At Workfall, we strive to provide the best tech and pay opportunities to AWS-certified talents. If you’re looking to work with global clients, build kick-ass products while making big bucks doing so, give it a shot at workfall.com/partner today.

Back To Top