|
1 |
| -# Serverless WSGI |
| 1 | +# Serverless Python Requirements |
2 | 2 |
|
3 | 3 | [](http://www.serverless.com)
|
4 | 4 |
|
5 |
| -A Serverless v1.0 plugin to build your deploy Python WSGI applications using Serverless. Compatible |
6 |
| -WSGI application frameworks include Flask, Django and Pyramid - for a complete list, see: |
7 |
| -[http://wsgi.readthedocs.io/en/latest/frameworks.html](http://wsgi.readthedocs.io/en/latest/frameworks.html). |
8 |
| - |
9 |
| -### Features |
10 |
| - |
11 |
| -* Transparently converts API Gateway requests to and from standard WSGI requests |
12 |
| -* Supports anything you'd expect from WSGI such as redirects, cookies, file uploads etc. |
13 |
| -* Automatically downloads Python packages that you specify in `requirements.txt` and deploys them along with your application |
14 |
| -* Convenient `wsgi serve` command for serving your application locally during development |
| 5 | +A Serverless v1.0 plugin to automatically bundle dependencies from |
| 6 | +`requirements.txt`. |
15 | 7 |
|
16 | 8 |
|
17 | 9 | ## Install
|
18 | 10 |
|
19 | 11 | ```
|
20 |
| -npm install --save serverless-wsgi |
| 12 | +npm install --save serverless-python-requirements |
21 | 13 | ```
|
22 | 14 |
|
23 |
| -Add the plugin to your `serverless.yml` file and set the WSGI application: |
| 15 | +Add the plugin to your `serverless.yml`: |
24 | 16 |
|
25 | 17 | ```yaml
|
26 | 18 | plugins:
|
27 |
| - - serverless-wsgi |
28 |
| -``` |
29 |
| -
|
30 |
| -
|
31 |
| -## Flask configuration example |
32 |
| -
|
33 |
| -This example assumes that you have intialized your application as `app` inside `api.py`. |
34 |
| - |
35 |
| -``` |
36 |
| -project |
37 |
| -├── api.py |
38 |
| -├── requirements.txt |
39 |
| -└── serverless.yml |
| 19 | + - serverless-python-requirements |
40 | 20 | ```
|
41 | 21 |
|
42 |
| -### api.py |
43 | 22 |
|
44 |
| -A regular Flask application. |
| 23 | +## Adding the dependencies to `sys.path` |
45 | 24 |
|
| 25 | +`serverless-python-requirements` adds a module called `requirements` to your |
| 26 | +puck. To easily make the bundled dependencies available, simply import it. Eg. |
| 27 | +add this to the top of any file using dependencies specified in your |
| 28 | +`requirements.txt`: |
46 | 29 | ```python
|
47 |
| -from flask import Flask |
48 |
| -app = Flask(__name__) |
49 |
| -
|
50 |
| -
|
51 |
| -@app.route("/cats") |
52 |
| -def cats(): |
53 |
| - return "Cats" |
54 |
| -
|
55 |
| -
|
56 |
| -@app.route("/dogs/<id>") |
57 |
| -def dog(id): |
58 |
| - return "Dog" |
59 |
| -``` |
60 |
| - |
61 |
| -### serverless.yml |
62 |
| - |
63 |
| -Load the plugin and set the `custom.wsgi.app` configuration in `serverless.yml` to the |
64 |
| -module path of your Flask application. |
65 |
| - |
66 |
| -All functions that will use WSGI need to have `wsgi.handler` set as the Lambda handler and |
67 |
| -use the `lambda-proxy` integration for API Gateway. |
68 |
| - |
69 |
| -```yaml |
70 |
| -service: example |
71 |
| - |
72 |
| -provider: |
73 |
| - name: aws |
74 |
| - runtime: python2.7 |
75 |
| - |
76 |
| -plugins: |
77 |
| - - serverless-wsgi |
78 |
| - |
79 |
| -functions: |
80 |
| - api: |
81 |
| - handler: wsgi.handler |
82 |
| - events: |
83 |
| - - http: |
84 |
| - path: cats |
85 |
| - method: get |
86 |
| - integration: lambda-proxy |
87 |
| - - http: |
88 |
| - path: dogs/{id} |
89 |
| - method: get |
90 |
| - integration: lambda-proxy |
91 |
| - |
92 |
| -custom: |
93 |
| - wsgi: |
94 |
| - app: api.app |
95 |
| -``` |
96 |
| -
|
97 |
| -### requirements.txt |
98 |
| -
|
99 |
| -Add Flask to the application bundle. |
100 |
| -
|
101 |
| -``` |
102 |
| -Flask==0.11.1 |
103 |
| -``` |
104 |
| - |
105 |
| - |
106 |
| -## Deployment |
107 |
| - |
108 |
| -Simply run the serverless deploy command as usual: |
109 |
| - |
110 |
| -``` |
111 |
| -$ sls deploy |
112 |
| -Serverless: Packaging Python WSGI handler... |
113 |
| -Serverless: Packaging required Python packages... |
114 |
| -Serverless: Packaging service... |
115 |
| -Serverless: Removing old service versions... |
116 |
| -Serverless: Uploading CloudFormation file to S3... |
117 |
| -Serverless: Uploading service .zip file to S3... |
118 |
| -Serverless: Updating Stack... |
119 |
| -Serverless: Checking Stack update progress... |
120 |
| -.......... |
121 |
| -Serverless: Stack update finished... |
122 |
| -``` |
123 |
| - |
124 |
| - |
125 |
| -## Other frameworks |
126 |
| - |
127 |
| -Set `custom.wsgi.app` in `serverless.yml` according to your WSGI callable: |
128 |
| - |
129 |
| -* 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 |
130 |
| -* Django is configured for WSGI by default, set the callable to `<project_name>.wsgi.application`. See [https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/](https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/) for more information. |
131 |
| - |
132 |
| - |
133 |
| -## Usage |
134 |
| - |
135 |
| -### Automatic requirement packaging |
136 |
| - |
137 |
| -You'll need to include any packages that your application uses in the bundle |
138 |
| -that's deployed to AWS Lambda. This plugin helps you out by doing this automatically, |
139 |
| -as long as you specify your required packages in a `requirements.txt` file in the root |
140 |
| -of your Serverless service path: |
141 |
| - |
142 |
| -``` |
143 |
| -Flask==0.11.1 |
144 |
| -requests==2.11.1 |
145 |
| -``` |
146 |
| - |
147 |
| -For more information, see [https://pip.readthedocs.io/en/1.1/requirements.html](https://pip.readthedocs.io/en/1.1/requirements.html). |
148 |
| - |
149 |
| -### Local server |
150 |
| - |
151 |
| -For convenience, a `sls wsgi serve` command is provided to run your WSGI application |
152 |
| -locally. This command requires the `werkzeug` Python package to be installed, |
153 |
| -and acts as a simple wrapper for starting werkzeug's built-in HTTP server. |
154 |
| - |
155 |
| -By default, the server will start on port 5000. |
156 |
| - |
157 |
| -``` |
158 |
| -$ sls wsgi serve |
159 |
| - * Running on http://localhost:5000/ (Press CTRL+C to quit) |
160 |
| - * Restarting with stat |
161 |
| - * Debugger is active! |
162 |
| -``` |
163 |
| - |
164 |
| -Configure the port using the `-p` parameter: |
165 |
| - |
166 |
| -``` |
167 |
| -$ sls wsgi serve -p 8000 |
168 |
| - * Running on http://localhost:8000/ (Press CTRL+C to quit) |
169 |
| - * Restarting with stat |
170 |
| - * Debugger is active! |
| 30 | +import requirements |
| 31 | +# Now you can use deps you specified in requirements.txt! |
| 32 | +import requests |
171 | 33 | ```
|
0 commit comments