Create a Serverless Workflow
with AWS Step Functions and AWS Lambda
In this tutorial, you will learn how to use AWS Step Functions to design and run a serverless workflow that coordinates multiple AWS Lambda functions. AWS Lambda is a compute service that lets you run code without provisioning or managing servers.
In our example, you are a developer who has been asked to create a serverless application to automate handling of support tickets in a call center. While you could have one Lambda function call the other, you worry that managing all of those connections will become challenging as the call center application becomes more sophisticated. Plus, any change in the flow of the application will require changes in multiple places, and you could end up writing the same code over and over again.
To solve this challenge, you decide to use AWS Step Functions. Step Functions is a serverless orchestration service that lets you easily coordinate multiple Lambda functions into flexible workflows that are easy to debug and easy to change. Step Functions will keep your Lambda functions free of additional logic by triggering and tracking each step of your application for you.
In the next 10 minutes, you will create a Step Functions state machine to describe the current call center process, create a few simple Lambda functions that simulate the tasks of the support team, and pass data between each Lambda function to track the progress of the support case. Then, you’ll perform several tests of your workflow to observe how it responds to different inputs. Finally, you’ll delete the AWS resources you used in the tutorial.
You'll use AWS Step Functions and AWS Lambda in this tutorial. These services are within the AWS Free Tier.
This tutorial requires an AWS account
There are no additional charge for Step Functions or Lambda. The resources you create in this tutorial are Free Tier eligible.
Step 1. Create a State Machine
Your first step is to design a workflow that describes how you want support tickets to be handled in your call center. Workflows describe a process as a series of discrete tasks that can be repeated again and again.
You are able to sit down with the call center manager to talk through best practices for handling support cases. Using the visual workflows in Step Functions as an intuitive reference, you define the workflow together.
a. Open the AWS Management Console, so you can keep this step-by-step guide open. When the screen loads, enter your user name and password to get started. Then type Step in the search bar and select Step Functions to open the service console.
Step 2. Create an AWS Identity and Access Management (IAM) Role
AWS IAM is a web service that helps you securely control access to AWS resources. In this step, you will create an IAM role that allows Step Functions to access Lambda.
a. In another browser window, open the AWS Management Console. When the screen loads, type IAM in the search bar, then select IAM to open the service console.
Step 3. Design a Serverless Workflow
Next, you’ll design your workflow in AWS Step Functions. Your workflow will call one AWS Lambda function to create a support case, invoke another function to assign the case to a support representative for resolution, and so on. It will also pass data between Lambda functions to track the status of the support case as it is being worked on.
b. Replace the contents of the State machine definition window with the Amazon States Language (ASL) state machine definition below. Amazon States Language is a JSON-based, structured language used to define your state machine.
This state machine uses a series of Task states to open, assign and work on a support case. Then, a Choice state is used to determine if the case can be closed or not. Two more Task states then close or escalate the support case as appropriate.
{
"Comment": "A simple AWS Step Functions state machine that automates a call center support session.",
"StartAt": "Open Case",
"States": {
"Open Case": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "Assign Case"
},
"Assign Case": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "Work on Case"
},
"Work on Case": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "Is Case Resolved"
},
"Is Case Resolved": {
"Type" : "Choice",
"Choices": [
{
"Variable": "$.Status",
"NumericEquals": 1,
"Next": "Close Case"
},
{
"Variable": "$.Status",
"NumericEquals": 0,
"Next": "Escalate Case"
}
]
},
"Close Case": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
},
"Escalate Case": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "Fail"
},
"Fail": {
"Type": "Fail",
"Cause": "Engage Tier 2 Support." }
}
}
c. Click the refresh button to have Step Functions translate the ASL state machine definition into a visual workflow. In our scenario, you are able to easily verify that the process is described correctly by reviewing the visual workflow with the call center manager. Click Create state machine. A green message will appear at the top of the screen confirming that your state machine was created successfully.
Step 4. Create your AWS Lambda Functions
Now that you’ve created your state machine, you can decide how you want it to perform work. You can connect your state machine to AWS Lambda functions and other microservices that already exist in your environment, or create new ones. In this tutorial, you’ll create a few simple Lambda functions that simulate various steps for handling support calls, such as assigning the case to a Customer Support Representative.
c. Leave Author from scratch selected. Next, configure your first Lambda function as follows:
For Name, type OpenCaseFunction.
For Runtime, choose Node.js 4.3.
For Role, select Create custom role.
A new IAM window will open. Leave the Role name as lambda_basic_execution and click Allow.
You will automatically be returned back to the Lambda console. Click Create function.
d. Replace the contents of the Function code window with the following code, and then click Save.
exports.handler = (event, context, callback) => {
// Create a support case using the input as the case ID, then return a confirmation message
var myCaseID = event.inputCaseID;
var myMessage = "Case " + myCaseID + ": opened...";
var result = {Case: myCaseID, Message: myMessage};
callback(null, result);
};
f. Repeat steps 4b-4d to create 4 more Lambda functions, using the lambda_basic_execution IAM role you created in step 4c.
Define AssignCaseFunction as:
exports.handler = (event, context, callback) => {
// Assign the support case and update the status message
var myCaseID = event.Case;
var myMessage = event.Message + "assigned...";
var result = {Case: myCaseID, Message: myMessage};
callback(null, result);
};
Define WorkOnCaseFunction as:
exports.handler = (event, context, callback) => {
// Generate a random number to determine whether the support case has been resolved, then return that value along with the updated message.
var min = 0;
var max = 1;
var myCaseStatus = Math.floor(Math.random() * (max - min + 1)) + min;
var myCaseID = event.Case;
var myMessage = event.Message;
if (myCaseStatus == 1) {
// Support case has been resolved
myMessage = myMessage + "resolved...";
} else if (myCaseStatus == 0) {
// Support case is still open
myMessage = myMessage + "unresolved...";
}
var result = {Case: myCaseID, Status : myCaseStatus, Message: myMessage};
callback(null, result);
};
Define CloseCaseFunction as:
exports.handler = (event, context, callback) => {
// Close the support case
var myCaseStatus = event.Status;
var myCaseID = event.Case;
var myMessage = event.Message + "closed.";
var result = {Case: myCaseID, Status : myCaseStatus, Message: myMessage};
callback(null, result);
};
Define EscalateCaseFunction as:
exports.handler = (event, context, callback) => {
// Escalate the support case
var myCaseID = event.Case;
var myCaseStatus = event.Status;
var myMessage = event.Message + "escalating.";
var result = {Case: myCaseID, Status : myCaseStatus, Message: myMessage};
callback(null, result);
};
When complete, you should have 5 Lambda functions.
Step 5. Populate your Workflow
The next step is to populate the task states in your Step Functions workflow with the Lambda functions you just created.
c. In the State machine definition section, find the line under the “Open Case” state which begins with “Resource” and replace the ARN with the ARN of your OpenCaseFunction. If you click on the sample ARN a list of the AWS Lambda functions in your account will appear and you can select it from the list.
Step 6. Execute your Workflow
Your serverless workflow is ready to be executed! A state machine execution is an instance of your workflow, and occurs each time a Step Functions state machine runs and performs its tasks. Each Step Functions state machine can have multiple simultaneous executions, which you can initiate from the Step Functions console (which is what you’ll do next), or using the AWS SDKs, the Step Functions API actions, or the AWS CLI. An execution receives JSON input and produces JSON output.
d. Step Functions lets you inspect each step of your workflow execution, including the inputs and outputs of each state. Click on each task in your workflow and expand the Input and Output fields under Step details. You can see that the case ID you inputted into your state machine is passed from each step to the next, and that the messages are updated as each Lambda function completes its work.
f. Depending on the output of your WorkOnCaseFunction, your workflow may have ended by resolving the support case and closing the ticket, or escalating the ticket to the next tier of support. You can re-run the execution a few more times to observe this different behavior. This image shows an execution of the workflow where the support case was escalated, causing the workflow to exit with a Fail state.
In a real world scenario, you might decide to continue working on the case until it is resolved instead of failing out of your workflow. To do that, you could remove the Fail state and edit the Escalate Case Task in your state machine to loop back to the Work On Case state. No changes to your Lambda functions would be required. The functions we built for this tutorial are samples only, so we’ll move on to the next step of the tutorial.
Step 7. Terminate your Resources
In this step you will terminate your AWS Step Functions and AWS Lambda related resources.
Important: Terminating resources that are not actively being used reduces costs and is a best practice. Not terminating your resources can result in a charge.
b. In the State machines window, click on your CallCenterStateMachine and select Delete. Confirm the action by selecting Delete state machine in the dialog box. Your state machine will be deleted in a minute or two once Step Functions has confirmed that any in process executions have completed.
Congratulations!
Nice work! You just created a serverless workflow using AWS Step Functions that triggers multiple AWS Lambda functions. Your workflow coordinated all of your functions for you according to the logic you defined, and passed data from one state to another, which meant you didn’t need to write that code into each individual function.
Now that you have learned to design and run a serverless workflow, you can progress to the next tutorial where you will learn how to use Step Functions to handle Lambda function errors. You’ll create a state machine with Retry and Catch fields that respond to response codes from a mock API based on error message type, a method called function error handling.