AWS Logo
Menu
Serverless Cloud Pizzeria Shop

Serverless Cloud Pizzeria Shop

Example Full-stack Serverless project using ReactJS, Momento Topics, Step Functions, API Gateway, ECS, Lambda, DynamoDB with streams, and more to build an online pizza shop where the progress of orders is updated automatically in the UI.

Published Sep 30, 2024
I have spent lots of time learning about various areas of software and cloud development so far in my career and wanted to work on a project that combined many of those components. In recent years most of my focus has been on backend technologies but i wanted to get back to building some full-stack solutions. The full source for this project can be found here in Github: https://github.com/RDarrylR/serverless-pizza-ordering
Recently we have all heard a lot about the company Momento and the products they offer include Topics and Caches. These are truly serverless, scale-to-zero, pay as you go products that implement high-speed caching and pub/sub messaging solutions you can use in your projects.
I wanted to come up with an example project that could take advantage of these. What I am presenting today is a web-based ordering system (developed using React JS) that includes live updates of the progress of your pizza orders using Momento Topics. The backend for the project is in AWS and uses various components including API Gateway, AWS Lambda, Step Functions, the Elastic Container Service (ECS) with Fargate compute, DynamoDB with streams, and more. It uses a mix of Python and Rust for the code.
A common Momento Topic is used between the frontend app and the backend for each order. The frontend has a short-lived token that allows it to subscribe to the topic and receive updates. The backend pushes updates to the topic as the order progresses. Powerful AI actually creates the pizzas and delivers them to the customers based on some containers running in the ECS cluster. Of course we could have used AWS Lambda functions for the actual making of the pizzas and the delivery but the AI tells me there are some rare cases where it could take more than 15 minutes and that they prefer working with containers rather than some Firecracker Micro-VM when pizza is involved.
The Infrastructure as Code (IaC) tool i used for this project is Terraform. I like to use the Serverless Application Model (SAM) as well for my projects but Terraform does a great job when there are a lot of components required to be setup. In this project we are creating a VPC, subnets, ECS clusters, Step Functions, an API Gateway, and much more. Terraform can handle this all cleanly and with a nice set of config files.

Frontend

The front end consists of a typical online storefront (coded with React JS) with a list of products — which (in the Cloud Pizzeria) are one-size fits all types of classic pizzas. For some reason we did include Hawaiian pizza but that must have been a glitch in the system. Please note that you cannot customize the toppings on each pizza as the AI is very fussy claiming each of their creations is a classic and you can pick things off when you get it if you’re are really that concerned. You can choose between super-fast AI-based delivery or pick up the pizzas yourself.
The store allows you to see the descriptions of the pizzas, add them to a cart, etc. Before you can place an order you need to fill in your profile information including name, address, and phone number — after all the AI needs to know where to deliver it or who to call if you don’t show up.
Here are some pics of the store:
Product list
Product list
Product detail
Product detail
Once you have chose your products and filled in a profile you can go to the cart and complete your order. Make sure to pick your order type of Delivery or Pickup. After this you can sit back and watch the progress as the powerful AI makes your pizza and delivers it to your door (or at least tells you it is done if you chose to pick it up).
Checkout
Checkout
After you complete the purchase in the frontend, you are presented with a status of your order. As the order progresses you will see live updates and timestamps on them. Once your order is ready close the status page and get ready to order again.
Order started
Order started
Order in oven
Order in oven
Order in oven
Order in oven
The key to the front end getting the updates is it subscribes to the Momento Topic that the backend passed it an ID for. Once it’s listening for these updates it will be able to update the progress as the images above show. Below is a snippet of the code to set this up.

Backend design

The backend of this project is driven by API Gateway and AWS Step Functions. The API that is used to create orders is setup in API Gateway. The API Gateway is built using an Open API spec file as setting it up piece by piece in Terraform is rather ugly. The create order call to API gateway is backed by an AWS Lambda function coded in Python. This function creates an entry for the order in a DynamoDB table and generates a temporary token using the Momemto Auth Client, which is specific to the current order, and sends it back to the client in the response. Below is a snippet of the Lambda handler.
The AWS Lambda function does not initiate the state machine that drives the order processing. I decided to go for a more event-driven approach and wanted to use DynamoDB streams. When the DynamoDB table has the new order inserted from the function above, the stream setup on the DynamoDB table will emit an event. The process_dynamodb_stream Lambda function — which has an Event Source Mapping (ESM) of events on this table setup on it — will be executed based on updates to the DynamoDB table.
The process_dynamodb_stream will parse the event and if it’s a new order in the PENDING state it will initiate an execution of the Process-Pizza_Order-State-Machine Step Function for the new order. Below is a snippet of the process_dynamodb_stream function.
The AWS Step Function is the main driver of the workflow to process the order. Step Functions can call almost any AWS service or any type of external API. In the example I am using the HTTP Task type in Step Functions to call the Momemento Topics HTTP API to update status of the order being processed. The actual work of making the pizza and delivering it is being done by advanced AI which is controlled using containers running in ECS with Fargate compute. The state machine passes a Task Token to the ECS Tasks which have to return it when they are done processing their work. Inside the ECS Tasks, the status of the order gets updated in the DynamoDB tables. The state machine also has to know whether to send the AI to deliver the order or whether the customer is picking it up. Below is a diagram of the state machine and one showing an example of a completed order.
State machine
State machine
State machine execution
State machine execution

Container Code

The code needed to control the AI to actually make the pizzas and deliver them is super complex and they insisted I use Rust for it since it is the only thing the AI is told it can trust. Unfortunately, I had to remove the core of that code before i checked it into Github but left a skeleton of it. It still shows the Step Functions token handling and the DynamoDB updates.

Try the example in your AWS account

You can clone the Github Repo and try this out in your own AWS account. The README.md file mentions any changes you need to make for it to work in your AWS account.
Please let me know if you have any suggestions or problems trying out this example project.
For more articles from me please visit my blog at Darryl's World of Cloud or find me on X, LinkedIn, Medium, Dev.to, or the AWS Community.
For tons of great serverless content and discussions please join the Believe In Serverless community we have put together at this link: Believe In Serverless Community
 

2 Comments