Skip to main content

Logo # django-zappa Build Status #### Serverless Django with AWS Lambda + API Gateway

django-zappa makes it super easy to deploy Django applications on AWS Lambda + API Gateway. Think of it as “serverless” web hosting for your Django apps.

That means:

  • No more tedious web server configuration!

  • No more paying for 24/7 server uptime!

  • No more worrying about load balancing / scalability!

  • No more worrying about keeping servers online!

  • No more worrying about security vulernabilities and patches!

django-zappa handles:

  • Packaging projects into Lambda-ready zip files and uploading them to S3

  • Correctly setting up IAM roles and permissions

  • Automatically configuring API Gateway routes, methods and integration responses

  • Deploying the API to various stages of readiness

Awesome!

See it in action here! You can also watch a `screencast on how to use django-zappa <https://www.youtube.com/watch?v=plUrbPN0xc8&feature=youtu.be>`__.

This project is for Django-specific integration. If you are intersted in how this works under the hood, you should look at the `Zappa core library <https://github.com/Miserlou/Zappa>`__, which can be used by any WSGI-compatible web framework.

Installation

$ pip install django-zappa

Configuration

There are a few settings that you must define before you deploy your application. First, you must have your AWS credentials stored in ~/.aws/credentials’.

Finally, define a ZAPPA_SETTINGS setting in your local settings file which maps your named deployment environments to deployed settings and an S3 bucket (which must already be created). These can be named anything you like, but you may wish to have seperate dev, staging and production environments in order to separate your data.

ZAPPA_SETTINGS = {
    'production': {
       's3_bucket': 'production-bucket',
       'settings_file': '~/Projects/MyApp/settings/production_settings.py',
    },
    'staging': {
       's3_bucket': 'staging-bucket',
       'settings_file': '~/Projects/MyApp/settings/staging_settings.py',
    },
}

Notice that each environment defines a path to a settings file. This file will be used as your server-side settings file. Specifically, you will want to define a new SECRET_KEY, as well as your deployment DATABASES information.

A Note About Databases

Since Zappa requirements are called from a bundled version of your local environment and not from pip, and because we have no way to determine what platform our Zappa handler will be executing on, we need to make sure that we only use portable packages. So, instead of using the default MySQL engine, we will instead need to use mysql-python-connector.

That means your app’s settings file will need an entry that looks like something this (notice the Engine field):

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': 'your_db_name',
        'USER': 'your_db_username',
        'PASSWORD': 'your_db_password',
        'HOST': 'your_db_name.your_db_id.us-east-1.rds.amazonaws.com',
        'PORT': '3306',
    }
}

At time of writing, there seems to be a problem with the Python MySQL connector when calling the initial ‘migrate’. You can remedy this by using the usual ‘django.db.backends.mysql’ for your initial migration from your local machine and just using ‘mysql.connector.django’ in your remote settings.

Currently, Zappa only supports MySQL and Aurora on RDS.

Middleware

Zappa requires special middleware for handling cookies, so in your remote settings file, you must include ZappaMiddleware as the first item in your MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
    'django_zappa.middleware.ZappaMiddleware',
    ...
)

Basic Usage

Initial Deployments

Once your settings are configured, you can package and deploy your Django application to an environment called ‘production’ with a single command:

$ python manage.py deploy production
Deploying..
Your application is now live at: https://7k6anj0k99.execute-api.us-east-1.amazonaws.com/production

And now your app is live! How cool is that?!

Updates

If your application has already been deployed and you only need to upload new Python code, but not touch the underlying routes, you can simply:

$ python manage.py update production
Updating..
Your application is now live at: https://7k6anj0k99.execute-api.us-east-1.amazonaws.com/production

Management

If you want to invoke Django management commands on the remote Zappa instance, you simply call the ‘invoke’ management command locally:

$ python manage.py invoke production check
System check identified no issues (0 silenced).

Advanced Usage

There are other settings that you can define in your ZAPPA_SETTINGS to change Zappa’s behavior. Use these at your own risk!

