AWS Builder Center

RTO Shield: Building a Serverless COD Risk Detection System with AWS

Learn how I built RTO Shield, a serverless system that checks customer and pincode delivery history to identify potential COD RTO risk using AWS Lambda and Amazon DynamoDB.

Tech Explorer

RTO Shield: Building a Serverless COD Risk Detection System with AWS

Return-to-Origin (RTO) is a common challenge for e-commerce businesses, especially for Cash-on-Delivery (COD) orders.
When an order fails to get delivered, the seller can lose money through shipping costs, reverse logistics, inventory blockage, and operational effort.
For my AWS hackathon project, I built RTO Shield, a serverless prototype that helps identify potentially risky COD orders before they are shipped.
The goal was simple:
Use historical delivery data to give sellers an early warning about possible RTO risk.

The Problem

A COD order can look completely normal when it is created.
The seller may know the customer's name, phone number, address, pincode, and order value, but historical delivery behavior may provide additional useful context.
For example, a customer may have previous failed deliveries or RTOs. If individual customer history is unavailable, historical performance for the delivery pincode can provide another signal.
However, the system should not automatically assume that every customer from a particular pincode is risky.
That led me to design RTO Shield around three possible outcomes:
  1. Customer history is available → use customer history.
  1. Customer history is unavailable but pincode history exists → use pincode-level historical indicators.
  1. Neither is available → return UNKNOWN instead of inventing a risk assessment.

What RTO Shield Does

The application provides a simple order-analysis interface.
The user enters:
  • Customer name
  • Phone number
  • Order value
  • Delivery address
  • Delivery pincode
The frontend sends this information to the backend.
The backend then checks historical information stored in Amazon DynamoDB.
The risk engine calculates a score from 0 to 100 and classifies the result as:
  • LOW
  • MEDIUM
  • HIGH
  • UNKNOWN
The system also provides reasons behind the score so that the result is explainable rather than being just a number.

Architecture

The final architecture is intentionally simple:
Frontend → AWS Lambda Function URL → AWS Lambda → Amazon DynamoDB → Risk Assessment → Frontend
I initially considered using API Gateway, but the AWS workshop environment restricted the required API Gateway permissions. Instead of getting stuck, I adapted the architecture and used a Lambda Function URL.
This turned out to be a useful lesson: designing a cloud application also means understanding the environment and adapting the architecture when necessary.

AWS Services Used

AWS Lambda

AWS Lambda runs the backend risk-analysis logic.
The Lambda function:
  1. Receives the order information.
  1. Searches DynamoDB for matching customer history.
  1. If no customer history is found, checks the delivery pincode.
  1. Calculates the risk score.
  1. Returns the result to the frontend.
Using Lambda meant I didn't need to provision or manage a traditional server.

Lambda Function URL

The Lambda Function URL provides the HTTP endpoint used by the frontend.
This allowed the browser-based application to communicate directly with the Lambda backend while keeping the architecture lightweight.

Amazon DynamoDB

Amazon DynamoDB stores the synthetic historical delivery data.
The prototype uses records representing customer history and pincode history.
For example, a pincode record can contain information such as:
  • Total orders
  • Failed deliveries
  • Previous RTOs
  • Pincode identifier
The Lambda function calculates historical rates from this information.
For example:
Failed-delivery rate = Failed deliveries / Total orders
RTO rate = RTO count / Total orders
These historical rates become inputs to the risk-scoring rules.

AWS IAM

IAM controls what the Lambda function can access.
The Lambda execution role was configured to allow the required DynamoDB read operations.
Working with IAM also helped me understand an important AWS concept: a service can exist and be correctly configured, but the application still cannot use it unless the appropriate permissions are available.

Amazon CloudWatch

CloudWatch Logs helped during development and debugging by making Lambda execution information available for inspection.
This was particularly useful while testing the DynamoDB lookup logic and frontend-to-Lambda integration.

How the Risk Engine Works

RTO Shield uses an explainable rule-based scoring system.
For customer history, factors such as previous failed deliveries and previous RTOs contribute to the score.
Order value can also contribute additional risk points because a high-value COD order can represent greater exposure for the seller.
For pincode-based analysis, the system looks at historical failed-delivery and RTO rates.
For example, if a pincode has:
  • 100 historical orders
  • 8 failed deliveries
  • 5 RTOs
then:
Failed-delivery rate = 8%
RTO rate = 5%
The system uses predefined thresholds to translate these indicators into a score.
The final score is capped at 100.
The result is then classified into a risk level.

Why Explainability Matters

One of the design goals of RTO Shield was to avoid producing a mysterious prediction.
Instead of simply saying:
Risk: 70
the system explains why.
A result can contain factors such as:
  • Previous failed deliveries
  • Previous RTOs
  • Pincode historical failed-delivery rate
  • Pincode historical RTO rate
  • Order value
This makes the output easier for an operations team to understand.
The system also clearly identifies whether the result came from customer history, pincode history, or no historical data.

Handling Missing Data

A particularly important part of the project is the UNKNOWN state.
If the system cannot find individual customer history and cannot find pincode history, it does not pretend that it has enough information.
Instead, it returns:
UNKNOWN — No historical data available
This allows the seller to manually review the order rather than treating missing information as evidence of risk.

Testing the System

I tested the complete workflow using synthetic data.
For example, a pincode with moderate historical delivery issues produced a result such as:
40/100 — MEDIUM
Another pincode with higher historical failure and RTO rates produced:
70/100 — HIGH
A pincode without historical data produced:
UNKNOWN — No historical data available
These tests verified that the application could automatically move through the customer-history, pincode-history, and no-history paths.

What I Learned

This project gave me practical experience with several AWS concepts.
The biggest learning was that building a cloud application is not just about writing code.
I had to understand:
  • Lambda execution roles
  • IAM permissions
  • DynamoDB partition keys
  • Lambda Function URLs
  • CORS
  • Serverless architecture
  • CloudWatch debugging
  • Connecting a frontend to an AWS backend
I also learned to adapt when an AWS service was not available in the workshop environment. Instead of depending on API Gateway, I redesigned the endpoint around Lambda Function URLs.

What's Next

RTO Shield is currently a prototype, so there are several directions in which it could be extended.
Possible future improvements include:
  • Larger historical datasets
  • More sophisticated statistical or machine-learning models
  • Time-based delivery trends
  • Better customer identity matching
  • Additional address-quality signals
  • Seller dashboards and analytics
  • Historical risk tracking
  • Integration with e-commerce order systems
The current version deliberately focuses on a simple, explainable workflow that can demonstrate the core idea.

Conclusion

RTO Shield started with a straightforward question:
Can historical delivery information help an e-commerce seller identify potential COD RTO risk before shipping?
The resulting prototype combines a lightweight frontend with AWS Lambda and Amazon DynamoDB to answer that question through an explainable risk-scoring workflow.
The project also gave me hands-on experience designing, debugging, and adapting a real serverless AWS application.
Most importantly, the system is designed to distinguish between known risk signals and missing information.
When there is evidence, it provides a score.
When there is no evidence, it says UNKNOWN.
That principle is an important part of making risk-assessment systems more transparent and useful.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article