AWS Step Functions with Workflow Studio finally there!
Introduction
In today's post, we will focus on AWS Step Functions, specifically AWS Step Functions Workflow Studio. We will start with some background information for people who have never worked with this AWS service, and then we will play with a practical example of Workflow Studio.
We will create a workflow that will send us an email reminder about some information we need to do this year.
But first, the theory.
The AWS Step Functions service has existed inside AWS since 2016. It's a serverless orchestration service, which helps developers working with their workflows by making possibility to model those workflows as state machines. The workflow design process is by using Amazon States Language.
Step Functions help a lot in the case of collaboration process with other team members and non-technical people. As you can see, it has a nice feature, which visualizes the whole workflow.
It has also other benefits like:
- Identifying and debugging errors is easier when whole steps are highlighted.
- It's better for long-running and complex workflows (when single Lambda has a limit of 15 minutes).
- AWS Step Functions automatically triggers and tracks each step, and retries when there are errors, so your application executes in order and as expected.
- With AWS Step Functions, you pay only for the transition from one step of your application workflow to the next, called a state transition (plus for the resources which you use).
We have two types of AWS Step Functions:
- Standard for long term running workflows.
- Express for the short term workflows (max 5 minutes, where for the standard we have 1 year).
Please keep in mind that for Express type there is no visualization feature available.
More information about the differences here.
General content
Since a couple of days, the AWS Step Functions Workflow Studio has been released. Finally, AWS created the visual workflow designer to create Step Functions. It's so amazing and was so highly demanded by users, so I am glad that this is now available to be used.
For the purpose of this blog post, let's create some AWS Step Functions workflow by using the visual workflow, and simply drag and drop what we need.
Our goal is to create a workflow that will send us an email reminder with some errand we need to do for the year. Given that AWS Step Functions Standard type can wait a whole year to be executed, I think it will be fun to try this out.
We will use the Amazon SES service to send the email, and we will use the AWS Lambda function to handle the sending. For the purpose of this article, I prepared a ready-made project (The full readme is available on Github) based on Serverless Framework that will handle this process. The implementation is in Ruby.
On our side, we will only need to deploy the above project, and combine a few elements in AWS Step Functions Workflow Studio.
Creating a state machine
So let's get started, log into AWS, go to AWS Step Functions, and start the fun by pressing Create state machine.
As you can see, there are two tabs on the left side in workflow studios:
- Action panel, where AWS service APIs are available.
- Flow panel, which are the flow states you can use in AWS Step Functions.
Items from both panels can be drag and dropped into the workflow.
Let's start by creating the first state flow, which is the Pass state. We will use it for the purpose of getting the input parameters, which are both the reminder title, and the reminder date. These are the only parameters we need.
For this drag and drop Pass state, and we need to set parameter transformations to pass them on.
Take into note that if you do not provide the required parameters, the execution will fail. Also, if you send more parameters, they will be omitted as we only allow two specific ones.
Let's now deal with the addition of Wait State. We need this state to wait for the next task to complete. From the parameters of the previous state, we have the reminder_date parameter. We will use it to set the time that must elapse to have a transition to the next state.
After waiting until the appropriate time, we will want to invoke our Lambda function which, based on the reminder_topic parameter, will send an email notification.
Finally, let's add basic error handling for our Lambda function. Any error will cause our workflow to end the execution of our state machine.
Now we just need to save our state machine, we can see the whole definition and diagram. Also, permissions will be created and we can add logging. At the very end, we have to name the state machine and save it, which will start the process of creating it.
After creating our state machine, let's test it.
Let's use as input parameters:
Note that reminder_date must be compatible with ISO8601. Here is a page where you can convert your dates. Remember about time zones.
Then wait until all the steps are done.
After waiting, we can see that our workflow was executed without any problems. We can see every step, all input and output parameters.
And at the very end we can check the email:
Summary
- We've learned the basics of Workflow Studio, and we were able to create a simple workflow for email reminders.
As you can see, it is very fast, efficient, and fun to use Workflow Studio! I highly encourage you to give it a try yourself, there are still many useful features to use, AWS services as well as different types of states.
Additionally, the workflow created for this blogpost is not a production solution.
There will be more blogposts on AWS Step Functions in the near future. Stay tuned.