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.
Recommended Posts