Skip to content

Commit a47f6eb

Browse files
chore(event_handlers): apply suggestions from code review
Co-authored-by: Heitor Lessa <[email protected]>
1 parent 0711e6d commit a47f6eb

File tree

8 files changed

+20
-18
lines changed

8 files changed

+20
-18
lines changed

aws_lambda_powertools/event_handler/lambda_function_url.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ class LambdaFunctionUrlResolver(ApiGatewayResolver):
1010
1111
Notes:
1212
-----
13-
For now, this seems to work the same way as API Gateway HTTP APIs Payload Format Version 2.0.
13+
Lambda Function URL follows the API Gateway HTTP APIs Payload Format Version 2.0.
1414
1515
Documentation:
1616
- https://docs.aws.amazon.com/lambda/latest/dg/urls-configuration.html
1717
- https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-payloads
1818
1919
Examples
2020
--------
21-
Simple example with a custom lambda handler using the Tracer capture_lambda_handler decorator
21+
Simple example integrating with Tracer
2222
2323
```python
2424
from aws_lambda_powertools import Tracer
@@ -34,7 +34,7 @@ def simple_get():
3434
@app.post("/post-call")
3535
def simple_post():
3636
post_data: dict = app.current_event.json_body
37-
return {"message": post_data["value"]}
37+
return {"message": post_data}
3838
3939
@tracer.capture_lambda_handler
4040
def lambda_handler(event, context):

aws_lambda_powertools/logging/correlation_paths.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
APPSYNC_RESOLVER = 'request.headers."x-amzn-trace-id"'
77
APPLICATION_LOAD_BALANCER = 'headers."x-amzn-trace-id"'
88
EVENT_BRIDGE = "id"
9-
LAMBDA_FUNCTION_URL = "requestContext.requestId"
9+
LAMBDA_FUNCTION_URL = API_GATEWAY_REST
1010
S3_OBJECT_LAMBDA = "xAmzRequestId"

aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,21 @@ def caller_id(self) -> Optional[str]:
127127
def cognito_amr(self) -> Optional[List[str]]:
128128
"""This represents how the user was authenticated.
129129
AMR stands for Authentication Methods References as per the openid spec"""
130-
cognito_identity = self["cognitoIdentity"] or {}
130+
cognito_identity = self["cognitoIdentity"] or {} # not available in FunctionURL
131131
return cognito_identity.get("amr")
132132

133133
@property
134134
def cognito_identity_id(self) -> Optional[str]:
135135
"""The Amazon Cognito identity ID of the caller making the request.
136136
Available only if the request was signed with Amazon Cognito credentials."""
137-
cognito_identity = self.get("cognitoIdentity") or {}
137+
cognito_identity = self.get("cognitoIdentity") or {} # not available in FunctionURL
138138
return cognito_identity.get("identityId")
139139

140140
@property
141141
def cognito_identity_pool_id(self) -> Optional[str]:
142142
"""The Amazon Cognito identity pool ID of the caller making the request.
143143
Available only if the request was signed with Amazon Cognito credentials."""
144-
cognito_identity = self.get("cognitoIdentity") or {}
144+
cognito_identity = self.get("cognitoIdentity") or {} # not available in FunctionURL
145145
return cognito_identity.get("identityPoolId")
146146

147147
@property
@@ -163,12 +163,12 @@ def user_id(self) -> Optional[str]:
163163
class RequestContextV2Authorizer(DictWrapper):
164164
@property
165165
def jwt_claim(self) -> Optional[Dict[str, Any]]:
166-
jwt = self.get("jwt") or {}
166+
jwt = self.get("jwt") or {} # not available in FunctionURL
167167
return jwt.get("claims")
168168

169169
@property
170170
def jwt_scopes(self) -> Optional[List[str]]:
171-
jwt = self.get("jwt") or {}
171+
jwt = self.get("jwt") or {} # not available in FunctionURL
172172
return jwt.get("scopes")
173173

174174
@property

aws_lambda_powertools/utilities/data_classes/common.py

+1
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ def time_epoch(self) -> int:
395395
@property
396396
def authentication(self) -> Optional[RequestContextClientCert]:
397397
"""Optional when using mutual TLS authentication"""
398+
# FunctionURL might have NONE as AuthZ
398399
authentication = self["requestContext"].get("authentication") or {}
399400
client_cert = authentication.get("clientCert")
400401
return None if client_cert is None else RequestContextClientCert(client_cert)

aws_lambda_powertools/utilities/data_classes/lambda_function_url_event.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class LambdaFunctionUrlEvent(APIGatewayProxyEventV2):
66
77
Notes:
88
-----
9-
For now, this seems to follow the exact same payload as HTTP APIs Payload Format Version 2.0.
10-
Certain keys in this payload format don't make sense for function urls (e.g: `routeKey`, `stage`).
11-
These keys will have default values that come on the payload, but they are not useful since they can't be changed.
9+
Lambda Function URL follows the API Gateway HTTP APIs Payload Format Version 2.0.
10+
11+
Keys related to API Gateway features not available in Function URL use a sentinel value (e.g.`routeKey`, `stage`).
1212
1313
Documentation:
1414
- https://docs.aws.amazon.com/lambda/latest/dg/urls-configuration.html

docs/core/event_handler/api_gateway.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ When using Amazon Application Load Balancer (ALB) to front your Lambda functions
9797

9898
#### Lambda Function URL
9999

100-
When using an [AWS Lambda Function URL](https://docs.aws.amazon.com/lambda/latest/dg/urls-configuration.html), you can use `LambdaFunctionUrlResolver`.
100+
When using [AWS Lambda Function URL](https://docs.aws.amazon.com/lambda/latest/dg/urls-configuration.html), you can use `LambdaFunctionUrlResolver`.
101101

102102
=== "getting_started_lambda_function_url_resolver.py"
103103

@@ -294,7 +294,7 @@ This will ensure that CORS headers are always returned as part of the response w
294294

295295
#### Pre-flight
296296

297-
Pre-flight (OPTIONS) calls are typically handled at the API Gateway or Lambda Function URL level as per [our sample infrastructure](#required-resources), no Lambda integration necessary. However, ALB expects you to handle pre-flight requests.
297+
Pre-flight (OPTIONS) calls are typically handled at the API Gateway or Lambda Function URL level as per [our sample infrastructure](#required-resources), no Lambda integration is necessary. However, ALB expects you to handle pre-flight requests.
298298

299299
For convenience, we automatically handle that for you as long as you [setup CORS in the constructor level](#cors).
300300

@@ -362,8 +362,9 @@ Like `compress` feature, the client must send the `Accept` header with the corre
362362

363363
???+ warning
364364
This feature requires API Gateway to configure binary media types, see [our sample infrastructure](#required-resources) for reference.
365-
For Lambda Function URLs, no additional configuration is necessary.
366365

366+
???+ note
367+
Lambda Function URLs handle binary media types automatically.
367368
=== "binary_responses.py"
368369

369370
```python hl_lines="14 20"
@@ -405,7 +406,7 @@ This will enable full tracebacks errors in the response, print request and respo
405406

