Introduction: The Need for Multi-Step Workflows

Welcome back! In the last two lessons, you learned how to build and deploy simple serverless APIs using AWS Lambda and API Gateway. You saw how a single Lambda function can handle a request and return a response.

However, many real-world applications require more than just a single step. For example, when processing an online order, you might need to:

  • Validate the order details
  • Charge the customer
  • Record the order in a database

Each of these steps could be handled by a separate Lambda function. But how do you make sure they run in the right order and that data flows smoothly from one step to the next? This is where AWS Step Functions come in. Step Functions let you organize and manage multi-step workflows in your serverless applications.

Quick Recall: Lambda Functions in Serverless Apps

Before we dive into Step Functions, let's quickly remind ourselves how Lambda functions fit into serverless applications.

A Lambda function is a small piece of code that runs in response to an event, such as an API call or a message. In previous lessons, you learned how to:

  • Write a Lambda function that processes input and returns output
  • Connect a Lambda function to an API Gateway endpoint using a SAM template

In this lesson, we will see how to connect several Lambda functions together to handle a more complex process.

Understanding AWS Step Functions

AWS Step Functions help you coordinate multiple Lambda functions (or other AWS services) into a single workflow, called a state machine.

A state machine is like a flowchart: it defines a series of steps (called states) and the order in which they run. Each state can perform a task, make a choice, or wait for something to happen.

Let's use a real-world example: processing an online order. Imagine you have three steps:

  1. Validate the order (check if the amount is valid)
  2. Charge the customer (simulate payment)
  3. Record the order (save it to a database)

With Step Functions, you can define a workflow that runs these steps one after another, passing the result from one step to the next.

Breaking Down the Order Workflow Example

Let's build up the order processing workflow step by step. We will use three Lambda functions: one for each step in the process.

Step 1: Validate the Order

First, we need a Lambda function to check if the order amount is valid.

  • This function receives an event with an order object.
  • It checks if the amount is greater than 0. If not, it raises an error.
  • If the amount is valid, it adds a "validated": True field to the order and returns it.

Example input:

Example output:

Step 2: Charge the Customer

Next, we need a Lambda function to simulate charging the customer.

  • This function takes the output from the previous step.
  • It creates a payment_id based on the order ID and adds a charged flag.
  • It returns the updated event.

Example input:

Example output:

Step 3: Record the Order

Finally, we need a Lambda function to save the order to a DynamoDB table.

  • This function connects to a DynamoDB table (the table name is set in the environment).
  • It creates an item with the order ID, amount, payment ID, and a timestamp.
  • It saves the item to the table and returns a status.

Example input:

Example output:

Defining the Workflow in SAM

Now, let's see how these functions are connected using AWS Step Functions in the template.yaml file.

Here is the relevant part of the template:

  • The state machine starts at the Validate step.
  • Each step (Validate, Charge, Record) runs a Lambda function.
  • The output of each step is passed as input to the next step.
  • The workflow ends after the Record step.

This setup ensures that the order is validated, charged, and recorded in sequence.

Deploying and Testing the Workflow

To deploy and test this workflow, you use the AWS SAM CLI. Here's a high-level overview of the process:

  1. Build the application:
  2. Deploy the application:
  3. Start a workflow execution:
    You can trigger the state machine with a sample order:

Expected output:
If everything works, the workflow will process the order through all three steps. The final output will look like this:

This shows that the order was validated, charged, and recorded successfully.

Summary and Practice Preview

In this lesson, you learned how AWS Step Functions help you organize and run multi-step workflows in your serverless applications. You saw how to:

  • Break a process into separate Lambda functions
  • Connect those functions in a state machine using Step Functions
  • Deploy and test the workflow using AWS SAM

Next, you'll get hands-on practice building and running your own Step Functions workflows. This will help you reinforce what you've learned and prepare you for more advanced serverless patterns. Good luck!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal