Skip to content

Commit 0d97c6e

Browse files
committed
docs: add HTTP API and ALB example
1 parent 24affe8 commit 0d97c6e

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

docs/core/event_handler/api_gateway.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,60 @@ Here's an example where we have two separate functions to resolve two paths: `/h
173173
}
174174
```
175175

176-
#### API Gateway HTTP API
176+
#### HTTP API
177177

178+
When using API Gateway HTTP API to front your Lambda functions, you can instruct `ApiGatewayResolver` to conform with their contract via `proxy_type` param:
179+
180+
=== "app.py"
181+
182+
```python hl_lines="3 7"
183+
from aws_lambda_powertools import Logger, Tracer
184+
from aws_lambda_powertools.logging import correlation_paths
185+
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver, ProxyEventType
186+
187+
tracer = Tracer()
188+
logger = Logger()
189+
app = ApiGatewayResolver(proxy_type=ProxyEventType.http_api_v2)
190+
191+
@app.get("/hello")
192+
@tracer.capture_method
193+
def get_hello_universe():
194+
return {"message": "hello universe"}
195+
196+
# You can continue to use other utilities just as before
197+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_HTTP)
198+
@tracer.capture_lambda_handler
199+
def lambda_handler(event, context):
200+
return app.resolve(event, context)
201+
```
178202

179203
#### ALB
180204

205+
When using ALB to front your Lambda functions, you can instruct `ApiGatewayResolver` to conform with their contract via `proxy_type` param:
206+
207+
=== "app.py"
208+
209+
```python hl_lines="3 7"
210+
from aws_lambda_powertools import Logger, Tracer
211+
from aws_lambda_powertools.logging import correlation_paths
212+
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver, ProxyEventType
213+
214+
tracer = Tracer()
215+
logger = Logger()
216+
app = ApiGatewayResolver(proxy_type=ProxyEventType.alb_event)
217+
218+
@app.get("/hello")
219+
@tracer.capture_method
220+
def get_hello_universe():
221+
return {"message": "hello universe"}
222+
223+
# You can continue to use other utilities just as before
224+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPLICATION_LOAD_BALANCER)
225+
@tracer.capture_lambda_handler
226+
def lambda_handler(event, context):
227+
return app.resolve(event, context)
228+
```
229+
181230

182231
### Path expressions
183232

0 commit comments

Comments
 (0)