Skip to content

Commit 24affe8

Browse files
committed
docs: add getting started example
1 parent 3bcba99 commit 24affe8

File tree

1 file changed

+114
-12
lines changed

1 file changed

+114
-12
lines changed

docs/core/event_handler/api_gateway.md

+114-12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ This is the sample infrastructure we are using for the initial examples in this
3333
Description: Hello world event handler API Gateway
3434

3535
Globals:
36+
Api:
37+
TracingEnabled: true
3638
Function:
3739
Timeout: 5
3840
Runtime: python3.8
@@ -52,17 +54,17 @@ This is the sample infrastructure we are using for the initial examples in this
5254
Handler: app.lambda_handler
5355
CodeUri: hello_world
5456
Description: Hello World function
55-
Events:
56-
HelloUniverse:
57-
Type: Api
58-
Properties:
59-
Path: /hello
60-
Method: GET
61-
HelloYou:
62-
Type: Api
63-
Properties:
64-
Path: /hello/{name}
65-
Method: GET
57+
Events:
58+
HelloUniverse:
59+
Type: Api
60+
Properties:
61+
Path: /hello
62+
Method: GET
63+
HelloYou:
64+
Type: Api
65+
Properties:
66+
Path: /hello/{name}
67+
Method: GET
6668

6769
Outputs:
6870
HelloWorldApigwURL:
@@ -74,7 +76,107 @@ This is the sample infrastructure we are using for the initial examples in this
7476
Value: !GetAtt HelloWorldFunction.Arn
7577
```
7678

77-
### Resolver decorator
79+
### API Gateway decorator
80+
81+
You can define your functions to match a path and HTTP method, when you use the decorator `ApiGatewayResolver`.
82+
83+
Here's an example where we have two separate functions to resolve two paths: `/hello` and `/hello/{name}`.
84+
85+
=== "app.py"
86+
87+
```python hl_lines="3 7 9 18"
88+
from aws_lambda_powertools import Logger, Tracer
89+
from aws_lambda_powertools.logging import correlation_paths
90+
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
91+
92+
tracer = Tracer()
93+
logger = Logger()
94+
app = ApiGatewayResolver() # by default API Gateway REST API (v1)
95+
96+
@app.get("/hello")
97+
@tracer.capture_method
98+
def get_hello_universe():
99+
return {"message": "hello universe"}
100+
101+
# You can continue to use other utilities just as before
102+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
103+
@tracer.capture_lambda_handler
104+
def lambda_handler(event, context):
105+
return app.resolve(event, context)
106+
```
107+
=== "hello_event.json"
108+
109+
This utility uses `path` and `httpMethod` to route to the right function. This helps make unit tests and local invocation easier too.
110+
111+
```json hl_lines="4-5"
112+
{
113+
"body": "hello",
114+
"resource": "/hello",
115+
"path": "/hello",
116+
"httpMethod": "GET",
117+
"isBase64Encoded": false,
118+
"queryStringParameters": {
119+
"foo": "bar"
120+
},
121+
"multiValueQueryStringParameters": {},
122+
"pathParameters": {
123+
"hello": "/hello"
124+
},
125+
"stageVariables": {},
126+
"headers": {
127+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
128+
"Accept-Encoding": "gzip, deflate, sdch",
129+
"Accept-Language": "en-US,en;q=0.8",
130+
"Cache-Control": "max-age=0",
131+
"CloudFront-Forwarded-Proto": "https",
132+
"CloudFront-Is-Desktop-Viewer": "true",
133+
"CloudFront-Is-Mobile-Viewer": "false",
134+
"CloudFront-Is-SmartTV-Viewer": "false",
135+
"CloudFront-Is-Tablet-Viewer": "false",
136+
"CloudFront-Viewer-Country": "US",
137+
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
138+
"Upgrade-Insecure-Requests": "1",
139+
"User-Agent": "Custom User Agent String",
140+
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
141+
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
142+
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
143+
"X-Forwarded-Port": "443",
144+
"X-Forwarded-Proto": "https"
145+
},
146+
"multiValueHeaders": {},
147+
"requestContext": {
148+
"accountId": "123456789012",
149+
"resourceId": "123456",
150+
"stage": "Prod",
151+
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
152+
"requestTime": "25/Jul/2020:12:34:56 +0000",
153+
"requestTimeEpoch": 1428582896000,
154+
"identity": {
155+
"cognitoIdentityPoolId": null,
156+
"accountId": null,
157+
"cognitoIdentityId": null,
158+
"caller": null,
159+
"accessKey": null,
160+
"sourceIp": "127.0.0.1",
161+
"cognitoAuthenticationType": null,
162+
"cognitoAuthenticationProvider": null,
163+
"userArn": null,
164+
"userAgent": "Custom User Agent String",
165+
"user": null
166+
},
167+
"path": "/Prod/hello",
168+
"resourcePath": "/hello",
169+
"httpMethod": "POST",
170+
"apiId": "1234567890",
171+
"protocol": "HTTP/1.1"
172+
}
173+
}
174+
```
175+
176+
#### API Gateway HTTP API
177+
178+
179+
#### ALB
78180

79181

80182
### Path expressions

0 commit comments

Comments
 (0)