AWS Builder Center

Building a Cloud-Powered Game with AWS Lambda, DynamoDB, and Hybrid AI: How We Combined FSM and Reinforcement Learning in a Zombie Survival Game

Most game AI tutorials stop at FSM. Most AWS tutorials stop at the API. This article shows how to wire a fully serverless AWS backend, Flask, Zappa, Lambda, API Gateway, DynamoDB, and S3, into a Unity game that runs a hybrid FSM and Reinforcement Learning NPC system. Built from a real Final Year Project that won Top 8 Runner-Up at a university exhibition. Intermediate level, assumes basic AWS and Unity knowledge.

Associate Solutions Architect | AWS · Kubernetes · Terraform · CI/CD | Technical Author



The idea

Hassan Saleh Hayat and I built AI Hitman for our Final Year Project at the University of Lahore. We wanted to build something that actually pushed us, not another web app, not another CRUD system. The goal was a zombie survival game with NPCs that genuinely learned, backed by a real cloud backend.
It won Top 8 Runner-Up at our university's FYP Exhibition, supervised by Mam Sana Fatima. That part was nice. But honestly, what stuck with me was how much the AWS backend taught me about building systems under pressure.



Who built what

Hassan owned the Unity side, the game world, player mechanics, enemy animations, and the first version of the NPC behavior system.
I owned the AWS backend completely. The Flask API, Zappa deployment, Lambda functions, API Gateway routing, DynamoDB schema, S3 integration, all of that was mine to design and ship.
The AI architecture was also mine to propose. Hassan's original plan was FSM-only, which is a reasonable starting point. Finite State Machines are clean and controllable. They are also, if you have played enough games, painfully easy to exploit once you figure out the pattern. I pushed for something harder to predict. Pure Reinforcement Learning was the obvious answer, except it is notoriously unstable in real-time game environments, the agent does strange things during training, and you end up with zombies that stand in corners because the reward function did not account for that.
The hybrid approach solved it. FSM handles the state structure, Idle, Patrol, Chase, Attack, Flee. RL runs underneath, controlling movement decisions within those states. The FSM keeps behavior readable and controllable. The RL makes the movement unpredictable. Neither weakness cancels the other out.
Hassan implemented the RL layer in Unity once the architecture was locked. He made the game feel like a game. I made the backend work and pushed the AI further than we originally planned.



The AI system

Standard FSM NPCs are fine. The player learns the rules in twenty minutes and the game stops being scary. I wanted zombies that felt like they were actually hunting you.
The hybrid system works like this. Each zombie has five FSM states: Idle, Patrol, Chase, Attack, Flee. Inside those states, the RL agent controls how the zombie moves, the angle it approaches from, whether it hangs back or rushes, how it responds to other zombies nearby.
The RL agent trained on a reward signal tied to damage dealt, survival time relative to the player, and proximity. After enough sessions, something interesting happened. Zombies started approaching from different angles simultaneously. Nobody programmed that. It came out of the reward function.
Training used Unity ML-Agents. The weights were baked into the build. All session data flowed to AWS.



The AWS architecture

The entire backend was serverless. No EC2 instances, no containers, no infrastructure to babysit at 2am.
The API was built in Python with Flask. I used Zappa to deploy it to Lambda. If you have not used Zappa before, it takes your Flask app and packages it into a Lambda function, wires up API Gateway, and sorts out IAM roles, one command.  zappa deploy production . That is it. The first time it worked I genuinely sat back for a moment.
Flask API → Zappa → Lambda → API Gateway

The game client in Unity called three endpoints.  POST /session/start  created a session and returned an ID.  POST /session/update  recorded events during the game, kills, damage, survival time.  GET /session/{id}/stats  pulled the stats back when the session ended.
DynamoDB stored all of it. The schema was straightforward: session ID as the partition key, timestamp as the sort key, and attributes for player stats and RL reward signals per round. I used on-demand capacity mode, which meant zero cost during development and automatic scaling if the game ever saw real traffic.
S3 handled the static side, level configs, NPC behavior files, leaderboard snapshots. Storing NPC configuration in S3 meant I could tweak enemy difficulty without shipping a new Unity build. That saved time more than once during testing.
Unity Client
│
▼
API Gateway
│
▼
Lambda (Flask via Zappa)
│
┌──┴──────────┐
▼ ▼
DynamoDB S3




The Architecture Diagram





What I learned

Zappa is genuinely one of the most underrated tools in the Python ecosystem. Deploying a Flask API to Lambda without touching a single CloudFormation template or writing a Dockerfile felt wrong at first, like I was skipping steps. I was not. It just worked.
DynamoDB took longer to click. I kept designing tables like they were PostgreSQL, which is exactly the wrong way to think about it. DynamoDB rewards you when you start from the access patterns and work backward to the schema. Once I restructured around "what queries will I actually run," the whole thing made sense.
The RL reward function was the hardest part of the project, not the AWS setup, not the Unity integration. Getting an agent to learn useful behavior instead of weird exploits takes more iteration than you expect. Early training runs produced zombies that stood completely still because the penalty for dying was lower than the cost of moving toward a player who kept shooting them. Technically rational. Completely useless. Reward shaping is its own skill.



Results

Top 8 Runner-Up at the University of Lahore FYP Exhibition 2026, team of two. Fully serverless backend that cost nothing to run during development. NPC behavior that nobody explicitly programmed, which is still the part I find most satisfying to explain.



What I would do differently

If I rebuilt this today, I would run RL inference in the cloud using Amazon Bedrock rather than baking weights into the build. Live sessions would feed back into training and the enemies would keep getting harder over time.
I would also add CloudWatch dashboards from day one. Flying blind on Lambda cold starts and DynamoDB read patterns during testing cost me debugging time I did not need to spend.
The event pipeline would move to EventBridge too. Decoupling session events from the API directly made the architecture more resilient, a pattern I went on to use in StatusNest and OrderFlow, which I will cover next.


Source code and architecture documentation for AI Hitman is on our GitHub.
This was a collaboration between myself and Hassan Saleh Hayat. Questions about the RL system or the AWS setup, drop them in the comments.
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