Skip to content

Commit b3a916a

Browse files
Merging from develop
1 parent 01dcb01 commit b3a916a

Some content is hidden

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

50 files changed

+1075
-854
lines changed

docs/media/utilities_data_classes.png

-152 KB
Loading

docs/utilities/data_classes.md

+350-798
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"eventSource": "aws:mq",
3+
"eventSourceArn": "arn:aws:mq:us-east-2:111122223333:broker:test:b-9bcfa592-423a-4942-879d-eb284b418fc8",
4+
"messages": [
5+
{
6+
"messageID": "ID:b-9bcfa592-423a-4942-879d-eb284b418fc8-1.mq.us-east-2.amazonaws.com-37557-1234520418293-4:1:1:1:1",
7+
"messageType": "jms/text-message",
8+
"destination": {
9+
"physicalName": "testQueue"
10+
},
11+
"data": "QUJDOkFBQUE=",
12+
"timestamp": 1598827811958,
13+
"properties": {
14+
"index": "1"
15+
}
16+
},
17+
{
18+
"messageID": "ID:b-9bcfa592-423a-4942-879d-eb284b418fc8-1.mq.us-east-2.amazonaws.com-37557-1234520418293-4:1:1:1:2",
19+
"messageType": "jms/bytes-message",
20+
"destination": {
21+
"physicalName": "testQueue2"
22+
},
23+
"data": "LQaGQ82S48k=",
24+
"timestamp": 1598827811959
25+
}
26+
]
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"resource": "/helloworld",
3+
"path": "/hello",
4+
"httpMethod": "GET",
5+
"headers": {
6+
"Accept": "*/*",
7+
"Host": "api.example.com"
8+
},
9+
"queryStringParameters": {
10+
"name": "John"
11+
},
12+
"pathParameters": null,
13+
"stageVariables": null,
14+
"requestContext": {
15+
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
16+
"stage": "prod"
17+
},
18+
"body": null,
19+
"isBase64Encoded": false
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"xAmzRequestId": "1a5ed718-5f53-471d-b6fe-5cf62d88d02a",
3+
"getObjectContext": {
4+
"inputS3Url": "https://myap-123412341234.s3-accesspoint.us-east-1.amazonaws.com/s3.txt?X-Amz-Security-Token=...",
5+
"outputRoute": "io-iad-cell001",
6+
"outputToken": "..."
7+
},
8+
"configuration": {
9+
"accessPointArn": "arn:aws:s3-object-lambda:us-east-1:123412341234:accesspoint/myolap",
10+
"supportingAccessPointArn": "arn:aws:s3:us-east-1:123412341234:accesspoint/myap",
11+
"payload": "test"
12+
},
13+
"userRequest": {
14+
"url": "/s3.txt",
15+
"headers": {
16+
"Host": "myolap-123412341234.s3-object-lambda.us-east-1.amazonaws.com",
17+
"Accept-Encoding": "identity",
18+
"X-Amz-Content-SHA256": "e3b0c44297fc1c149afbf4c8995fb92427ae41e4649b934ca495991b7852b855"
19+
}
20+
},
21+
"userIdentity": {
22+
"type": "IAMUser",
23+
"principalId": "...",
24+
"arn": "arn:aws:iam::123412341234:user/myuser",
25+
"accountId": "123412341234",
26+
"accessKeyId": "..."
27+
},
28+
"protocolVersion": "1.00"
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import json
2+
3+
from aws_lambda_powertools import Logger
4+
from aws_lambda_powertools.utilities.data_classes import event_source
5+
from aws_lambda_powertools.utilities.data_classes.active_mq_event import ActiveMQEvent
6+
7+
logger = Logger()
8+
9+
10+
@event_source(data_class=ActiveMQEvent)
11+
def lambda_handler(event: ActiveMQEvent, context):
12+
for message in event.messages:
13+
msg = message.message_id
14+
msg_pn = message.destination_physicalname
15+
16+
logger.info(f"Message ID: {msg} and physical name: {msg_pn}")
17+
18+
return {"statusCode": 200, "body": json.dumps("Processing complete")}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from aws_lambda_powertools.utilities.data_classes import ALBEvent, event_source
2+
3+
4+
@event_source(data_class=ALBEvent)
5+
def lambda_handler(event: ALBEvent, context):
6+
if "lambda" in event.path and event.http_method == "GET":
7+
return {"statusCode": 200, "body": f"Hello from path: {event.path}"}
8+
else:
9+
return {"statusCode": 400, "body": "No Hello from path"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from secrets import compare_digest
2+
3+
from aws_lambda_powertools.utilities.data_classes import event_source
4+
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
5+
APIGatewayAuthorizerEventV2,
6+
APIGatewayAuthorizerResponseV2,
7+
)
8+
9+
10+
def get_user_by_token(token):
11+
if compare_digest(token, "value"):
12+
return {"name": "Foo"}
13+
return None
14+
15+
16+
@event_source(data_class=APIGatewayAuthorizerEventV2)
17+
def lambda_handler(event: APIGatewayAuthorizerEventV2, context):
18+
user = get_user_by_token(event.headers.get("Authorization"))
19+
20+
if user is None:
21+
# No user was found, so we return not authorized
22+
return APIGatewayAuthorizerResponseV2(authorize=False).asdict()
23+
24+
# Found the user and setting the details in the context
25+
response = APIGatewayAuthorizerResponseV2(
26+
authorize=True,
27+
context=user,
28+
)
29+
30+
return response.asdict()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from aws_lambda_powertools.utilities.data_classes import event_source
2+
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
3+
APIGatewayAuthorizerRequestEvent,
4+
APIGatewayAuthorizerResponse,
5+
)
6+
7+
8+
@event_source(data_class=APIGatewayAuthorizerRequestEvent)
9+
def lambda_handler(event: APIGatewayAuthorizerRequestEvent, context):
10+
# Simple auth check (replace with your actual auth logic)
11+
is_authorized = event.headers.get("HeaderAuth1") == "headerValue1"
12+
13+
if not is_authorized:
14+
return {"principalId": "", "policyDocument": {"Version": "2012-10-17", "Statement": []}}
15+
16+
arn = event.parsed_arn
17+
18+
policy = APIGatewayAuthorizerResponse(
19+
principal_id="user",
20+
context={"user": "example"},
21+
region=arn.region,
22+
aws_account_id=arn.aws_account_id,
23+
api_id=arn.api_id,
24+
stage=arn.stage,
25+
)
26+
27+
policy.allow_all_routes()
28+
29+
return policy.asdict()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from aws_lambda_powertools.utilities.data_classes import event_source
2+
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
3+
APIGatewayAuthorizerResponse,
4+
APIGatewayAuthorizerTokenEvent,
5+
)
6+
7+
8+
@event_source(data_class=APIGatewayAuthorizerTokenEvent)
9+
def lambda_handler(event: APIGatewayAuthorizerTokenEvent, context):
10+
# Simple token check (replace with your actual token validation logic)
11+
is_valid_token = event.authorization_token == "allow"
12+
13+
if not is_valid_token:
14+
return {"principalId": "", "policyDocument": {"Version": "2012-10-17", "Statement": []}}
15+
16+
arn = event.parsed_arn
17+
18+
policy = APIGatewayAuthorizerResponse(
19+
principal_id="user",
20+
context={"user": "example"},
21+
region=arn.region,
22+
aws_account_id=arn.aws_account_id,
23+
api_id=arn.api_id,
24+
stage=arn.stage,
25+
)
26+
27+
policy.allow_all_routes()
28+
29+
return policy.asdict()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent, event_source
2+
3+
4+
@event_source(data_class=APIGatewayProxyEvent)
5+
def lambda_handler(event: APIGatewayProxyEvent, context):
6+
if "hello" in event.path and event.http_method == "GET":
7+
return {"statusCode": 200, "body": f"Hello from path: {event.path}"}
8+
else:
9+
return {"statusCode": 400, "body": "No Hello from path"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEventV2, event_source
2+
3+
4+
@event_source(data_class=APIGatewayProxyEventV2)
5+
def lambda_handler(event: APIGatewayProxyEventV2, context):
6+
if "hello" in event.path and event.http_method == "POST":
7+
return {"statusCode": 200, "body": f"Hello from path: {event.path}"}
8+
else:
9+
return {"statusCode": 400, "body": "No Hello from path"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import Dict
2+
3+
from aws_lambda_powertools.logging import correlation_paths
4+
from aws_lambda_powertools.logging.logger import Logger
5+
from aws_lambda_powertools.utilities.data_classes.appsync_authorizer_event import (
6+
AppSyncAuthorizerEvent,
7+
AppSyncAuthorizerResponse,
8+
)
9+
from aws_lambda_powertools.utilities.data_classes.event_source import event_source
10+
11+
logger = Logger()
12+
13+
14+
def get_user_by_token(token: str):
15+
"""Look a user by token"""
16+
...
17+
18+
19+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPSYNC_AUTHORIZER)
20+
@event_source(data_class=AppSyncAuthorizerEvent)
21+
def lambda_handler(event: AppSyncAuthorizerEvent, context) -> Dict:
22+
user = get_user_by_token(event.authorization_token)
23+
24+
if not user:
25+
# No user found, return not authorized
26+
return AppSyncAuthorizerResponse().asdict()
27+
28+
return AppSyncAuthorizerResponse(
29+
authorize=True,
30+
resolver_context={"id": user.id},
31+
# Only allow admins to delete events
32+
deny_fields=None if user.is_admin else ["Mutation.deleteEvent"],
33+
).asdict()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from aws_lambda_powertools.utilities.data_classes import event_source
2+
from aws_lambda_powertools.utilities.data_classes.appsync_resolver_event import (
3+
AppSyncIdentityCognito,
4+
AppSyncResolverEvent,
5+
)
6+
from aws_lambda_powertools.utilities.typing import LambdaContext
7+
8+
9+
@event_source(data_class=AppSyncResolverEvent)
10+
def lambda_handler(event: AppSyncResolverEvent, context: LambdaContext):
11+
# Access the AppSync event details
12+
type_name = event.type_name
13+
field_name = event.field_name
14+
arguments = event.arguments
15+
source = event.source
16+
17+
print(f"Resolving field '{field_name}' for type '{type_name}'")
18+
print(f"Arguments: {arguments}")
19+
print(f"Source: {source}")
20+
21+
# Check if the identity is Cognito-based
22+
if isinstance(event.identity, AppSyncIdentityCognito):
23+
user_id = event.identity.sub
24+
username = event.identity.username
25+
print(f"Request from Cognito user: {username} (ID: {user_id})")
26+
else:
27+
print("Request is not from a Cognito-authenticated user")
28+
29+
if type_name == "Merchant" and field_name == "locations":
30+
page = arguments.get("page", 1)
31+
size = arguments.get("size", 10)
32+
name_filter = arguments.get("name")
33+
34+
# Here you would typically fetch locations from a database
35+
# This is a mock implementation
36+
locations = [
37+
{"id": "1", "name": "Location 1", "address": "123 Main St"},
38+
{"id": "2", "name": "Location 2", "address": "456 Elm St"},
39+
{"id": "3", "name": "Location 3", "address": "789 Oak St"},
40+
]
41+
42+
# Apply name filter if provided
43+
if name_filter:
44+
locations = [loc for loc in locations if name_filter.lower() in loc["name"].lower()]
45+
46+
# Apply pagination
47+
start = (page - 1) * size
48+
end = start + size
49+
paginated_locations = locations[start:end]
50+
51+
return {
52+
"items": paginated_locations,
53+
"totalCount": len(locations),
54+
"nextToken": str(page + 1) if end < len(locations) else None,
55+
}
56+
else:
57+
raise Exception(f"Unhandled field: {field_name} for type: {type_name}")

examples/event_sources/src/aws_config_rule.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
AWSConfigRuleEvent,
44
event_source,
55
)
6-
from aws_lambda_powertools.utilities.typing import LambdaContext
76

87
logger = Logger()
98

109

1110
@event_source(data_class=AWSConfigRuleEvent)
12-
def lambda_handler(event: AWSConfigRuleEvent, context: LambdaContext):
11+
def lambda_handler(event: AWSConfigRuleEvent, context):
1312
message_type = event.invoking_event.message_type
1413

1514
logger.info(f"Logging {message_type} event rule", invoke_event=event.raw_invoking_event)

0 commit comments

Comments
 (0)