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.
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
Lambdafunction that processes input and returns output - Connect a
Lambdafunction to an API Gateway endpoint using aSAMtemplate
In this lesson, we will see how to connect several Lambda functions together to handle a more complex process.
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:
- Validate the order (check if the amount is valid)
- Charge the customer (simulate payment)
- 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.
Let's build up the order processing workflow step by step. We will use three Lambda functions: one for each step in the process.
First, we need a Lambda function to check if the order amount is valid.
- This function receives an
eventwith anorderobject. - It checks if the
amountis greater than 0. If not, it raises an error. - If the amount is valid, it adds a
"validated": Truefield to the order and returns it.
Example input:
Example output:
Next, we need a Lambda function to simulate charging the customer.
- This function takes the output from the previous step.
- It creates a
payment_idbased on the order ID and adds achargedflag. - It returns the updated event.
Example input:
Example output:
Finally, we need a Lambda function to save the order to a DynamoDB table.
- This function connects to a
DynamoDBtable (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:
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
Validatestep. - Each step (
Validate,Charge,Record) runs aLambdafunction. - The output of each step is passed as input to the next step.
- The workflow ends after the
Recordstep.
This setup ensures that the order is validated, charged, and recorded in sequence.
To deploy and test this workflow, you use the AWS SAM CLI. Here's a high-level overview of the process:
- Build the application:
- Deploy the application:
- 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.
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
Lambdafunctions - 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!
