Skip to content

Commit 1170e76

Browse files
authored
docs(event-handler): snippets split, improved, and lint (#1279)
1 parent 46c1754 commit 1170e76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1164
-1018
lines changed

docs/core/event_handler/api_gateway.md

Lines changed: 148 additions & 1018 deletions
Large diffs are not rendered by default.

docs/core/logger.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Logger provides an opinionated logger with output structured as JSON.
1414

1515
## Getting started
1616

17+
???+ tip
18+
All examples shared in this documentation are available within the [project repository](https://github.com/awslabs/aws-lambda-powertools-python/tree/develop/examples){target="_blank"}.
19+
1720
Logger requires two settings:
1821

1922
| Setting | Description | Environment variable | Constructor parameter |

docs/core/metrics.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ If you're new to Amazon CloudWatch, there are two terminologies you must be awar
2828

2929
## Getting started
3030

31+
???+ tip
32+
All examples shared in this documentation are available within the [project repository](https://github.com/awslabs/aws-lambda-powertools-python/tree/develop/examples){target="_blank"}.
33+
3134
Metric has two global settings that will be used across all metrics emitted:
3235

3336
| Setting | Description | Environment variable | Constructor parameter |

docs/core/tracer.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Tracer is an opinionated thin wrapper for [AWS X-Ray Python SDK](https://github.
1616

1717
## Getting started
1818

19+
???+ tip
20+
All examples shared in this documentation are available within the [project repository](https://github.com/awslabs/aws-lambda-powertools-python/tree/develop/examples){target="_blank"}.
21+
1922
### Permissions
2023

2124
Before your use this utility, your AWS Lambda function [must have permissions](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html#services-xray-permissions) to send traces to AWS X-Ray.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
AWSTemplateFormatVersion: "2010-09-09"
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: Hello world event handler API Gateway
4+
5+
Globals:
6+
Api:
7+
TracingEnabled: true
8+
Cors: # see CORS section
9+
AllowOrigin: "'https://example.com'"
10+
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date'"
11+
MaxAge: "'300'"
12+
BinaryMediaTypes: # see Binary responses section
13+
- "*~1*" # converts to */* for any binary type
14+
Function:
15+
Timeout: 5
16+
Runtime: python3.8
17+
Tracing: Active
18+
Environment:
19+
Variables:
20+
LOG_LEVEL: INFO
21+
POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1
22+
POWERTOOLS_LOGGER_LOG_EVENT: true
23+
POWERTOOLS_SERVICE_NAME: example
24+
25+
Resources:
26+
ApiFunction:
27+
Type: AWS::Serverless::Function
28+
Properties:
29+
Handler: app.lambda_handler
30+
CodeUri: api_handler/
31+
Description: API handler function
32+
Events:
33+
AnyApiEvent:
34+
Type: Api
35+
Properties:
36+
# NOTE: this is a catch-all rule to simplify the documentation.
37+
# explicit routes and methods are recommended for prod instead (see below)
38+
Path: /{proxy+} # Send requests on any path to the lambda function
39+
Method: ANY # Send requests using any http method to the lambda function
40+
41+
42+
# GetAllTodos:
43+
# Type: Api
44+
# Properties:
45+
# Path: /todos
46+
# Method: GET
47+
# GetTodoById:
48+
# Type: Api
49+
# Properties:
50+
# Path: /todos/{todo_id}
51+
# Method: GET
52+
# CreateTodo:
53+
# Type: Api
54+
# Properties:
55+
# Path: /todos
56+
# Method: POST
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import Optional
2+
3+
import requests
4+
from requests import Response
5+
6+
from aws_lambda_powertools import Logger, Tracer
7+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
8+
from aws_lambda_powertools.logging import correlation_paths
9+
from aws_lambda_powertools.utilities.typing import LambdaContext
10+
11+
tracer = Tracer()
12+
logger = Logger()
13+
app = APIGatewayRestResolver()
14+
15+
16+
@app.get("/todos")
17+
@tracer.capture_method
18+
def get_todos():
19+
todo_id: str = app.current_event.get_query_string_value(name="id", default_value="")
20+
# alternatively
21+
_: Optional[str] = app.current_event.query_string_parameters.get("id")
22+
23+
# Payload
24+
_: Optional[str] = app.current_event.body # raw str | None
25+
26+
endpoint = "https://jsonplaceholder.typicode.com/todos"
27+
if todo_id:
28+
endpoint = f"{endpoint}/{todo_id}"
29+
30+
todos: Response = requests.get(endpoint)
31+
todos.raise_for_status()
32+
33+
return {"todos": todos.json()}
34+
35+
36+
# You can continue to use other utilities just as before
37+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
38+
@tracer.capture_lambda_handler
39+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
40+
return app.resolve(event, context)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import requests
2+
from requests import Response
3+
4+
from aws_lambda_powertools import Logger, Tracer
5+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
6+
from aws_lambda_powertools.logging import correlation_paths
7+
from aws_lambda_powertools.utilities.typing import LambdaContext
8+
9+
tracer = Tracer()
10+
logger = Logger()
11+
app = APIGatewayRestResolver()
12+
13+
14+
@app.get("/todos")
15+
@tracer.capture_method
16+
def get_todos():
17+
endpoint = "https://jsonplaceholder.typicode.com/todos"
18+
19+
api_key: str = app.current_event.get_header_value(name="X-Api-Key", case_sensitive=True, default_value="")
20+
todos: Response = requests.get(endpoint, headers={"X-Api-Key": api_key})
21+
todos.raise_for_status()
22+
23+
return {"todos": todos.json()}
24+
25+
26+
# You can continue to use other utilities just as before
27+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
28+
@tracer.capture_lambda_handler
29+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
30+
return app.resolve(event, context)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from dataclasses import dataclass
2+
3+
import assert_http_response_module
4+
import pytest
5+
6+
7+
@pytest.fixture
8+
def lambda_context():
9+
@dataclass
10+
class LambdaContext:
11+
function_name: str = "test"
12+
memory_limit_in_mb: int = 128
13+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
14+
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
15+
16+
return LambdaContext()
17+
18+
19+
def test_lambda_handler(lambda_context):
20+
minimal_event = {
21+
"path": "/todos",
22+
"httpMethod": "GET",
23+
"requestContext": {"requestId": "227b78aa-779d-47d4-a48e-ce62120393b8"}, # correlation ID
24+
}
25+
26+
ret = assert_http_response_module.lambda_handler(minimal_event, lambda_context)
27+
assert ret["statusCode"] == 200
28+
assert ret["body"] != ""
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import requests
2+
from requests import Response
3+
4+
from aws_lambda_powertools import Logger, Tracer
5+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
6+
from aws_lambda_powertools.logging import correlation_paths
7+
from aws_lambda_powertools.utilities.typing import LambdaContext
8+
9+
tracer = Tracer()
10+
logger = Logger()
11+
app = APIGatewayRestResolver()
12+
13+
14+
@app.get("/todos")
15+
@tracer.capture_method
16+
def get_todos():
17+
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
18+
todos.raise_for_status()
19+
20+
return {"todos": todos.json()[:10]}
21+
22+
23+
# You can continue to use other utilities just as before
24+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
25+
@tracer.capture_lambda_handler
26+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
27+
return app.resolve(event, context)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"headers": {
3+
"Accept": "image/svg+xml"
4+
},
5+
"resource": "/logo",
6+
"path": "/logo",
7+
"httpMethod": "GET"
8+
}

0 commit comments

Comments
 (0)