Open In App

A Complete Guide For Using The AWS Step Functions

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

AWS Step functions allow the management of distributed workflows. It allows us to generate and manage serverless workflows as a series of steps. Each step runs as an event-driven workflow. A step is also called a state, which has specific input and generates output. Let’s see how we can use AWS step functions for running serverless workflows.

Primary Terminologies of AWS Step Functions

State Machine: It is a flow that defines a series of steps and the flow of their execution.

State: Each step in a state machine is called a state.

Amazon State Language: Language used for defining state machine configuration.

How to utilize the AWS Step function for serverless workflows

Let’s see how to build serverless workflows using the step function. We will build a simple workflow for GFG articles.

The state machine will contain a prototype design that will change the article status depending on the previous status.

Step-function dashboard

  • Before starting, first log in to the AWS console. On the console, go to search and open the AWS step functions. You should see a similar page below.

AWS Console Step Function

  • Click on Get started.

AWS step functions

  • select Create Your own from options.

Create Prototype for workflow:

On Prototype design page follow below steps to create prototype for our workflow.

  • First from left navigation drag and drop Lambda Invoke between start and end.

Prototype design

  • In left pane give name to this state as “GeeksForGeeks Article Description”. This state will take article description as a input and pass it to next state.
  • Now similarly drag and drop Choice state from flow in left pane. There will be two rules namely Rule 1 and default. from left pane under rules section click on add new choice rule. This will add another rule.
  • Now for rule 1 and rule 2 add AWS lambda states as next state and name them as “Add To Review” and “Move To Publish” respectively.
  • Click on default rule and under next state specify as Add To Review. Finally, the flow should look like below.

Prototype

  • Now add one more lambda state under Add To Review and name it as “Show Article”. Now modify first two states where next step will be “Show Article”. Finally, the state machine will look like below.

Prototype design

  • Now in choices click on Rule 1 and in left pane under conditions click on edit icon. Specify condition as below which will check if the article status is equal to pending or not. Click save condition after completion.

Conditions

  • Similarly add condition for Rule 2. For rule 2 value of comparison is “Review”.
  • Now we are done with prototype design.

Do not close this tab go to another tab for next step.

Add lambda functions to states:

Now let’s create lambda functions to invoke and add them to states.

  • From navigation go to lambda functions and click on create function.
  • For first function specify name of function and leave everything as default and click on create.

Specify Name

  • On successful creation of function on details page go to code source. Add serverless logic for execution and click on deploy. Here we have logic as below.

Function Deployed

export const handler = async (event) => {
const {
status,
name
} = event;

const articleDescription = {
name : name,
status : status
};
return articleDescription;
};


  • Similarly create another function with name “MoveToPublish” and add following code.

MoveToPublish

export const handler = async (event) => {
const {
status,
name
} = event;

const updatedDescription = {
name : name,
status : "Publish"
};
return updatedDescription;
};


  • Now create another function with name “AddToReview” and add following code.

AddToReview

export const handler = async (event) => {
const {
status,
name
} = event;

const updatedDescription = {
name : name,
status : "Review"
};
return updatedDescription;
};


  • At last, create our last function “ShowArticle” and add below logic.

ShowArticle

export const handler = async (event) => {
const {
status,
name
} = event;

const Description = {
name : name,
status : "Review"
};
return Description;
};


  • Once all functions are created add the respective function to each state. Go to previous tab and click on first state. In left pane under API parameter select respective lambda function from function name dropdown.

API Parameter

  • Now add functions for other states also

This will complete our state machine. On verification click on create.

Executing Step function

On state machine overview page click on start execution. Add input for our execution as below.

Execution

  • Click on start Execution. New IAM role will be added for execution where click confirm and continue.
  • On successful execution scroll down to see Graph View and click on “Show Article” state to view the output.

Show Article

As we can see the flow of execution and the output where the status of article is changed to “Review” from “Pending”

  • Similarly create another execution with input status as “Review” you should see following flow of execution.

Review

Conclusion

Thus, we have run serverless workflows with the help of Step function in easy way. These workflows can be configured to run complex business logic and functions. AWS Step functions allow flexibility in building complex business workflows along with simplicity with help of UI. This can be explored further as per application requirements.

Utilizing AWS Step Functions for Serverless Workflows – FAQ’s

How does AWS Step Functions work with serverless architectures?

AWS Step Functions is designed to work seamlessly with serverless architectures. It allows you to coordinate and execute tasks across serverless functions (e.g., AWS Lambda) and other serverless services, creating efficient and scalable workflows.

How do I create a serverless workflow with AWS Step Functions?

You can create a serverless workflow using the AWS Step Functions visual workflow designer in the AWS Management Console or by defining state machines using the AWS SDKs. The visual designer allows you to drag and drop states to define the workflow graphically.

How does error handling work in AWS Step Functions?

AWS Step Functions provides built-in error handling mechanisms. You can define error-handling states to handle specific errors, set up retries for failed states, and configure catchers to handle errors at different levels of the state machine.

Is there a way to monitor and log AWS Step Functions executions?

Yes, AWS Step Functions integrates with Amazon CloudWatch for monitoring and logging. You can view execution logs, set up CloudWatch Alarms for specific events, and gain insights into the performance of your state machines.

How can I control costs when using AWS Step Functions for serverless workflows?

AWS Step Functions provides cost control features, allowing you to set resource limits for state machine executions, control concurrency, and specify timeouts. This helps in optimizing costs associated with workflow executions.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads