AWS WSGI Adapter for Lambda/API Gateway
aws-wsgi is a WSGI adapter that enables the use of WSGI-compatible Python frameworks (like Flask and Django) with AWS API Gateway's Lambda Proxy Integration. The project's GitHub repository was archived in March 2024, and its last release on PyPI was in July 2020. Users are encouraged to consider actively maintained forks like `awsgi2` or alternative solutions for new projects.
Common errors
-
Unable to import module 'lambda_function': No module named 'awsgi'
cause The `aws-wsgi` library or its dependencies are not correctly packaged or deployed within the Lambda environment, or the import path is incorrect.fixEnsure `aws-wsgi` is included in your Lambda deployment package (e.g., in a layer or directly in the zip file). When using `pip`, install to a specific target directory: `pip install aws-wsgi -t ./python` and then zip the `python` directory. -
500 Internal Server Error: 'wsgi.input' is not available
cause This generic WSGI error often indicates a malformed API Gateway event or an issue with how the adapter is attempting to read the request body, possibly due to unexpected content types or missing data.fixVerify the incoming API Gateway event structure matches the expected Lambda Proxy Integration format. Ensure clients are sending valid request bodies, especially for POST/PUT requests, and that appropriate `Content-Type` headers are set.
Warnings
- breaking The official `slank/awsgi` GitHub repository has been archived since March 13, 2024, and is now read-only. This indicates that the original project is no longer actively maintained.
- gotcha API Gateway requires proper `binaryMimeTypes` configuration to handle binary data (e.g., images, fonts). Without this, binary content might be base64 encoded and sent as text, leading to display issues in the client.
- gotcha AWS Lambda has a 6MB payload limit for requests. Directly uploading large files via API Gateway and Lambda will hit this limit.
- gotcha WSGI applications deployed to AWS Lambda require explicit IAM permissions for the Lambda execution role to interact with other AWS services (e.g., S3, DynamoDB).
Install
-
pip install aws-wsgi
Imports
- awsgi
import awsgi
Quickstart
import awsgi
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/')
def index():
return jsonify({
'message': 'Hello from aws-wsgi!',
'path': request.path,
'method': request.method
})
def lambda_handler(event, context):
return awsgi.response(app, event, context)
# To test locally (e.g., with 'python -m flask run')
if __name__ == '__main__':
app.run(debug=True)