You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/event_handler/api_gateway.md
+13-39
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,11 @@ title: REST API
3
3
description: Core utility
4
4
---
5
5
6
-
Event handler for Amazon API Gateway REST and HTTP APIs, Application Loader Balancer (ALB), and Lambda Function URLs.
6
+
Event handler for Amazon API Gateway REST and HTTP APIs, and Application Loader Balancer (ALB).
7
7
8
8
## Key Features
9
9
10
-
* Lightweight routing to reduce boilerplate for API Gateway REST/HTTP API, ALB and Lambda Function URLs.
10
+
* Lightweight routing to reduce boilerplate for API Gateway REST/HTTP APIand ALB
11
11
* Support for CORS, binary and Gzip compression, Decimals JSON encoding and bring your own JSON serializer
12
12
* Built-in integration with [Event Source Data Classes utilities](../../utilities/data_classes.md){target="_blank"} for self-documented event schema
13
13
@@ -18,31 +18,23 @@ Event handler for Amazon API Gateway REST and HTTP APIs, Application Loader Bala
18
18
19
19
### Required resources
20
20
21
-
If you're using any API Gateway integration, you must have an existing [API Gateway Proxy integration](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html){target="_blank"} or [ALB](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html){target="_blank"} configured to invoke your Lambda function.
21
+
You must have an existing [API Gateway Proxy integration](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html){target="_blank"} or [ALB](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html){target="_blank"} configured to invoke your Lambda function.
22
22
23
-
This is the sample infrastructure for API Gateway and Lambda Function URLs we are using for the examples in this documentation.
23
+
This is the sample infrastructure for API Gateway we are using for the examples in this documentation.
24
24
25
25
???+ info "There is no additional permissions or dependencies required to use this utility."
26
26
27
-
=== "API Gateway SAM Template"
28
-
29
-
```yaml title="AWS Serverless Application Model (SAM) example"
Before you decorate your functions to handle a given path and HTTP method(s), you need to initialize a resolver.
42
34
43
35
A resolver will handle request resolution, including [one or more routers](#split-routes-with-router), and give you access to the current event via typed properties.
44
36
45
-
For resolvers, we provide: `APIGatewayRestResolver`, `APIGatewayHttpResolver`, `ALBResolver`, and `LambdaFunctionUrlResolver`.
37
+
For resolvers, we provide: `APIGatewayRestResolver`, `APIGatewayHttpResolver`, and `ALBResolver`.
46
38
47
39
???+ info
48
40
We will use `APIGatewayRestResolver` as the default across examples.
@@ -95,22 +87,6 @@ When using Amazon Application Load Balancer (ALB) to front your Lambda functions
You can use `/todos/<todo_id>` to configure dynamic URL paths, where `<todo_id>` will be resolved at runtime.
@@ -222,7 +198,7 @@ You can use **`not_found`** decorator to override this behavior, and return a cu
222
198
223
199
You can use **`exception_handler`** decorator with any Python exception. This allows you to handle a common exception outside your route, for example validation errors.
@@ -291,7 +267,7 @@ This will ensure that CORS headers are always returned as part of the response w
291
267
292
268
#### Pre-flight
293
269
294
-
Pre-flight (OPTIONS) calls are typically handled at the API Gateway or Lambda Function URL level as per [our sample infrastructure](#required-resources), no Lambda integration is necessary. However, ALB expects you to handle pre-flight requests.
270
+
Pre-flight (OPTIONS) calls are typically handled at the API Gateway level as per [our sample infrastructure](#required-resources), no Lambda integration necessary. However, ALB expects you to handle pre-flight requests.
295
271
296
272
For convenience, we automatically handle that for you as long as you [setup CORS in the constructor level](#cors).
297
273
@@ -360,8 +336,6 @@ Like `compress` feature, the client must send the `Accept` header with the corre
360
336
???+ warning
361
337
This feature requires API Gateway to configure binary media types, see [our sample infrastructure](#required-resources) for reference.
362
338
363
-
???+ note
364
-
Lambda Function URLs handle binary media types automatically.
365
339
=== "binary_responses.py"
366
340
367
341
```python hl_lines="14 20"
@@ -403,7 +377,7 @@ This will enable full tracebacks errors in the response, print request and respo
403
377
404
378
### Custom serializer
405
379
406
-
You can instruct event handler to use a custom serializer to best suit your needs, for example take into account Enums when serializing.
380
+
You can instruct API Gateway handler to use a custom serializer to best suit your needs, for example take into account Enums when serializing.
407
381
408
382
```python hl_lines="35 40" title="Using a custom JSON serializer for responses"
@@ -413,7 +387,7 @@ You can instruct event handler to use a custom serializer to best suit your need
413
387
414
388
As you grow the number of routes a given Lambda function should handle, it is natural to split routes into separate files to ease maintenance - That's where the `Router` feature is useful.
415
389
416
-
Let's assume you have `split_route.py` as your Lambda function entrypoint and routes in `split_route_module.py`. This is how you'd use the `Router` feature.
390
+
Let's assume you have `app.py` as your Lambda function entrypoint and routes in `split_route_module.py`, this is how you'd use the `Router` feature.
417
391
418
392
=== "split_route_module.py"
419
393
@@ -524,7 +498,7 @@ A micro function means that your final code artifact will be different to each f
524
498
525
499
**Downsides**
526
500
527
-
***Upfront investment**. You need custom build tooling to bundle assets, including [C bindings for runtime compatibility](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html){target="_blank"}. Operations become more elaborate — you need to standardize tracing labels/annotations, structured logging, and metrics to pinpoint root causes.
501
+
***Upfront investment**. You need custom build tooling to bundle assets, including [C bindings for runtime compatibility](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html){target="_blank"}. `Operations become more elaborate — you need to standardize tracing labels/annotations, structured logging, and metrics to pinpoint root causes.
528
502
* Engineering discipline is necessary for both approaches. Micro-function approach however requires further attention in consistency as the number of functions grow, just like any distributed system.
529
503
***Harder to share code**. Shared code must be carefully evaluated to avoid unnecessary deployments when that changes. Equally, if shared code isn't a library,
530
504
your development, building, deployment tooling need to accommodate the distinct layout.
0 commit comments