Serverless Micro Django | Lightweight, yet powerful Python utility for lambda functions

Serverless Micro Django

A couple of weeks ago I had this task in Agileful to refactor and improve some AWS lambda functions.

There was a bunch of pure SQL queries written alongside python codes which made me think, what if we could use standalone Django ORM in our python lambda functions?

So I did some digging on how we can use my beloved ORM in python scripts despite the serverless stuff, there were some vague and old answers but I managed to make something out of it.
It’s as simple as defining a database connection settings and calling django.setup().

Standalone Django ORM

Here is the minimal python code needed to use standalone Django ORM:

Python Codes Pt. 1
Serverless Micro Django - Python Codes Pt. 2

Plus these two files you need to create your desired Django app models. (e.g. videos_app/

There is one downside in this way of using Django and that is the lack of !
I haven’t got time to try adding commands so I added some helper scripts like the ones below:

Serverless Micro Django - Python Codes Pt. 3 /

So even if you just need Django ORM without all the serverless and fancy stuff use the codes above and you’re good to go. Or check out the GitHub repository:

Serverless Micro Django!

So let’s make it Serverless!
As we know lambda functions must accept two function parameters: event, context.

As SMD (Serverless Micro Django) is supposed to be lightweight and micro! it doesn’t support any REST API like Django & Flask. Just function!

Hence when you are invoking the function you must pass your desired action/event in lambda event argument:

SMD - Python Codes Pt. 4

And in the end, I developed a ModelController so I can execute the events. Which I’m not going to get into and talk about.

I’ve also done some event input validations and modeling using PyDantic which you can find in the project’s GitHub repository.

I know there is a long way for SMD to become mature and production-ready. I would gladly accept and be open to contributions and feedback. (Documenting, Development, Content Writing, etc…)

GitHub Repository:

Published by

Mohammad Moallemi

Software Engineer by day, Technical Lead by night

2 thoughts on “Serverless Micro Django | Lightweight, yet powerful Python utility for lambda functions”

    1. Good Question.
      I personally rather Django ORM syntax and rich documentation in comparison to SQLAlchemy.
      It’s more like a personal preference rather than a logical reason.

Leave a Reply

Your email address will not be published. Required fields are marked *