0

For starters, I have tried the following posts & they did not help.

Using Django ORM Inside AWL Lambda

Using Only The Db Part Of Django

Django Stand Alone Docs

Following the django docs and using pieces of the above links I created the following standalone script to use django's ORM.

import django
from django.conf import settings



def configure():
    settings.configure(DEBUG=False,
                       DATABASES={
                           # TODO: Change this to environment variables
                           'default': {
                               "ENGINE": "django.db.backends.mysql",
                               "NAME": "db_name",
                               "HOST": "db_host",
                               "USER": "admin",
                               "PASSWORD": "db_pass"
                           }
                        },
                        INSTALLED_APPS=['my_models']
                       )
    django.setup()

In my handler.py file with my registerUser lambda functions

from .django_orm.main import configure
configure()

from .django_orm.my_models import AppUser

def registerUser(event, context):
    #Application Logic
    newUser = AppUser()
    newUser.save()
    return True

When I run this I get the following error ...

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'my_models'

My directory structure is the following:

./myServerlessProject
   ./djangoORM
     ./main.py
     ./manage.py
     ./my_models
       ./__init__.py
       ./models.py
   ./handler.py
   ./serverless.yml

What am I doing wrong and how do I get the django ORM to be stand-alone?

pjonson2
  • 31
  • 3
  • If it were me, I'd use Zappa (https://github.com/Miserlou/Zappa) to spin up your barebones Django project on AWS Lambda, with an AWS Virtual Private Cloud and RDS. Then point your settings against the RDS instance credentials. – FlipperPA Aug 07 '19 at 22:08
  • I can not use Zappa. The Zappa middleware adds too much lag time for cold starts. – pjonson2 Aug 08 '19 at 14:28
  • Have you looked at the keep warm option in Zappa? The only cold start should be the first one. https://github.com/Miserlou/Zappa#keeping-the-server-warm – FlipperPA Aug 08 '19 at 17:14

0 Answers0