0
# Request Processing
1
2
Lambda function handlers and WSGI request processing for converting AWS Lambda events into standard HTTP requests that can be processed by web applications like Django and Flask. This capability enables seamless deployment of existing web applications to serverless infrastructure.
3
4
## Capabilities
5
6
### Lambda Handler
7
8
Main entry point for AWS Lambda function execution and request routing.
9
10
```python { .api }
11
def lambda_handler(event, context):
12
"""
13
Global Lambda handler entry point for AWS Lambda runtime.
14
15
Processes incoming Lambda events and routes them to appropriate
16
handlers based on event type and source.
17
18
Parameters:
19
- event: dict, AWS Lambda event data
20
- context: LambdaContext, AWS Lambda runtime context
21
22
Returns:
23
dict: HTTP response with statusCode, headers, and body
24
"""
25
```
26
27
```python { .api }
28
class LambdaHandler:
29
@classmethod
30
def lambda_handler(cls, event, context):
31
"""
32
Class method entry point for Lambda event processing.
33
34
Manages singleton instance and delegates to handler method.
35
36
Parameters:
37
- event: dict, AWS Lambda event data
38
- context: LambdaContext, AWS Lambda runtime context
39
40
Returns:
41
dict: HTTP response
42
"""
43
```
44
45
### Request Processing
46
47
Core request processing and WSGI application execution.
48
49
```python { .api }
50
def handler(self, event, context):
51
"""
52
Process Lambda events and route to WSGI application.
53
54
Converts Lambda events to WSGI environ dict, executes application,
55
and formats response for API Gateway.
56
57
Parameters:
58
- event: dict, Lambda event from API Gateway
59
- context: LambdaContext, Lambda runtime context
60
61
Returns:
62
dict: API Gateway response format
63
"""
64
```
65
66
```python { .api }
67
def run_function(self, event, context):
68
"""
69
Execute application function with Lambda event and context.
70
71
Loads and executes the target application function with
72
Lambda event data and runtime context.
73
74
Parameters:
75
- event: dict, Lambda event data
76
- context: LambdaContext, Lambda runtime context
77
78
Returns:
79
any: Function return value
80
"""
81
```
82
83
### Dynamic Loading
84
85
Import and load application modules and functions at runtime.
86
87
```python { .api }
88
@staticmethod
89
def import_module_and_get_function(whole_function):
90
"""
91
Import module and return function from dotted path.
92
93
Dynamically imports module and retrieves function for execution.
94
95
Parameters:
96
- whole_function: str, dotted path to function (module.function)
97
98
Returns:
99
callable: The imported function
100
"""
101
```
102
103
### Remote Loading
104
105
Load application code and settings from remote sources.
106
107
```python { .api }
108
def load_remote_project_archive(self, project_s3_bucket, project_s3_key):
109
"""
110
Load project code from remote ZIP archive in S3.
111
112
Downloads and extracts project archive to Lambda runtime
113
for execution of remotely stored applications.
114
115
Parameters:
116
- project_s3_bucket: str, S3 bucket containing project archive
117
- project_s3_key: str, S3 key for project ZIP file
118
119
Returns:
120
bool: True if loading successful
121
"""
122
```
123
124
```python { .api }
125
def load_remote_settings(self, settings_s3_bucket, settings_s3_key):
126
"""
127
Load Zappa settings from remote S3 configuration file.
128
129
Downloads settings file from S3 and applies configuration
130
to current Lambda execution environment.
131
132
Parameters:
133
- settings_s3_bucket: str, S3 bucket containing settings
134
- settings_s3_key: str, S3 key for settings file
135
136
Returns:
137
dict: Loaded settings configuration
138
"""
139
```
140
141
### Keep-Warm Functionality
142
143
Handle keep-warm ping events to prevent Lambda cold starts.
144
145
```python { .api }
146
def keep_warm_callback(event, context):
147
"""
148
Handle keep-warm ping events for Lambda function.
149
150
Responds to scheduled keep-warm events to maintain Lambda
151
function in warm state and reduce cold start latency.
152
153
Parameters:
154
- event: dict, CloudWatch Events scheduled event
155
- context: LambdaContext, Lambda runtime context
156
157
Returns:
158
dict: Keep-warm response
159
"""
160
```
161
162
## WSGI Processing
163
164
Convert Lambda events to WSGI requests and process responses.
165
166
```python { .api }
167
def create_wsgi_request(event, script_name=None, base_path=None, trailing_slash=True):
168
"""
169
Create WSGI environ dictionary from Lambda event.
170
171
Converts API Gateway Lambda proxy integration event into
172
standard WSGI environ dict for web application processing.
173
174
Parameters:
175
- event: dict, API Gateway Lambda proxy event
176
- script_name: str, WSGI SCRIPT_NAME value
177
- base_path: str, base path for the application
178
- trailing_slash: bool, add trailing slash to paths
179
180
Returns:
181
dict: WSGI environ dictionary
182
"""
183
```
184
185
```python { .api }
186
def process_lambda_payload_v1(event):
187
"""
188
Process API Gateway v1.0 payload format.
189
190
Extracts HTTP request data from API Gateway v1.0 Lambda
191
proxy integration event format.
192
193
Parameters:
194
- event: dict, API Gateway v1.0 event
195
196
Returns:
197
tuple: (method, path, headers, body, query_params)
198
"""
199
```
200
201
```python { .api }
202
def process_lambda_payload_v2(event):
203
"""
204
Process API Gateway v2.0 payload format.
205
206
Extracts HTTP request data from API Gateway v2.0 Lambda
207
proxy integration event format.
208
209
Parameters:
210
- event: dict, API Gateway v2.0 event
211
212
Returns:
213
tuple: (method, path, headers, body, query_params)
214
"""
215
```
216
217
### Response Formatting
218
219
Format responses for API Gateway integration.
220
221
```python { .api }
222
def common_log(environ, response, response_time=None):
223
"""
224
Create Apache Common Log Format entries.
225
226
Generates standardized log entries from WSGI environ
227
and response data for request monitoring.
228
229
Parameters:
230
- environ: dict, WSGI environ dictionary
231
- response: dict, HTTP response data
232
- response_time: float, request processing time in seconds
233
234
Returns:
235
str: Common Log Format entry
236
"""
237
```
238
239
```python { .api }
240
def get_wsgi_string(wsgi_string):
241
"""
242
Encode string for WSGI compatibility.
243
244
Ensures proper string encoding for WSGI environ values
245
and HTTP headers.
246
247
Parameters:
248
- wsgi_string: str, input string to encode
249
250
Returns:
251
str: WSGI-compatible encoded string
252
"""
253
```
254
255
## WSGI Middleware
256
257
```python { .api }
258
class ZappaWSGIMiddleware:
259
"""
260
WSGI middleware for Zappa-specific request handling.
261
262
Provides WSGI application interface with Zappa-specific
263
processing for Lambda/API Gateway integration.
264
"""
265
266
def __init__(self, application):
267
"""
268
Initialize middleware with WSGI application.
269
270
Parameters:
271
- application: callable, WSGI application
272
"""
273
274
def __call__(self, environ, start_response):
275
"""
276
WSGI application interface.
277
278
Processes WSGI requests with Zappa-specific handling
279
and delegates to wrapped application.
280
281
Parameters:
282
- environ: dict, WSGI environ dictionary
283
- start_response: callable, WSGI start_response function
284
285
Returns:
286
iterable: WSGI response
287
"""
288
```
289
290
## Constants
291
292
```python { .api }
293
# HTTP methods that may contain binary data
294
BINARY_METHODS = ['POST', 'PUT', 'PATCH']
295
```
296
297
## Usage Examples
298
299
### Basic Lambda Handler Setup
300
301
```python
302
from zappa.handler import lambda_handler
303
304
# Use as Lambda function entry point
305
def my_lambda_handler(event, context):
306
return lambda_handler(event, context)
307
```
308
309
### Custom Handler Implementation
310
311
```python
312
from zappa.handler import LambdaHandler
313
314
class CustomHandler(LambdaHandler):
315
def handler(self, event, context):
316
# Custom preprocessing
317
processed_event = self.preprocess_event(event)
318
319
# Call parent handler
320
response = super().handler(processed_event, context)
321
322
# Custom postprocessing
323
return self.postprocess_response(response)
324
325
# Use custom handler
326
handler_instance = CustomHandler()
327
lambda_handler = handler_instance.lambda_handler
328
```
329
330
### WSGI Application Integration
331
332
```python
333
from zappa.wsgi import create_wsgi_request
334
from zappa.middleware import ZappaWSGIMiddleware
335
336
# Wrap existing WSGI app
337
from myapp import wsgi_application
338
339
zappa_app = ZappaWSGIMiddleware(wsgi_application)
340
341
def lambda_handler(event, context):
342
# Convert Lambda event to WSGI
343
environ = create_wsgi_request(event)
344
345
# Process through WSGI app
346
response = zappa_app(environ, start_response)
347
return response
348
```