406407
### Custom serializer
407408

408-
You can instruct an event handler to use a custom serializer to best suit your needs, for example take into account Enums when serializing.
409+
You can instruct event handler to use a custom serializer to best suit your needs, for example take into account Enums when serializing.
409410

410411
```python hl_lines="35 40" title="Using a custom JSON serializer for responses"
411412
--8<-- "examples/event_handler_rest/src/custom_serializer.py"

docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ Core utilities such as Tracing, Logging, Metrics, and Event Handler will be avai
417417
| [Logger](./core/logger.md) | Structured logging made easier, and decorator to enrich structured logging with key Lambda context details |
418418
| [Metrics](./core/metrics.md) | Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF) |
419419
| [Event handler: AppSync](./core/event_handler/appsync.md) | AppSync event handler for Lambda Direct Resolver and Amplify GraphQL Transformer function |
420-
| [Event handler: API Gateway, ALB and Lambda Function URL](https://awslabs.github.io/aws-lambda-powertools-python/latest/core/event_handler/api_gateway/) | Amazon API Gateway REST/HTTP API and ALB event handler for Lambda functions invoked using Proxy integration |
420+
| [Event handler: API Gateway, ALB and Lambda Function URL](https://awslabs.github.io/aws-lambda-powertools-python/latest/core/event_handler/api_gateway/) | Amazon API Gateway REST/HTTP API and ALB event handler for Lambda functions invoked using Proxy integration, and Lambda Function URL |
421421
| [Middleware factory](./utilities/middleware_factory.md) | Decorator factory to create your own middleware to run logic before, and after each Lambda invocation |
422422
| [Parameters](./utilities/parameters.md) | Retrieve parameter values from AWS Systems Manager Parameter Store, AWS Secrets Manager, or Amazon DynamoDB, and cache them for a specific amount of time |
423423
| [Batch processing](./utilities/batch.md) | Handle partial failures for AWS SQS batch processing |

examples/event_handler_lambda_function_url/sam/template.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ Resources:
2828
CodeUri: ../src
2929
Description: API handler function
3030
FunctionUrlConfig:
31-
AuthType: NONE
31+
AuthType: NONE # AWS_IAM for added security beyond sample documentation

0 commit comments

Comments
 (0)