Package: apig-wsgi
Wrap aWSGIapplication in an AWS Lambda handler function for running on API Gateway or an ALB.A quick example:fromapig_wsgiimportmake_lambda_handlerfrommyapp.wsgiimportapp# Configure this as your entry point in AWS Lambdalambda_handler=make_lambda_handler(app)InstallationUsepip:python-mpipinstallapig-wsgiPython 3.8 to 3.12 supported.Working on a Django project?Improve your skills withone of my books.UsageUse apig-wsgi in your AWS Lambda Function that you attach to one of:ALambda Function URLAnAPI Gateway “HTTP API”AnAPI Gateway “REST API”AnALBBoth “format version 1” and “format version 2” are supported
(documentation).
apig-wsgi will automatically detect the version in use.
At time of writing, “format version 2” is used for Lambda Function URL’s and API Gateway HTTP API’s.make_lambda_handler(app, binary_support=None, non_binary_content_type_prefixes=None)appshould be a WSGI app, for example from Django’swsgi.pyor Flask’sFlask()object.binary_supportconfigures whether responses containing binary are
supported. The default,None, means to automatically detect this from the
format version of the event - on it defaults toTruefor format version 2,
andFalsefor format version 1. Depending on how you’re deploying your
lambda function, you may need extra configuration before you can enable binary
responses:ALB’s support binary responses by default.API Gateway HTTP API’s support binary responses by default (and default to
event format version 2).API Gateway REST API’s (the “old” style) require you to add'*/*'in the
“binary media types” configuration. You will need to configure this through
API Gateway directly, CloudFormation, SAM, or whatever tool your project is
using. Whilst this supports a list of binary media types, using'*/*'is
the best way to configure it, since it is used to match the request ‘Accept’
header as well, which WSGI applications often ignore. You may need to delete
and recreate your stages for this value to be copied over.Note that binary responses aren’t sent if your response has a ‘Content-Type’
starting ‘text/’, ‘application/json’ or ‘application/vnd.api+json’ - this
is to support sending larger text responses, since the base64 encoding would
otherwise inflate the content length. To avoid base64 encoding other content
types, you can setnon_binary_content_type_prefixesto a list or tuple of
content type prefixes of your choice (which replaces the default list).If the event from API Gateway contains therequestContextkey, for example
on format version 2 or from custom request authorizers, this will be available
in the WSGI environ at the keyapig_wsgi.request_context.If you want to inspect the full event from API Gateway, it’s available in the
WSGI environ at the keyapig_wsgi.full_event.If you need theLambda Context object,
it’s available in the WSGI environ at the keyapig_wsgi.context.If you’re using “format version 1”, multiple values for request and response
headers and query parameters are supported. They are enabled automatically on
API Gateway but needexplict activation on
ALB’s.
If you need to determine from within your application if multiple header values
are enabled, you can can check theapgi_wsgi.multi_value_headerskey in the
WSGI environ, which isTrueif they are enabled andFalseotherwise.ExampleAn example Django project with Ansible deployment is provided in theexample/directory in the repository.
See theREADME.rstthere for guidance.