Check out Rosius's GitHub: https://github.com/trey-rosius/sam_stepfunctions
Hey there, welcome to the 3rd Scenario in “Building a Step Functions Workflow “
In the first part of this series, we built a Step Functions workflow for a simple apartment booking scenario using the AWS Step Functions low code visual editor.
In the second part of this series, we built the same workflow using CDK as IaC, AppSync and Python, while invoking the Step Functions execution from a Lambda function.
In this post, we’ll look at how to build the same workflow, using SAM as IaC, AppSync, and Python.
Prerequisite
- Install AWS Cli (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)
- Install AWS SAM CLI(https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
- AWS Account
- Any IDE of your choice. I use PyCharm
- Install Python 3.8
Assumption
In this post, we won’t be looking at SAM basics. So I’ll assume you’ve worked with SAM before.
If I’m wrong, I apologize. Please level up with these articles:
- https://phatrabbitapps.com/building-modern-serverless-apis-with-aws-dynamodb-lambda-and-api-gatewaypart-3
- https://github.com/aws/aws-sam-cli-app-templates
Problem Statement
What are we trying to solve?
So while building out a bigger system(Apartment Complex Management System), I encountered an interesting problem.
I’ll assume that most of us have reserved or booked either an apartment or hotel, or flight online.
For this scenario, let’s go with apartments. So when you reserve an apartment, here’s a breakdown in the simplest form of the series of steps that occur after that:
- The apartment is marked as reserved, probably with a status change. Let’s say the apartment status changes from vacant to reserved.
- This apartment is made unavailable for reserving by others for a particular period of time.
- The client is required to make payment within that period of time
- If payment isn’t made within that time, the reservation is canceled, and the apartment status changes back from reserved to vacant.
- If payment is made, then apartment status changes from reserved to occupied/paid
Building out this business logic using custom code is very possible but inefficient.
Why?
Because as developers, good ones for that matter, we always have to be on the lookout for tools that’ll help us carry out tasks in an efficient and scalable manner.
The series of steps outlined above serves as a good use case for AWS Step Functions.
- The sequence of a service interaction is important
- State has to be managed with AWS service calls
- Decision trees, retries, and error handling logic are required.
Solutions Architecture

Let me quarterback this entire architecture for you, please. Here’s what’s happening:
- A frontend application sends a mutation to AppSync.
- A Lambda resolver is invoked by AppSync based on that mutation.
- Lambda gets the input from the mutation and starts a Step Functions workflow based on the input.
We’ll use Flutter and Amplify to build out the front-end application in the next tutorial.
Create And Initialize a SAM Application
Open any Terminal/Command line interface, type in the command
And then follow the instructions as seen in these screenshots:

Choose Python 3.8 as your runtime environment

I gave the project name 'samWorkshopApp'

Once your application has been created, open it up in your IDE, and let’s proceed. For reference, I’m using Pycharm.
Activate your virtualenv like this on mac or linux machines:
'source .venv/bin/activate'
If you are a Windows platform, you would activate the virtualenv like this:
'.venv\\Scripts\\activate.bat'
Once the virtualenv is activated, you can install the required dependencies.
From the root directory of the project, install all dependencies in 'requirements.txt' by running the command 'pip install -r requirements.txt'
Initially, here’s how my folder structure looks like:

There are a couple of changes we are about to make:
- Inside the 'functions' directory, delete all folders, and then create a folder called lambda.
- Delete everything inside the 'statemachine' folder, then create a file inside that same folder called `booking_step_functions.asl.json.
This file would contain the state machine definition for our workflow. We visually defined this workflow in part 1 of this series. Copy the ASL(Amazon States Language) for the workflow below and paste it inside the file we’ve created above:
Now, my folder structure looks like this:

Create GraphQL API
Remember, we have to create a GraphQL API, attach a schema and a database, and connect a Lambda resolver to it. This Lambda would be responsible for invoking the Step Functions workflow.
Open up the 'template.yaml' and add this GraphQl API and API key to the resources section
We want to see a stream of logs in CloudWatch from AppSync, so let’s create and assign a CloudWatch role to the GraphQL API:
GraphQL Schema
A GraphQL API always works with a GraphQL schema. In the root directory, create a folder called ‘graphql’, and inside that folder, create a file called 'schema.graphql'. Type in the following ‘graphql’ schema into the file:
This schema has a single mutation 'addStepFunction' that sends an input('id' and 'arn') to a Lambda resolver. The Lambda resolver uses this input to start a Step Functions execution. Let’s define the schema in 'templates.yaml' under resources.
Create Lambda Function
Let’s create a lambda function that we’ll attach to a data source and then attach that data source to an AppSync resolver Inside the 'functions/lambda' folder, create a file called 'app.py' and type in the following code:
For now, this lambda function simply takes an input('id' and 'arn') and outputs('id' and 'arn'). Later on, we’ll use this lambda function to start the Step Functions workflow.
Let’s define the Lambda function in 'template.yaml' alongside its 'role':
Lambda Datasource
For this application, we’ll be using Lambda as our data source.
Inside 'template.yaml' add the following code below resources:
Since this data source would have to call AppSync, we attach an AppSync service role to it:
Create Direct Lambda Resolver
Now, we have to create a direct Lambda resolver connecting the mutation in our schema to the Lambda data source we created above.
Under resources in 'template.yaml', type in:
Database
In our workflow, we save apartment attributes to a database. Let’s go ahead and create the database. It’s a DynamoDB with a single primary key of ID.
State Machine
A couple of steps above, we saved the Step Functions workflow in a file called 'booking_step_functions.asl.json'. We have to create a state machine resource in 'template.yml', link to that file, do some variable substitutions like the DB name, and also provide DynamoDB read and write policies for the 'update' and 'get' item DynamoDB methods.
So let’s go ahead and define the step machine resource under ‘Resources’ in 'template.yml':
After all variable substitutions, the 'booking_step_function.asl.json' file looks like this now:
Please Grab the complete code HERE.
Invoke Step Functions From Lambda
Navigate to 'functions/lambda/app.py' and type in this code:
We import the Step Functions class from the boto3 client and use it to start a Step Functions execution by passing in the StateMachineArn we get from deploying the project, a unique name for the state machine execution, and the state machine input.
Deploy
Deploy the app to your AWS account using 'sam build' and 'sam deploy'
Once deployment is successful, grab the Step Functions arn and proceed to testing in AppSync.
Testing
Sign in to your AWS console and search for AppSync. Open up AppSync and click on your newly deployed AppSync project.



Conclusion
In this post, we built a Step Functions workflow using Appsync, SAM, and Python. This workflow mimics a real-life scenario of booking/reserving an apartment.
- We saw how to invoke Step Functions from a Lambda.
- We covered defining a state machine in a yaml file with variable substitution.
- We broke down how to use IaC to create Applications with Step Functions.
In the next post, we’ll invoke the Step Functions workflow from a mobile frontend application built with amplify and flutter. Stay tuned!
More Screenshots


