Serverless WSGI
Serverless WSGI is a Python library and Serverless Framework plugin that enables the deployment of standard Python WSGI applications (like Flask, Django, Pyramid) to AWS Lambda, using API Gateway as the HTTP frontend. It transparently converts API Gateway requests to WSGI requests and vice-versa, handling packaging and deployment complexities. The current version is 3.1.0, and it is actively maintained with regular updates.
Warnings
- breaking Version 3.0.0 introduced breaking changes for Serverless Framework integration, requiring Serverless Framework versions 2.32.0 or newer. Ensure your `serverless` CLI is updated.
- breaking Version 2.0.0 dropped Python 2 support and requires Werkzeug 2 or later. It also removed deprecated WSGI environment variables (`API_GATEWAY_AUTHORIZER`, `event`, `context`); these should be accessed via `serverless.authorizer`, `serverless.event`, and `serverless.context` respectively.
- breaking With Werkzeug 3.0.0 (a dependency of `serverless-wsgi`), `werkzeug.urls.url_encode` is no longer available. If your application directly uses this Werkzeug function, you'll need to migrate to `urllib` counterparts.
- gotcha When deploying with the Serverless Framework plugin, the handler `wsgi.handler` was renamed to `wsgi_handler.handler` in version 1.7.0. While the old name is still supported, using `wsgi_handler.handler` is the recommended and up-to-date practice.
- gotcha Binary data (e.g., images, fonts) might be incorrectly base64 encoded by `serverless-wsgi` if the corresponding MIME types are not explicitly added to `binaryMimeTypes` in your API Gateway configuration (typically via `serverless.yml`).
- gotcha AWS Lambda has a deployment package size limit (50MB compressed, 250MB uncompressed). Large applications or those with many dependencies might exceed this, especially if development artifacts or unnecessary files are included.
Install
-
pip install serverless-wsgi -
npm install serverless-wsgi
Imports
- handle_request
import serverless_wsgi def lambda_handler(event, context): from your_app_module import app # Your WSGI application instance return serverless_wsgi.handle_request(app, event, context)
Quickstart
import os
import serverless_wsgi
from flask import Flask, jsonify
# Your Flask (or any WSGI) application
app = Flask(__name__)
@app.route('/')
def hello():
return jsonify(message='Hello from Serverless WSGI!')
@app.route('/env')
def show_env():
return jsonify(environment=dict(os.environ))
# The AWS Lambda handler function
def lambda_handler(event, context):
# This is the core function from the serverless-wsgi Python library
# that maps API Gateway events to your WSGI application.
return serverless_wsgi.handle_request(app, event, context)
# Example of how you would run it locally (for testing)
if __name__ == '__main__':
# In a real setup, this is handled by `sls wsgi serve` or a WSGI server.
# For a minimal local test, you could use Werkzeug's run_simple.
from werkzeug.serving import run_simple
print("Running local Flask app on http://127.0.0.1:5000/")
run_simple('127.0.0.1', 5000, app, use_reloader=True, use_debugger=True)