Introduction
In the last few years, since around 2016, there’s a concept that’s been gaining popularity. This concept is known as “Serverless Infrastructure”.
Having a serverless infrastructure allows you to have a project running live, but without any permanent infrastructure. This means that when no requests are arriving for the server, there will be no servers actively running. When a request is received, the server will turn on, answer the request, and then shut down.
This serverless approach has one big benefit: server resources will only be in use while they are required, leaving in the past the problem of idle servers.
Providers
AWS provides serverless capabilities through one of their services called Lambda. Quoting AWS Lambda description:
-
Takes care of running and scaling your code (no need to worry about the horizontal scale, Lambda has that covered).
- Only pay for what you use (save money by only paying for actual usage, no usage, no charge).
Frameworks
Regarding deploying Django apps with a serverless infrastructure, there are two big players that can help us:
-
Serverless (Currently over 21900 stars on Github)
- Zappa (Currently over 6000 stars on Github)
Getting started
-
-
Cloud Formation
-
IAM
-
Lambda
-
S3
-
-
-
API Gateway
-
CloudWatch
-
Other services you would want to use
-
There is currently an open task in Zappa for a proper definition of the permissions needed. In the meanwhile, there are two approaches you can take: be flexible with the permissions or do some trial and error until reaching the minimal permissions you need for your app. It’s recommended to remove the IAM permission after Zappa creates the user the first time.
The Project
We will be deploying a simple project that was developed for this tutorial. The project itself is not really important, so please focus on learning how to add Zappa support to an existing Django project. The project can be found here.
Then let’s create a configuration file using the following command:
{
“dev”: { “aws_region”: “us-east-1”, “django_settings”: “innuy_lambda.settings.dev”, “profile_name”: “default”, “project_name”: “innuy-lambda”, “runtime”: “python3.6”, “s3_bucket”: “bucketname” } }
We can solve this by setting a few variables in the dev settings file (or you can do it by env variables in zappa). In this case just set the bucket where you want the statics files to live (if you don’t have a bucket, you can create one with a wizard in AWS S3):