Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

Serverless WSGI With AWS Lambda + API Gateway

Project description

<p align="center">
<img src="" alt="Zappa Rocks!"/>

## Zappa - Serverless Python Web Services

[![Build Status](](
[![Requirements Status](](

**Zappa** makes it super easy to deploy all Python WSGI applications on AWS Lambda + API Gateway. Think of it as "serverless" web hosting for your Python web apps.

> What do you mean "serverless"?

Okay, so there still is a server - but it only has a _40 millisecond_ life cycle! Serverless in this case means **"without any permanent infrastucture."**

With a traditional HTTP server, the server is online 24/7, processing requests one by one as they come in. If the queue of incoming requests grows too large, some requests will time out. With Zappa, **each request is given its own virtual HTTP "server"** by Amazon API Gateway. AWS handles the horizontal scaling automatically, so no requests ever time out. Each request then calls your application from a memory cache in AWS Lambda and returns the response via Python's WSGI interface. After your app returns, the "server" dies.

Better still, with Zappa you only pay for the milliseconds of server time that you use, so it's many **orders of magnitude cheaper** than VPS/PaaS hosts like Linode or Heroku - and in most cases, it's completely free. Plus, there's no need to worry about load balancing or keeping servers online ever again.

It's great for deploying serverless microservices with frameworks like Flask and Bottle, and for hosting larger web apps and CMSes with Django. Or, you can use any WSGI-compatible app you like! You **probably don't need to change your existing applications** to use it, and you're not locked into using it.

And finally, Zappa is **super easy to use**. You can deploy your application with a single command out of the box.

Using **Zappa** 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!


<p align="center">
<img src="" alt="Zappa Demo Gif"/>

## Installation and Configuration

_Before you begin, make sure you have a valid AWS account and your [AWS credentials file]( is properly installed._

**Zappa** can easily be installed through pip, like so:

$ pip install zappa

If you're looking for Django-specific integration, you should probably check out _**[django-zappa](**_ instead.

Next, you'll need to define your local and server-side settings.

#### Settings

Next, you'll need to define a few settings for your Zappa deployment environments in a file named *zappa_settings.json* in your project directory. The simplest example is:

"dev": { // The name of your environment
"s3_bucket": "lmbda", // The name of your S3 bucket
"app_function": "" // The python path to your WSGI application function. In Flask, this is your 'app' object.

You can define as many environments as your like - we recommend having _dev_, _staging_, and _production_.

Now, you're ready to deploy!

## Basic Usage

#### Initial Deployments

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

$ zappa deploy production
Your application is now live at:

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

To expain what's going on, when you call 'deploy', Zappa will automatically package up your application and local virtual environment into a Lambda-compatible archive, replace any dependencies with versions [precompiled for Lambda](, set up the function handler and necessary WSGI Middleware, upload the archive to S3, register it as a new Lambda function, create a new API Gateway resource, create WSGI-compatible routes for it, link it to the new Lambda function, and finally delete the archive from your S3 bucket. Handy!

#### 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:

$ zappa update production
Your application is now live at:

This creates a new archive, uploads it to S3 and updates the Lambda function to use the new code, but doesn't touch the API Gateway routes.

#### Rollback

You can also rollback the deployed code to a previous version by supplying the number of revisions to return to. For instance, to rollback to the version deployed 3 versions ago:

$ zappa rollback production -n 3

#### Tailing Logs

You can watch the logs of a deployment by calling the "tail" management command.

$ zappa tail production

## Advanced Usage

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

"dev": {
"aws_region": "us-east-1", // AWS Region (default US East),
"debug": true // Print Zappa configuration errors tracebacks in the 500
"delete_zip": true // Delete the local zip archive after code updates
"domain": "", // Required if you're using a domain
"exclude": ["*.gz", "*.pem"], // A list of regex patterns to exclude from the archive
"http_methods": ["GET", "POST"], // HTTP Methods to route,
"integration_response_codes": [200, 301, 404, 500], // Integration response status codes to route
"memory_size": 512, // Lambda function memory in MB
"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.
"prebuild_script": "your_module.your_function", // Function to execute before uploading code
"role_name": "MyLambdaRole", // Lambda execution Role
"s3_bucket": "dev-bucket", // Zappa zip bucket,
"settings_file": "~/Projects/MyApp/settings/", // Server side settings file location,
"touch": false, // GET the production URL upon initial deployment (default True)
"use_precompiled_packages": false, // If possible, use C-extension packages which have been pre-compiled for AWS Lambda
"vpc_config": { // Optional VPC configuration for Lambda function
"SubnetIds": [ "subnet-12345678" ], // Note: not all availability zones support Lambda!
"SecurityGroupIds": [ "sg-12345678" ]

#### 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, Zappa will configure this automatically. It would be nice to also add support LetsEncrypt through this same mechanism.

#### Enabling CORS

To enable Cross-Origin Resource Sharing (CORS) for your application, follow the [AWS "How to CORS" Guide]( to enable CORS via the API Gateway Console. Don't forget to re-deploy your API after making the changes!

#### Deploying to a Domain With a Let's Encrypt Certificate

If you want to use Zappa on a domain with a free Let's Encrypt certificate, you can follow [this guide](

## Zappa Guides

* [Django-Zappa tutorial screencast](
* [Using Django-Zappa, Part 1](
* [Using Django-Zappa, Part 2: VPCs](
* [Building Serverless Microservices with Zappa and Flask](
* _Your guide here?_

## Zappa in the Press

* _[Zappa Serves Python, Minus the Servers](
* _[Zappa lyfter serverlösa applikationer med Python](
* _[Interview: Rich Jones on Zappa](

## Sites Using Zappa

* []( - A Zappa "Hello, World" (real homepage coming.. soon..)
* []( - Spheres, a photosphere host and viewer
* [Mailchimp Signup Utility]( - A microservice for adding people to a mailing list via API.
* [Serverless Image Host]( - A thumbnailing service with Flask, Zappa and Pillow.
* Your site here?

## Hacks

Zappa goes quite far beyond what Lambda and API Gateway were ever intended to handle. As a result, there are quite a few hacks in here that allow it to work. Some of those include, but aren't limited to..

* Using VTL to map body, headers, method, params and query strings into JSON, and then turning that into valid WSGI.
* Attaching response codes to response bodies, Base64 encoding the whole thing, using that as a regex to route the response code, decoding the body in VTL, and mapping the response body to that.
* Packing and _Base58_ encoding multiple cookies into a single cookie because we can only map one kind.
* Turning cookie-setting 301/302 responses into 200 responses with HTML redirects, because we have no way to set headers on redirects.

## Contributing

This project is still 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.

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
zappa-0.17.2-py2-none-any.whl (32.5 kB) Copy SHA256 hash SHA256 Wheel 2.7
zappa-0.17.2.tar.gz (27.9 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page