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.
pip install serverless-wsgiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate `serverless-wsgi` with a simple Flask application for direct use within an AWS Lambda function. The `lambda_handler` function serves as the entry point, passing the AWS event and context to `serverless_wsgi.handle_request`. For actual deployment, the `serverless-wsgi` plugin for the Serverless Framework is typically used, which abstracts away the manual `lambda_handler` setup by configuring `wsgi_handler.handler` in your `serverless.yml`.
Upgrade your Serverless Framework CLI: `npm install -g serverless` (or `npm install serverless@^2.32.0` if you need to pin to a specific 2.x version).
Ensure your Lambda runtime is Python 3.6+ and that Werkzeug 2.0+ is installed. Update your application code to use the new `serverless.*` variables.
Replace `werkzeug.urls.url_encode` calls in your application with functions from Python's standard `urllib.parse` module, such as `urllib.parse.urlencode`.
Update your `serverless.yml` to specify `handler: wsgi_handler.handler` for your Lambda functions.
Configure API Gateway to include the necessary `binaryMimeTypes` in your `serverless.yml` to ensure proper decoding of binary responses. You can also extend `serverless_wsgi.TEXT_MIME_TYPES` if you have custom text types that should not be base64 encoded.
Use a `.serverlessignore` file or the `package.exclude` options in `serverless.yml` to omit large or unnecessary files. Consider using `serverless-python-requirements` with `dockerizePip: true` for complex C-extension dependencies to reduce package size.
Ensure your `wsgi.py` file is located in the root directory of your project or explicitly included in your `serverless.yml`'s `package.include` configuration.
Install and configure the `serverless-python-requirements` plugin in your `serverless.yml` to automatically bundle dependencies from your `requirements.txt` file. For cross-platform development, ensure `dockerizePip: true` is set.
Check your AWS CloudWatch logs for the Lambda function associated with your `serverless-wsgi` endpoint; the detailed Python traceback will pinpoint the exact cause of the unhandled exception in your application code.
Verify that the `wsgi.app` value in `serverless.yml` accurately maps to the actual WSGI application instance (e.g., `app = Flask(__name__)` as `app.app`) within your Python files, ensuring exact matching of names.