Deploy Django serverless with Lambda

You have successfully deployed Django to AWS Lambda. It's easy with the Serverless Framework. The URL and repository are here. https://django-sls-helloworld.umihi.co/ https://github.com/umihico/django-sls-helloworld

Create a default project for sls and f1a13ba After confirming the operation, install the custom domain plugin. 0970afe API Gateway does not remove the stage name at the end of the URL, it seems to be incompatible with django's routing, so I prepared a custom domain first.

$ serverless create --template aws-python3 --path django-sls-helloworld #Project creation
$ cd django-sls-helloworld
$ serverless deploy #Deploy once
$ serverless invoke -f hello #Test if it works properly
{
    "statusCode": 200,
    "body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}
$ sls plugin install -n serverless-domain-manager #Plugin installation for domain settings

Edit serverless.yml. 603753b

serverless.yml




+ custom:
+   customDomain:
+     domainName: django-sls-helloworld.umihi.co
+     certificateName: umihi.co
+     basePath: ''
+     stage: ${opt:stage, self:provider.stage}
+     createRoute53Record: true
+     endpointType: 'edge'
+     securityPolicy: tls_1_2

provider:
  name: aws
  runtime: python3.8
$ sls create_domain
#It is said that it will take up to 40 minutes, but the next deploy command can be done immediately. It just takes time to apply the domain.
$ sls deploy

This completes the web page with the custom domain set. Then install the required libraries for Django. 80e8f1b

$ sls plugin install -n serverless-python-requirements

I added the following files and edited handler.py to test if it was imported but not needed. ec47570

requirements.py


import os
import sys

requirements = os.path.join(
    os.path.split(__file__)[0],
    '.requirements',
)

if requirements not in sys.path:
    sys.path.append(requirements)

requirements.txt


Django
Werkzeug
PyMySQL

handler.py


import json
+ import requirements

def hello(event, context):
+     # testing import libraries
+     import django
+     import werkzeug
+     import pymysql
    body = {
        "message": "Go Serverless v1.0! Your function executed successfully!",

If you deploy and serverless invoke -f hello is successful, the import is successful. Next, create a Django project. 930a0c2 Then install the WSGI plugin for the serverless library. 52386e4

$ django-admin startproject django_sls_helloworld .
$ sls plugin install -n serverless-wsgi

Finally, Fix installing PyMySQL instead of MySQLdb and fix the version difference pointed out at that time, 30361dd Add the name of the domain set in ALLOWED_HOST and 1966857 Sort Lambda to refer to WSGI. 1dce3d3

django_sls_helloworld/settings.py


import os
+ import pymysql
+ 
+ pymysql.version_info = (1, 4, 2, "final", 0)
+ pymysql.install_as_MySQLdb()
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)

abridgement

DATABASES = {
    'default': {
-         'ENGINE': 'django.db.backends.sqlite3',
-         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+         'ENGINE': 'django.db.backends.mysql',
+         'NAME': 'djangodemo',
    }
}

abridgement

- ALLOWED_HOSTS = []
+ ALLOWED_HOSTS = ["django-sls-helloworld.umihi.co"]

serveless.yml


  endpointType: 'edge'
  securityPolicy: tls_1_2
+ wsgi:
+   app: django_sls_helloworld.wsgi.application
+   packRequirements: false

abridgement

functions:
  hello:
-     handler: handler.hello
+     handler: wsgi_handler.handler
#    The following are a few example events you can configure

After deploying, the familiar screen appeared even at the custom domain destination. that's all. By the way, since it is a disposable project, it is deployed with DEBUG = True, and SECRET_KEY is also posted on Github, but please note that it is taboo in actual operation.

スクリーンショット 2020-06-20 11.14.30.png

Recommended Posts

Deploy Django serverless with Lambda
Deploy Python3 function with Serverless Framework on AWS Lambda
Deploy a Django application with Docker
Run python3 Django1.9 with mod_wsgi (deploy)
Django Heroku Deploy 1
Internationalization with django
Django Heroku Deploy 2
CRUD with Django
Regular serverless scraping with AWS lambda + scrapy Part 1.8
Serverless scraping using selenium with [AWS Lambda] -Part 1-
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
Lambda function deploy best practices with CircleCI + Lamvery
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Make Lambda Layers with Lambda
Development digest with Django
Output PDF with Django
Markdown output with Django
Use Gentelella with django
Twitter OAuth with Django
Getting Started with Django 1
Send email with Django
File upload with django
Use LESS with Django
Pooling mechanize with Django
Use MySQL with Django
Start today with Django
Getting Started with Django 2
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Create Python version Lambda function (+ Lambda Layer) with Serverless Framework
Deploy an existing app with docker + pyenv-virtualenv + uwsgi + django
Deploy a Django app made with PTVS on Azure
Deploy Flask with ZEIT Now
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Easily serverless with Python with chalice
Minimal website environment with django
Create an API with Django
Do Django with CodeStar (Python3.8, Django2.1.15)
Python3 + Django ~ Mac ~ with Apache
Easily write if-elif with lambda
Getting Started with Python Django (1)
Create a homepage with django
Get started with Django! ~ Tutorial ④ ~
Getting Started with Python Django (4)
Python compilation with pyqt deploy
Web application creation with Django
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
Get started with Django! ~ Tutorial ⑥ ~
Save tweet data with Django
AWS Lambda with PyTorch [Lambda import]
Do AES encryption with DJango
Dynamically move Amplify with Lambda
Getting Started with Python Django (6)
Combine two images with Django
Getting Started with Django with PyCharm
Real-time web with Django Channels
Deploy django project to heroku
Double submit suppression with Django
Django REST framework with Vue.js