Build the Order Processing System using serverless architecture on AWS

1. Introduction The order processing system is an indispensable component of e-commerce platforms. It is responsible for receiving order information, recording order details in the database, and sending order confirmation notifications to users. In this article, I will walk you through how to design and deploy a serverless order processing system on AWS. 2. Architecture The architecture of the order processing system is built completely using AWS serverless services, which means customers don't need to worry about managing servers as in traditional models. This architecture operates as follows: • When the client sends data via API to the API Gateway, the data is processed by the code within the Lambda function. • The Lambda function then sends a message to the SQS queue, where it awaits processing by another backend logic component in the system. • Additionally, the Lambda function can send order status notification emails via SNS. • To store order data, the Lambda function also writes the information to a NoSQL database (DynamoDB). 3. Implementation Steps 3.1 Create SQS Queue The SQS FIFO (First-In-First-Out) queue ensures that order messages are processed in the exact order they are received. This is crucial for maintaining the sequence of operations and ensuring that orders are handled correctly and efficiently, providing fault tolerance and decoupling the processing steps. This step will guide you through creating an SQS FIFO queue to serve as the message queue • Login to AWS Console and search SQS • In SQS, click Create queue • Select a FIFO queue and name the queue • In the FIFO queue settings: o Enable Content-based deduplication so that SQS can detect and eliminate duplicate messages o Enable High throughput FIFO queue to maintain the order of messages in the queue and ensure deduplication • Click Create queue 3.2. Create Lambda function for processing The Lambda function serves as the central component of the processing logic. It receives incoming requests from the API Gateway, processes the order information, sends the order details to the SQS FIFO queue, and initiates notifications through SNS. Leveraging Lambda allows the system to scale efficiently and remain reliable—without the overhead of managing server infrastructure This step will create a Lambda function to handle messages from the client • In Lambda console, click Create a function • Select Author from scratch • Enter function name • Select Runtime: Python 3.1x • Click Create function 3.3. Create SNS Topic The Simple Notification Service (SNS) delivers notifications to the admin. Whenever an order is received, SNS ensures that a real-time alert is sent to the administrator, keeping them updated with the order details. This section will create an SNS Topic to send order notifications to the recipient's email. • In AWS console, search SNS • Click Topics **then click **Create topic • In SNS, select Standard and enter queue name • Click Create topic • Click Create subscription • Select Protocol: Email • Endpoint: enter your email • Click Create subscription • Login your email • Check email from AWS from Inbox or Spam • Click Confirm subscription to subscribe. 3.4. Create DynamoDB to store order information DynamoDB is used to store order details, with each order being saved in this highly scalable, low-latency NoSQL database. It provides quick and reliable access to order data, ensuring that the information is always available for further processing or querying as needed This section will create a DynamoDB table to store order information • In AWS console, search DynamoDB • In DynamoDB, click Create table • Enter Table name, Partition key and Sort key • Click Create table • Wait for 5 minutes and click the created table • Click *Actions *-> *Create item * • Click Add new attribute -> String • Add 2 attributes “items” and “shippingAddress” • Enter Partition key and Sort key o Partition key: orderID – Value: 1234 o Sort key: customerName – Value: Selina • Click Create item 3.5. Create IAM policy for Lambda function This step will create an IAM policy to allow Lambda to call APIs for SQS, SNS, and DynamoDB. • Access IAM – click Policies **– click **Create policy • Select JSON • Download IAM Policy from Github:https://github.com/ductt7/serverless/blob/main/CustomLambdaPolicy.json • Copy the contents of the CustomLambdaPolicy.json file and paste it into the Policy editor in the IAM policy. o Go back to SQS, copy the ARN of the created SQS queue, and replace the ARN in the policy (line 7 in the Policy editor) with the one you just copied. o Go back to SNS, copy the ARN of the created SNS topic, and replace the ARN in the policy (line 12 in the Policy editor) with the one you just copied o Go to DynamoDB, under the Tables section, click on the Order table you c

May 4, 2025 - 17:45
 0
Build the Order Processing System using serverless architecture on AWS

1. Introduction