ZAPPA_SETTINGS = {
    'dev': {
        'aws_region': 'us-east-1', # AWS Region (default US East),
        'deploy_delay': 1, # Delay time while deploying, in seconds (default 1)
        'domain': 'yourapp.yourdomain.com', # Required if you're using a domain
        'http_methods': ['GET', 'POST'], # HTTP Methods to route,
        'integration_response_codes': [200, 301, 404, 500], # Integration response status codes to route
        'method_response_codes': [200, 301, 404, 500], # Method response status codes to route
        'parameter_depth': 10, # Size of URL depth to route. Defaults to 5.
        'role_name': "MyLambdaRole", # Lambda execution Role
        's3_bucket': 'dev-bucket', # Zappa zip bucket,
        'settings_file': '~/Projects/MyApp/settings/dev_settings.py', # Server side settings file location,
        'touch': False # GET the production URL upon initial deployment (default True)
    }
}

Let’s Encrypt SSL

Zappa has very basic support for Let’s Encrypt, but not automatic certificate updating.

There is also a bootstrapping problem here, as the ACME server will need to access your domain in order to verify that you own it, so you will have to create an initial self-signed certificate when you first configure a domain for use with API Gateway.

Then, you can generate your Let’s Encrypt challenge information using a local client or a service like GetHTTPSForFree.com.

Next, in your remote settings file, define the following entries (change these values, obviously):

LETS_ENCRYPT_CHALLENGE_PATH = "KkI_AMwzmQxlMDtaitt7eZMWEDn0t0Fsl5HjkJSPxyz"
LETS_ENCRYPT_CHALLENGE_CONTENT = "KkI_AMwzmQxlMDtaitt7eZMWEDn0t0Fsl5HjkJSPxyz.ABC5hET2fxMsBLCsQLlAVA5MLvYUnX8gEAYaXN0xI4Y"

Then, continue with the process and you should receive a valid Let’s Encrypt Certificate for your domain. Nice!

(When creating scheduled Lambda events via API is possible, this whole process may be wrapped into the ‘deploy’ command. Until then, you’re kind of on your own.)

Keeping the server warm

Lambda has a limitation that functions which aren’t called very often take longer to start - sometimes up to ten seconds. However, functions that are called regularly are cached and start quickly, usually in less than 50ms. To ensure that your servers are kept in a cached state, you can manually configure a scheduled task for your Zappa function that’ll keep the server cached by calling it every 5 minutes. There is currently no way to configure this through API, so you’ll have to set this up manually. When this ability is available via API, django-zappa will configure this automatically. It would be nice to also add support LetsEncrypt through this same mechanism.

TODO

This project is very young, so there is still plenty to be done. Contributions are more than welcome! Please file tickets before submitting patches, and submit your patches to the ‘dev’ branch.

Things that need work right now:

  • Testing!

  • Feedback!

  • Real documentation / website!

Metadata

Release files for django-zappa 0.10.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-zappa 0.10.1
File Size Uploaded Provenance Actions
django-zappa-0.10.1.tar.gz 12.2 kB Without provenance Details for django-zappa-0.10.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-zappa 0.10.1
File Interpreter ABI Platform Actions
django_zappa-0.10.1-py2-none-any.whl Python 2 none any Details for django_zappa-0.10.1-py2-none-any.whl

Total release size: 31.8 kB

Release files / django-zappa-0.10.1.tar.gz

Download URL django-zappa-0.10.1.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
bffffca72a7f4e589ac59e8336a127e27a8308ba8db381115b759dc8a9701e69
BLAKE2b-256 checksum
How to use checksums
f14463b5bad10bf9c377666e4ad073c483708d980d872b3bc34581ebe2d8babb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_zappa-0.10.1-py2-none-any.whl

Download URL django_zappa-0.10.1-py2-none-any.whl
Size 19.6 kB
Tags Python 2
SHA-256 checksum
How to use checksums
39439ea3fe02aae00be39880d2039aba42c289b6430d6ea48ddc091b2eadc292
BLAKE2b-256 checksum
How to use checksums
e461fcaff88b674f5d58c3212153e593637b943c25f8801b313a76aa4749180c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-zappa 0.10.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10.1 This release

2 release files No provenance

0.0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page