AWS Builder Center

Building a Serverless Spam Filter for Student Emails: What AWS Lambda Taught Me About Production Thinking

When I set out to build an AI-based spam detection system for student email

When I set out to build an AI-based spam detection system for student emails during the FAST University SOFTEC AI Hackathon, my first instinct was to reach for a traditional setup: a server, a queue, a database, and a model sitting behind an API. It's the pattern most of us default to because it's familiar. But a 24-48 hour hackathon window has a way of forcing better decisions, and going serverless turned out to be one of the best calls I made, not because it was trendy, but because it matched the actual shape of the problem.
Why Serverless Fits This Problem: Spam detection on incoming student emails is fundamentally an event-driven workload. An email arrives, it needs to be classified, and then it needs to be routed, accepted, flagged, or quarantined. There's no steady-state traffic to plan for. Email volume spikes around registration periods, assignment deadlines, and exam announcements, then drops to near-zero overnight. Provisioning a server to sit idle for that pattern is wasted spend and wasted operational effort.
AWS Lambda made this trivial to reason about. Each email event triggered a function invocation, the classification model ran, and the function exited. No idle compute, no capacity planning, and critically for a hackathon, no infrastructure to babysit while I was supposed to be improving the model itself.
misclassifications feed back into retraining

The Architecture
The pipeline ended up looking like this:
  1. Ingestion — incoming emails were captured and dropped into an event source (in our case, a queue-backed trigger simulating a mail server hook).
  1. Lambda classifier — a Python-based Lambda function loaded a lightweight trained model and scored each email for spam probability.
  1. Routing logic — based on the score, the function tagged the email as clean, suspicious, or spam, and forwarded it accordingly.
  1. Logging and feedback — classification decisions were logged so the model's false positives and false negatives could be reviewed and fed back into retraining.
The entire system had no persistent server. Everything scaled with the actual number of emails coming in, and everything shut down completely between invocations.
The Real Lesson: Cold Starts and Model Loading
The part that took the most iteration wasn't the ML model it was making Lambda's execution model work well with a machine learning workload. Loading a model file on every cold start added latency that would have been invisible in a traditional always-on server but was very visible in a serverless function, measured in milliseconds.
The fix was to keep the model artifact small and load it outside the handler function, at the module level, so that warm invocations could reuse the already-loaded model instead of reading it from disk or memory every time. This is a pattern that's easy to state but easy to get wrong the first time you try it: anything expensive to initialize — model weights, database connections, SDK clients — belongs outside the handler, not inside it.

What I'd Do Differently in Production

A hackathon prototype and a production system have different tolerances for failure, and this is where I've kept thinking about the project even after the event ended:
  • Concurrency limits matter more than they seem to. A sudden burst of emails (say, during registration week) could trigger a burst of concurrent Lambda invocations. Without reserved concurrency or throttling in place, that burst could either overwhelm downstream services or hit account-level concurrency caps and start failing invocations silently.
  • Dead-letter queues aren't optional. In a hackathon, a failed classification just means one email doesn't get tagged. In production, that's a silent gap in your spam filter. Routing failed invocations to a dead-letter queue for later inspection is a small addition with a large payoff.
  • Observability needs to be designed in, not bolted on. CloudWatch logs alone tell you a function ran. They don't tell you why your false-positive rate crept up last week. Structured logging with classification confidence scores, tied into a dashboard, would have made the model's drift visible instead of something we noticed only when someone complained.

Closing Thought

What stuck with me most from this project wasn't the spam detection accuracy it was how much the serverless model changed the way I thought about the problem itself. Instead of asking "how much capacity do I need?" I was asking "what actually needs to happen when an email arrives?" That's a more honest question, and it's one that traditional infrastructure tends to let you avoid asking.
Serverless isn't a shortcut around infrastructure thinking. It just moves the hard questions earlier into the design of the event, the function, and the failure paths instead of letting you defer them to a server you'll eventually have to babysit anyway.
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