The order processing system is an indispensable component of e-commerce platforms. It is responsible for receiving order information, recording order details in the database, and sending order confirmation notifications to users. In this article, I will walk you through how to design and deploy a serverless order processing system on AWS.

2. Architecture

The architecture of the order processing system is built completely using AWS serverless services, which means customers don't need to worry about managing servers as in traditional models. This architecture operates as follows:
• When the client sends data via API to the API Gateway, the data is processed by the code within the Lambda function.
• The Lambda function then sends a message to the SQS queue, where it awaits processing by another backend logic component in the system.
• Additionally, the Lambda function can send order status notification emails via SNS.
• To store order data, the Lambda function also writes the information to a NoSQL database (DynamoDB).

Order Processing System Architecture Based on Serverless

3. Implementation Steps

3.1 Create SQS Queue

The SQS FIFO (First-In-First-Out) queue ensures that order messages are processed in the exact order they are received. This is crucial for maintaining the sequence of operations and ensuring that orders are handled correctly and efficiently, providing fault tolerance and decoupling the processing steps.
This step will guide you through creating an SQS FIFO queue to serve as the message queue
• Login to AWS Console and search SQS

Image description
• In SQS, click Create queue

Image description

• Select a FIFO queue and name the queue
• In the FIFO queue settings:

o Enable Content-based deduplication so that SQS can detect and eliminate duplicate messages
o Enable High throughput FIFO queue to maintain the order of messages in the queue and ensure deduplication

Image description
• Click Create queue

Image description

3.2. Create Lambda function for processing

The Lambda function serves as the central component of the processing logic. It receives incoming requests from the API Gateway, processes the order information, sends the order details to the SQS FIFO queue, and initiates notifications through SNS. Leveraging Lambda allows the system to scale efficiently and remain reliable—without the overhead of managing server infrastructure
This step will create a Lambda function to handle messages from the client
• In Lambda console, click Create a function

Image description

• Select Author from scratch

Image description
• Enter function name
• Select Runtime: Python 3.1x

Image description
• Click Create function

Image description

3.3. Create SNS Topic

The Simple Notification Service (SNS) delivers notifications to the admin. Whenever an order is received, SNS ensures that a real-time alert is sent to the administrator, keeping them updated with the order details.
This section will create an SNS Topic to send order notifications to the recipient's email.
• In AWS console, search SNS

Image description

• Click Topics **then click **Create topic

Image description

• In SNS, select Standard and enter queue name

Image description

• Click Create topic

Image description

• Click Create subscription

Image description

• Select Protocol: Email
• Endpoint: enter your email
• Click Create subscription

Image description

Image description

• Login your email
• Check email from AWS from Inbox or Spam
• Click Confirm subscription to subscribe.

Image description

Image description

3.4. Create DynamoDB to store order information

DynamoDB is used to store order details, with each order being saved in this highly scalable, low-latency NoSQL database. It provides quick and reliable access to order data, ensuring that the information is always available for further processing or querying as needed
This section will create a DynamoDB table to store order information
• In AWS console, search DynamoDB

Image description
• In DynamoDB, click Create table

Image description

• Enter Table name, Partition key and Sort key
• Click Create table

Image description

Image description

Image description

• Wait for 5 minutes and click the created table

Image description

• Click *Actions *-> *Create item *

Image description

• Click Add new attribute -> String
• Add 2 attributes “items” and “shippingAddress”

Image description

• Enter Partition key and Sort key
o Partition key: orderID – Value: 1234
o Sort key: customerName – Value: Selina
• Click Create item

Image description

3.5. Create IAM policy for Lambda function

This step will create an IAM policy to allow Lambda to call APIs for SQS, SNS, and DynamoDB.
• Access IAM – click Policies **– click **Create policy

Image description
• Select JSON
• Download IAM Policy from Github:https://github.com/ductt7/serverless/blob/main/CustomLambdaPolicy.json
• Copy the contents of the CustomLambdaPolicy.json file and paste it into the Policy editor in the IAM policy.
o Go back to SQS, copy the ARN of the created SQS queue, and replace the ARN in the policy (line 7 in the Policy editor) with the one you just copied.

Image description

o Go back to SNS, copy the ARN of the created SNS topic, and replace the ARN in the policy (line 12 in the Policy editor) with the one you just copied

