{"id":13701300,"url":"https://github.com/logandk/serverless-wsgi","last_synced_at":"2025-05-14T06:12:33.121Z","repository":{"id":11920369,"uuid":"70798890","full_name":"logandk/serverless-wsgi","owner":"logandk","description":"Serverless plugin to deploy WSGI applications (Flask/Django/Pyramid etc.) and bundle Python packages","archived":false,"fork":false,"pushed_at":"2024-10-22T08:28:22.000Z","size":824,"stargazers_count":499,"open_issues_count":6,"forks_count":88,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-11T00:49:32.640Z","etag":null,"topics":["aws-lambda","flask","hacktoberfest","pyramid","python","serverless","serverless-framework","serverless-plugin","wsgi"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/logandk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-13T11:16:31.000Z","updated_at":"2025-03-19T20:12:45.000Z","dependencies_parsed_at":"2023-01-13T16:43:26.924Z","dependency_job_id":"54532bcc-69a0-4bf3-8de1-7dcbb7f85a81","html_url":"https://github.com/logandk/serverless-wsgi","commit_stats":{"total_commits":328,"total_committers":41,"mean_commits":8.0,"dds":0.3597560975609756,"last_synced_commit":"61775fa4e1e12bd08d0b1209fcfaba5773e8f144"},"previous_names":[],"tags_count":46,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logandk%2Fserverless-wsgi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logandk%2Fserverless-wsgi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logandk%2Fserverless-wsgi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logandk%2Fserverless-wsgi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/logandk","download_url":"https://codeload.github.com/logandk/serverless-wsgi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254080580,"owners_count":22011446,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws-lambda","flask","hacktoberfest","pyramid","python","serverless","serverless-framework","serverless-plugin","wsgi"],"created_at":"2024-08-02T20:01:27.395Z","updated_at":"2025-05-14T06:12:33.064Z","avatar_url":"https://github.com/logandk.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://logandk.github.io/serverless-wsgi/assets/header.svg\"\u003e\n\u003c/p\u003e\n\n[![npm package](https://nodei.co/npm/serverless-wsgi.svg?downloads=true\u0026downloadRank=true\u0026stars=true)](https://nodei.co/npm/serverless-wsgi/)\n\n[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com)\n[![Tests](https://github.com/logandk/serverless-wsgi/actions/workflows/tests.yaml/badge.svg)](https://github.com/logandk/serverless-wsgi/actions/workflows/tests.yaml)\n[![Coverage Status](https://codecov.io/gh/logandk/serverless-wsgi/branch/master/graph/badge.svg)](https://codecov.io/gh/logandk/serverless-wsgi)\n\nA Serverless Framework plugin to build your deploy Python WSGI applications using Serverless. Compatible\nWSGI application frameworks include Flask, Django and Pyramid - for a complete list, see:\nhttp://wsgi.readthedocs.io/en/latest/frameworks.html.\n\n### Features\n\n- Transparently converts API Gateway and ALB requests to and from standard WSGI requests\n- Supports anything you'd expect from WSGI such as redirects, cookies, file uploads etc.\n- Automatically downloads Python packages that you specify in `requirements.txt` and deploys them along with your application\n- Convenient `wsgi serve` command for serving your application locally during development\n- Includes CLI commands for remote execution of Python code (`wsgi exec`), shell commands (`wsgi command`), Flask CLI commands (`wsgi flask`) and Django management commands (`wsgi manage`)\n- Supports both APIGatewayV1 and APIGatewayV2 payloads\n\n## Install\n\n```\nsls plugin install -n serverless-wsgi\n```\n\nThis will automatically add the plugin to `package.json` and the plugins section of `serverless.yml`.\n\n## Flask configuration example\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://logandk.github.io/serverless-wsgi/assets/hello-world.svg\"\u003e\n\u003c/p\u003e\n\nThis example assumes that you have intialized your application as `app` inside `api.py`.\n\n```\nproject\n├── api.py\n├── requirements.txt\n└── serverless.yml\n```\n\n### api.py\n\nA regular Flask application.\n\n```python\nfrom flask import Flask\napp = Flask(__name__)\n\n\n@app.route(\"/cats\")\ndef cats():\n    return \"Cats\"\n\n\n@app.route(\"/dogs/\u003cid\u003e\")\ndef dog(id):\n    return \"Dog\"\n```\n\n### serverless.yml\n\nLoad the plugin and set the `custom.wsgi.app` configuration in `serverless.yml` to the\nmodule path of your Flask application.\n\nAll functions that will use WSGI need to have `wsgi_handler.handler` set as the Lambda handler and\nuse the default `lambda-proxy` integration for API Gateway. This configuration example treats\nAPI Gateway as a transparent proxy, passing all requests directly to your Flask application,\nand letting the application handle errors, 404s etc.\n\n_Note: The WSGI handler was called `wsgi.handler` earlier, but was renamed to `wsgi_handler.handler`\nin `1.7.0`. The old name is still supported but using it will cause a deprecation warning._\n\n```yaml\nservice: example\n\nprovider:\n  name: aws\n  runtime: python3.6\n\nplugins:\n  - serverless-wsgi\n\nfunctions:\n  api:\n    handler: wsgi_handler.handler\n    events:\n      - http: ANY /\n      - http: ANY /{proxy+}\n\ncustom:\n  wsgi:\n    app: api.app\n```\n\n### requirements.txt\n\nAdd Flask to the application bundle.\n\n```\nFlask==1.0.2\n```\n\n## Deployment\n\nSimply run the serverless deploy command as usual:\n\n```\n$ sls deploy\nServerless: Using Python specified in \"runtime\": python3.6\nServerless: Packaging Python WSGI handler...\nServerless: Packaging required Python packages...\nServerless: Linking required Python packages...\nServerless: Packaging service...\nServerless: Excluding development dependencies...\nServerless: Unlinking required Python packages...\nServerless: Uploading CloudFormation file to S3...\nServerless: Uploading artifacts...\nServerless: Uploading service .zip file to S3 (864.57 KB)...\nServerless: Validating template...\nServerless: Updating Stack...\nServerless: Checking Stack update progress...\n..............\nServerless: Stack update finished...\n```\n\n## Other frameworks\n\nSet `custom.wsgi.app` in `serverless.yml` according to your WSGI callable:\n\n- For Pyramid, use [make_wsgi_app](http://docs.pylonsproject.org/projects/pyramid/en/latest/api/config.html#pyramid.config.Configurator.make_wsgi_app) to intialize the callable\n- Django is configured for WSGI by default, set the callable to `\u003cproject_name\u003e.wsgi.application`. See https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ for more information.\n\n## Usage\n\n### Automatic requirement packaging\n\nYou'll need to include any packages that your application uses in the bundle\nthat's deployed to AWS Lambda. This plugin helps you out by doing this automatically,\nas long as you specify your required packages in a `requirements.txt` file in the root\nof your Serverless service path:\n\n```\nFlask==1.0.2\nrequests==2.21.0\n```\n\nFor more information, see https://pip.pypa.io/en/latest/user_guide/#requirements-files.\n\nThe `serverless-wsgi` plugin itself depends on `werkzeug` and will package it automatically,\neven if `werkzeug` is not present in your `requirements.txt`.\n\nYou can use the requirement packaging functionality of _serverless-wsgi_ without the WSGI\nhandler itself by including the plugin in your `serverless.yml` configuration, without specifying\nthe `custom.wsgi.app` setting. This will omit the WSGI handler from the package, but include\nany requirements specified in `requirements.txt`.\n\nIf you don't want to use automatic requirement packaging you can set `custom.wsgi.packRequirements` to false:\n\n```yaml\ncustom:\n  wsgi:\n    app: api.app\n    packRequirements: false\n```\n\nIn order to pass additional arguments to `pip` when installing requirements, the `pipArgs` configuration\noption is available:\n\n```yaml\ncustom:\n  wsgi:\n    app: api.app\n    pipArgs: --no-deps\n```\n\nFor a more advanced approach to packaging requirements, consider using https://github.com/UnitedIncome/serverless-python-requirements.\nWhen the `serverless-python-requirements` is added to `serverless.yml`, the `packRequirements` option\nis set to `false` by default.\n\nIf you have `packRequirements` set to `false`, or if you use `serverless-python-requirements`, remember to add\n`werkzeug` explicitly in your `requirements.txt`.\n\n### Python version\n\nPython is used for packaging requirements and serving the app when invoking `sls wsgi serve`. By\ndefault, the current runtime setting is expected to be the name of the Python binary in `PATH`,\nfor instance `python3.6`. If this is not the name of your Python binary, override it using the\n`pythonBin` option:\n\n```yaml\ncustom:\n  wsgi:\n    app: api.app\n    pythonBin: python3\n```\n\n### Local server\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://logandk.github.io/serverless-wsgi/assets/serve.svg\"\u003e\n\u003c/p\u003e\n\nFor convenience, a `sls wsgi serve` command is provided to run your WSGI application\nlocally. This command requires the `werkzeug` Python package to be installed,\nand acts as a simple wrapper for starting werkzeug's built-in HTTP server.\n\nBy default, the server will start on port 5000.\n(Note: macOS [reserves port 5000](https://twitter.com/mitsuhiko/status/1462734023164416009)\nfor AirPlay by default, see below for instructions on changing the port.)\n\n```\n$ sls wsgi serve\n * Running on http://localhost:5000/ (Press CTRL+C to quit)\n * Restarting with stat\n * Debugger is active!\n```\n\nConfigure the port using the `-p` parameter:\n\n```\n$ sls wsgi serve -p 8000\n * Running on http://localhost:8000/ (Press CTRL+C to quit)\n * Restarting with stat\n * Debugger is active!\n```\n\nWhen running locally, an environment variable named `IS_OFFLINE` will be set to `True`.\nSo, if you want to know when the application is running locally, check `os.environ[\"IS_OFFLINE\"]`.\n\n### Remote command execution\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://logandk.github.io/serverless-wsgi/assets/command.svg\"\u003e\n\u003c/p\u003e\n\nThe `wsgi exec` command lets you execute Python code remotely:\n\n```\n$ sls wsgi exec -c \"import math; print((1 + math.sqrt(5)) / 2)\"\n1.618033988749895\n\n$ cat count.py\nfor i in range(3):\n    print(i)\n\n$ sls wsgi exec -f count.py\n0\n1\n2\n```\n\nThe `wsgi command` command lets you execute shell commands remotely:\n\n```\n$ sls wsgi command -c \"pwd\"\n/var/task\n\n$ cat script.sh\n#!/bin/bash\necho \"dlrow olleh\" | rev\n\n$ sls wsgi command -f script.sh\nhello world\n```\n\nThe `wsgi flask` command lets you execute [Flask CLI custom commands](http://flask.pocoo.org/docs/latest/cli/#custom-commands) remotely:\n\n```\n$ sls wsgi flask -c \"my command\"\nHello world!\n```\n\nThe `wsgi manage` command lets you execute Django management commands remotely:\n\n```\n$ sls wsgi manage -c \"check --list-tags\"\nadmin\ncaches\ndatabase\nmodels\nstaticfiles\ntemplates\nurls\n```\n\nAll commands have `local` equivalents that let you run commands through `sls invoke local` rather\nthan `sls invoke`, i.e. on the local machine instead of through Lambda. The `local` commands (`sls wsgi command local`,\n`sls wsgi exec local`, `sls wsgi flask local` and `sls wsgi manage local`) take the same arguments\nas their remote counterparts documented above.\n\n### Explicit routes\n\nIf you'd like to be explicit about which routes and HTTP methods should pass through to your\napplication, see the following example:\n\n```yaml\nservice: example\n\nprovider:\n  name: aws\n  runtime: python3.6\n\nplugins:\n  - serverless-wsgi\n\nfunctions:\n  api:\n    handler: wsgi_handler.handler\n    events:\n      - http:\n          path: cats\n          method: get\n          integration: lambda-proxy\n      - http:\n          path: dogs/{id}\n          method: get\n          integration: lambda-proxy\n\ncustom:\n  wsgi:\n    app: api.app\n```\n\n### Custom domain names\n\nIf you use custom domain names with API Gateway, you might have a base path that is\nat the beginning of your path, such as the stage (`/dev`, `/stage`, `/prod`). In this case, set\nthe `API_GATEWAY_BASE_PATH` environment variable to let `serverless-wsgi` know.\nE.g, if you deploy your WSGI application to https://mydomain.com/api/myservice,\nset `API_GATEWAY_BASE_PATH` to `api/myservice` (no `/` first).\n\nThe example below uses the [serverless-domain-manager](https://github.com/amplify-education/serverless-domain-manager)\nplugin to handle custom domains in API Gateway:\n\n```yaml\nservice: example\n\nprovider:\n  name: aws\n  runtime: python3.6\n  environment:\n    API_GATEWAY_BASE_PATH: ${self:custom.customDomain.basePath}\n\nplugins:\n  - serverless-wsgi\n  - serverless-domain-manager\n\nfunctions:\n  api:\n    handler: wsgi_handler.handler\n    events:\n      - http: ANY /\n      - http: ANY {proxy+}\n\ncustom:\n  wsgi:\n    app: api.app\n  customDomain:\n    basePath: ${opt:stage}\n    domainName: mydomain.name.com\n    stage: ${opt:stage}\n    createRoute53Record: true\n```\n\n**Note**: The **API_GATEWAY_BASE_PATH** configuration is only needed when using the payload V1. In the V2, the path does not have the **basePath** in the beginning.\n\n### Using CloudFront\n\nIf you're configuring CloudFront manually in front of your API and setting\nthe Path in the CloudFront Origin to include your stage name, you'll need\nto strip it out from the path supplied to WSGI. This is so that your app\ndoesn't generate URLs starting with `/production`.\n\nPass the `STRIP_STAGE_PATH=yes` environment variable to your application\nto set this:\n\n```yaml\nservice: example\n\nprovider:\n  name: aws\n  runtime: python3.6\n  environment:\n    STRIP_STAGE_PATH: yes\n```\n\n### File uploads\n\nIn order to accept file uploads from HTML forms, make sure to add `multipart/form-data` to\nthe list of content types with _Binary Support_ in your API Gateway API. The\n[serverless-apigw-binary](https://github.com/maciejtreder/serverless-apigw-binary)\nServerless plugin can be used to automate this process.\n\nKeep in mind that, when building Serverless applications, uploading\n[directly to S3](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingHTTPPOST.html)\nfrom the browser is usually the preferred approach.\n\n### Raw context and event\n\nThe raw context and event from AWS Lambda are both accessible through the WSGI\nrequest. The following example shows how to access them when using Flask:\n\n```python\nfrom flask import Flask, request\napp = Flask(__name__)\n\n\n@app.route(\"/\")\ndef index():\n    print(request.environ['serverless.context'])\n    print(request.environ['serverless.event'])\n```\n\nFor more information on these objects, read the documentation on [events](https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html)\nand the [invocation context](https://docs.aws.amazon.com/lambda/latest/dg/python-context.html).\n\n### Text MIME types\n\nBy default, all MIME types starting with `text/` and the following whitelist are sent\nthrough API Gateway in plain text. All other MIME types will have their response body\nbase64 encoded (and the `isBase64Encoded` API Gateway flag set) in order to be\ndelivered by API Gateway as binary data (remember to add any binary MIME types that\nyou're using to the _Binary Support_ list in API Gateway).\n\nThis is the default whitelist of plain text MIME types:\n\n- `application/json`\n- `application/javascript`\n- `application/xml`\n- `application/vnd.api+json`\n- `image/svg+xml`\n\nIn order to add additional plain text MIME types to this whitelist, use the\n`textMimeTypes` configuration option:\n\n```yaml\ncustom:\n  wsgi:\n    app: api.app\n    textMimeTypes:\n      - application/custom+json\n      - application/vnd.company+json\n```\n\n### Preventing cold starts\n\nCommon ways to keep lambda functions warm include [scheduled events](https://serverless.com/framework/docs/providers/aws/events/schedule/)\nand the [WarmUP plugin](https://github.com/FidelLimited/serverless-plugin-warmup). Both these event sources\nare supported by default and will be ignored by `serverless-wsgi`.\n\n### Alternative directory structure\n\nIf you have several functions in `serverless.yml` and want to organize them in\ndirectories, e.g.:\n\n```\nproject\n├── web\n│   ├── api.py\n│   └── requirements.txt\n├── serverless.yml\n└── another_function.py\n```\n\nIn this case, tell `serverless-wsgi` where to find the handler by prepending the\ndirectory:\n\n```yaml\nservice: example\n\nprovider:\n  name: aws\n  runtime: python3.6\n\nplugins:\n  - serverless-wsgi\n\nfunctions:\n  api:\n    handler: wsgi_handler.handler\n    events:\n      - http: ANY /\n      - http: ANY {proxy+}\n\n  another_function:\n    handler: another_function.handler\n\ncustom:\n  wsgi:\n    app: web/api.app\n```\n\nRequirements will now be installed into `web/`, rather than at in the service root directory.\n\nThe same rule applies when using the `individually: true` flag in the `package` settings, together\nwith the `module` option provided by `serverless-python-requirements`. In that case, both the requirements\nand the WSGI handler will be installed into `web/`, if the function is configured with `module: \"web\"`.\n\n## Usage without Serverless\n\nThe AWS API Gateway to WSGI mapping module is available on PyPI in the\n`serverless-wsgi` package.\n\nUse this package if you need to deploy Python Lambda functions to handle\nAPI Gateway events directly, without using the Serverless framework.\n\n```\npip install serverless-wsgi\n```\n\nInitialize your WSGI application and in your Lambda event handler, call\nthe request mapper:\n\n```python\nimport app  # Replace with your actual application\nimport serverless_wsgi\n\n# If you need to send additional content types as text, add then directly\n# to the whitelist:\n#\n# serverless_wsgi.TEXT_MIME_TYPES.append(\"application/custom+json\")\n\ndef handler(event, context):\n    return serverless_wsgi.handle_request(app.app, event, context)\n```\n\n# Thanks\n\nThanks to [Zappa](https://github.com/Miserlou/Zappa), which has been both the\ninspiration and source of several implementations that went into this project.\n\nThanks to [chalice](https://github.com/awslabs/chalice) for the\nrequirement packaging implementation.\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flogandk%2Fserverless-wsgi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flogandk%2Fserverless-wsgi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flogandk%2Fserverless-wsgi/lists"}