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. 

More about the Free Tier >>


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.

01a
01a

( click to enlarge )


b.  Click Create state machine.

01b
01b

( click to enlarge )


c. Select Author from scratch, then name your state machine CallCenterStateMachine and select I will provide an IAM role ARN.

01c
01c

( click to enlarge )


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.

02a
02a

( click to enlarge )


b. Click Roles and then click Create Role.

02b
02b

( click to enlarge )


c. On the Create Roles screen, leave AWS Service selected, select Step Functions then click Next: Permissions. On the next screen, click Next: Review.

02c
02c

( click to enlarge )


d. Enter Role name as step_functions_basic_execution and choose Create role. On the next screen, click Next: Review.

02d
02d

( click to enlarge )


e. Your role is created and appears in the list. Select the name of your role to view it. 

02e
02e

( click to enlarge )


f. Copy the Role ARN on the next screen.

02f
02f

( click to enlarge )


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.


a. Back in the Step Functions console, paste the ARN you just copied into IAM role ARN.

03a
03a

( click to enlarge )


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."    }   
  }
}
03b
03b

( click to enlarge )


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.

03c
03c

( click to enlarge )


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.


a. Click on Services, type Lambda in the search bar, then select Lambda to open the service console.

04a
04a

( click to enlarge )


b. Choose Create function.

04b
04b

( click to enlarge )


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.

04c
04c

( click to enlarge )


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);    
};
04d
04d

( click to enlarge )


e. At the top of the page, click Functions.

04e
04e

( click to enlarge )


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.

04f
04f

( click to enlarge )


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.


a. Click on Services, type Step in the search bar, then select Step Functions to open the service console.

05a
05a

( click to enlarge )


b. On the State machines screen, select your CallCenterStateMachine and click Edit.

05b
05b

( click to enlarge )


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.

05c
05c

( click to enlarge )


d. Repeat the previous step to update the Lambda function ARNs for the Assign Case, Work on Case, Close Case, and Escalate Case Task states in your state machine, then click Save.

05d
05d

( click to enlarge )


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.


a. Click on Start execution.

06a
06a

( click to enlarge )


b. A new execution dialog box appears, where you can enter input for your state machine. To supply an ID for your support case, enter the content below in the New execution window in the Input window, then choose Start execution.

{
"inputCaseID": "001"
}
06b
06b

( click to enlarge )


c. As your workflow executes, each step will change color in the Visual workflow pane. Wait a few seconds for execution to complete. Then, in the Execution details pane, click Input and Output to view the inputs and results of your workflow.

06c
06c

( click to enlarge )


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.

06d
06d

( click to enlarge )


e. Scroll down to the Execution event history section. Click through each step of execution to see how Step Functions called your Lambda functions and passed data between functions.

06e
06e

( click to enlarge )


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.

06f
06f

( click to enlarge )


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.


a. At the top of the AWS Step Functions console window, click on State machines.

07a
07a

( click to enlarge )


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.

07b
07b

( click to enlarge )


c. Next, you’ll delete your Lambda functions. Click Services in the AWS Management Console menu, then select Lambda.

07c
07c

( click to enlarge )


d. In the Functions screen, click each of the functions you created for this tutorial and then select Actions and then Delete. Confirm the deletion by clicking Delete again.

07d
07d

( click to enlarge )


e. Lastly, you’ll delete your IAM roles. Click Services in the AWS Management Console menu, then select IAM.

07e
07e

( click to enlarge )


f. Select both of the IAM roles that you created for this tutorial, then click Delete role. Confirm the delete by clicking Yes, Delete on the dialog box.


You can now sign out of the AWS Management console.

07f
07f

( click to enlarge )


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.