Configure Database
For local development, we can use SQLite, but in general, it’s better to use MySQL server for better performance in large-scale applications, here in this case we used AWS RDS as a DB server.
MySQL in Lambda?
By default, AWS Lambda will not have MySQL, either we have to add custom compiled binary or use existing public lambda layers or we can also use certain python packages to add MySQL support to our Application.
Here in this project, we’re using 3rd approach, and using pymysql , which is a python package that helps us to connect to MySQL DB with ease, to configure our project with pymysql, add the below code snippet in Django settings, here we’ve added this under settings/__init__.py.
The below snippet makes Django ORM use pymysql without any code changes needed.
Python3
import pymysql pymysql.install_as_MySQLdb() |
Have a look at the Database config in todo/settings/prod.py > DATABASES variable.
I recommend you use environment variables for confidential data, and also it can be easily configurable for different environments.
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': os.getenv("SQL_DB_NAME"), 'USER': os.getenv("SQL_DB_USERNAME"), 'PASSWORD': os.getenv("SQL_DB_PASSWORD"), 'HOST': os.getenv("SQL_DB_HOST"), 'PORT': os.getenv("SQL_DB_PORT"), } }
How to Deploy Django Application in AWS Lambda?
Django is a Python web framework that makes it easy to build web applications quickly and securely. It has a large and helpful community that provides support and contributes to its development.
AWS Lambda is a serverless computing platform that runs your code in Docker containers, provides us with the best possible speed, and there is a number of third-party apps, and tools that make Lambda usage much easier, in this tutorial we gonna use one such tool.