Image description

o Go to DynamoDB, under the Tables section, click on the Order table you created.
o Click Additional info, copy the ARN of the DynamoDB table, and replace the ARN in the policy (line 17 in the Policy editor) with the copied value.

Image description

Image description

o After updating the ARNs of SQS, SNS, and DynamoDB in the IAM Policy editor, click Next.

Image description

• Enter IAM policy name and click Create policy

Image description

Image description

• Go back to Lambda, navigate to the Configuration tab, click Permissions, and then click on the Role name of the Lambda to assign the IAM policy you just created to the Lambda's IAM role.

Image description

• In IAM Role of Lambda, click Add permissions -> Attach policies

Image description

• Type the name of the IAM policy you created earlier, check the box next to the IAM policy, and click Add permissions.

Image description

3.6. Test Lambda function

This step will create a Python script on the Lambda function to process order messages from the SQS queue, send an email to the user, and store order information in DynamoDB.
• Download the Python script:https://github.com/ductt7/serverless
• Go back to Lambda, under the Code section, click on the lambda_function.py file.
• Delete all the existing code.
• Copy the code from the downloaded Python file and paste it into lambda_function.py in the console.

Image description

• Click Test **-> **Configure test event

Image description

• Enter Event name
• Template: select apigateway-aws-proxy

Image description

• Download file: https://github.com/ductt7/serverless/blob/main/JsonForTestEvent.json
• Copy the contents of the JsonForTestEvent.json file and paste it into the Event JSON. Then click Save.

Image description

• Click Test
• If the result returns a status code of 200, it means the code is correct. If not, check the code or the IAM role of the Lambda

Image description

3.7. Create API Gateway

API Gateway acts as the entry point for the order processing system. It receives order data through a POST request, ensuring secure and reliable transmission to the backend for further processing. This service functions as a bridge between external client applications and internal services, enabling seamless and efficient communication.
This step will create an API Gateway to receive HTTP API requests from the client.
• In Lambda, click Add trigger.

Image description

• In Select a source, select API Gateway

Image description

• In Intent, select Create a new API.
• Security: select Open
• Click Add

Image description

• Return to Lambda, click API Gateway

Image description

Image description

• In API Gateway, click APIs and click the created API

Image description

• In Routes, click ANY **-> **Edit

Image description

• Change the API method to POST, as the API Gateway in this case only accepts POST requests from the client.
• Click Save

Image description

3.8. Verify

This step will test sending an API from the client to the API Gateway to check if the Lambda function operates correctly as configured.
• In the Lambda function code, update the following sections:
Update the name of the DynamoDB table on line 6 to match the name of the DynamoDB table you created.

Image description

Image description

o Update the QueueUrl in line 39 with the URL of SQS queue

Image description

Image description

o Update the TopicArn in line 49 with arn of SNS Topic

Image description

Image description

• Click *Deploy *

Image description

• Download test script: https://github.com/ductt7/serverless/blob/main/test-api-command.txt
• In API Gateway, copy URL of API Gateway

Image description

• Update URL of API Gateway.
• Add Lambda function name in the end of the URL

Image description

• In AWS console, open **CloudShell **to simulate the client sends API request to API Gateway

Image description

• Copy script, paste into CloudShell

Image description

• In SQS, click Send and receive messages

Image description

• Click Poll for messages to check if the message has been processed by the Lambda function and added to the queue

Image description

• Click on the message to inspect its content. These messages can be stored in the queue, waiting to be processed by another backend logic component in the system.

Image description

Image description

• Check email from SNS

Image description

• Go to DynamoDB, click Explore items, and check the data that was processed by the Lambda function and written as an item in DynamoDB

Image description

4. Clean up

After completing the lab, you can delete the following resources:
• Delete the API Gateway
• Delete the Lambda Function
• Delete the SNS Topic
• Delete the SQS Queue
• Delete the DynamoDB Table

5. Conclusion

Through this article, I have introduced how to design and deploy an order processing system entirely based on AWS serverless architecture. With this architecture, we no longer have to worry about managing servers or scaling the components within the system. I really hope to receive positive feedback from you all so I can improve and be motivated to continue releasing more articles in the future.