Skip to content

Commit 276293b

Browse files
committed
chore(docs): added documentation
1 parent 49f299b commit 276293b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

docs/core/event_handler/api_gateway.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ When using [Custom Domain API Mappings feature](https://docs.aws.amazon.com/apig
272272

273273
**Scenario**: You have a custom domain `api.mydomain.dev`. Then you set `/payment` API Mapping to forward any payment requests to your Payments API.
274274

275-
**Challenge**: This means your `path` value for any API requests will always contain `/payment/<actual_request>`, leading to HTTP 404 as Event Handler is trying to match what's after `payment/`. This gets further complicated with an [arbitrary level of nesting](https://github.com/aws-powertools/powertools-lambda-roadmap/issues/34){target="_blank"}.
275+
**Challenge**: This means your `path` value for any API requests will always contain `/payment/<actual_request>`, leading to HTTP 404 as Event Handler is trying to match what's after `payment/`. This gets further complicated with an [arbitrary level of nesting](https://github.com/aws-powertools/powertools-lambda/issues/34){target="_blank"}.
276276

277277
To address this API Gateway behavior, we use `strip_prefixes` parameter to account for these prefixes that are now injected into the path regardless of which type of API Gateway you're using.
278278

@@ -293,6 +293,14 @@ To address this API Gateway behavior, we use `strip_prefixes` parameter to accou
293293

294294
For example, when using `strip_prefixes` value of `/pay`, there is no difference between a request path of `/pay` and `/pay/`; and the path argument would be defined as `/`.
295295

296+
For added flexibility, you can use regexes to strip a prefix. This is helpful when you have many options due to different combinations of prefixes (e.g: multiple environments, multiple versions).
297+
298+
=== "strip_route_prefix_regex.py"
299+
300+
```python hl_lines="12"
301+
--8<-- "examples/event_handler_rest/src/strip_route_prefix_regex.py"
302+
```
303+
296304
## Advanced
297305

298306
### CORS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import re
2+
3+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
4+
from aws_lambda_powertools.utilities.typing import LambdaContext
5+
6+
# This will support:
7+
# /v1/dev/subscriptions/<subscription>
8+
# /v1/stg/subscriptions/<subscription>
9+
# /v1/qa/subscriptions/<subscription>
10+
# /v2/dev/subscriptions/<subscription>
11+
# ...
12+
app = APIGatewayRestResolver(strip_prefixes=[re.compile(r"/v[1-3]+/(dev|stg|qa)")])
13+
14+
15+
@app.get("/subscriptions/<subscription>")
16+
def get_subscription(subscription):
17+
return {"subscription_id": subscription}
18+
19+
20+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
21+
return app.resolve(event, context)

0 commit comments

Comments
 (0)