Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Django ORM inside an AWS Lambda function

I have an existing Django application data stored under Postgres RDS. Now I want to query/update the data via a lambda(AWS) function with Django style ORM.

I know in theory it is possible if,

  • Package the whole Django library with the lambda function(zipped)
  • Declare all the models inside package
  • Start using Django ORM as we do normally (Ex User.objects.all() )

I wanted to know if anyone has done this? Examples or write-ups are much appreciated

like image 719
nehem Avatar asked Sep 01 '17 01:09

nehem


People also ask

Can you run Django on AWS Lambda?

In order to deploy a Django project on AWS Lambdas you should prepare your AWS infrastructure. There is a list of AWS services I use for my Django project: Lambdas to run our wsgi application. API Gateway to handle HTTP request and send them to Lambdas.

Can I use Django ORM without Django?

Django doesn't have a separate package for it's ORM, but it's still possible to use it on its own.

Can you use boto3 in Lambda?

You can run Python code in AWS Lambda. Lambda provides runtimes for Python that run your code to process events. Your code runs in an environment that includes the SDK for Python (Boto3), with credentials from an AWS Identity and Access Management (IAM) role that you manage.

Should I use Django ORM?

Use Django ORM for your Data Management tasks. Even without starting any web server. The extensive feature set of Django as a high-level Web framework can scare off Data Analysts and Data Scientists who never really had contact with Web development.


1 Answers

If you only want to use Django's ORM (no views, admin, templates), then yes, you can use Django ORM in AWS Lambda as a library and no need for Zappa.

You can see how to do it from here: Using only the DB part of Django

However, take note that in AWS Lambda, you are billed per 100ms of execution time and Django ORM is not exactly fast (vs. direct raw queries).

It is recommended that you keep your Lambdas as lean as possible. Loading up the entire Django package is opposite of that recommendation.

like image 63
Noel Llevares Avatar answered Sep 17 '22 09:09

Noel Llevares