-
Notifications
You must be signed in to change notification settings - Fork 433
feat(parser): add support for Lambda Function URL #1442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f741299
feat: Add Parser official support for Lambda Function UR
ran-isenberg a0ef46d
Update parser.md
ran-isenberg dd7f5dd
Update aws_lambda_powertools/utilities/parser/envelopes/lambda_functi…
ran-isenberg 2393229
Update docs/utilities/parser.md
ran-isenberg 9c2f863
Merge branch 'develop' into func_url
ran-isenberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
aws_lambda_powertools/utilities/parser/envelopes/lambda_function_url.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logging | ||
from typing import Any, Dict, Optional, Type, Union | ||
|
||
from ..models import LambdaFunctionUrlModel | ||
from ..types import Model | ||
from .base import BaseEnvelope | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LambdaFunctionUrlEnvelope(BaseEnvelope): | ||
"""Lambda function URL envelope to extract data within body key""" | ||
|
||
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> Optional[Model]: | ||
"""Parses data found with model provided | ||
|
||
Parameters | ||
---------- | ||
data : Dict | ||
Lambda event to be parsed | ||
model : Type[Model] | ||
Data model provided to parse after extracting data using envelope | ||
|
||
Returns | ||
------- | ||
Any | ||
Parsed detail payload with model provided | ||
""" | ||
logger.debug(f"Parsing incoming data with Lambda function URL {LambdaFunctionUrlModel}") | ||
parsed_envelope: LambdaFunctionUrlModel = LambdaFunctionUrlModel.parse_obj(data) | ||
logger.debug(f"Parsing event payload in `detail` with {model}") | ||
return self._parse(data=parsed_envelope.body, model=model) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
aws_lambda_powertools/utilities/parser/models/lambda_function_url.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventV2Model | ||
|
||
|
||
class LambdaFunctionUrlModel(APIGatewayProxyEventV2Model): | ||
"""AWS Lambda Function URL model | ||
|
||
Notes: | ||
----- | ||
Lambda Function URL follows the API Gateway HTTP APIs Payload Format Version 2.0. | ||
|
||
Keys related to API Gateway features not available in Function URL use a sentinel value (e.g.`routeKey`, `stage`). | ||
|
||
Documentation: | ||
- https://docs.aws.amazon.com/lambda/latest/dg/urls-configuration.html | ||
- https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-payloads | ||
""" | ||
|
||
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
from aws_lambda_powertools.utilities.parser import envelopes, event_parser | ||
from aws_lambda_powertools.utilities.parser.models import LambdaFunctionUrlModel | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
from tests.functional.parser.schemas import MyALambdaFuncUrlBusiness | ||
from tests.functional.utils import load_event | ||
|
||
|
||
@event_parser(model=MyALambdaFuncUrlBusiness, envelope=envelopes.LambdaFunctionUrlEnvelope) | ||
def handle_lambda_func_url_with_envelope(event: MyALambdaFuncUrlBusiness, _: LambdaContext): | ||
assert event.message == "Hello" | ||
assert event.username == "Ran" | ||
|
||
|
||
@event_parser(model=LambdaFunctionUrlModel) | ||
def handle_lambda_func_url_event(event: LambdaFunctionUrlModel, _: LambdaContext): | ||
return event | ||
|
||
|
||
def test_lambda_func_url_event_with_envelope(): | ||
event = load_event("lambdaFunctionUrlEvent.json") | ||
event["body"] = '{"message": "Hello", "username": "Ran"}' | ||
handle_lambda_func_url_with_envelope(event, LambdaContext()) | ||
|
||
|
||
def test_lambda_function_url_event(): | ||
json_event = load_event("lambdaFunctionUrlEvent.json") | ||
event: LambdaFunctionUrlModel = handle_lambda_func_url_event(json_event, LambdaContext()) | ||
|
||
assert event.version == "2.0" | ||
assert event.routeKey == "$default" | ||
|
||
assert event.rawQueryString == "" | ||
|
||
assert event.cookies is None | ||
|
||
headers = event.headers | ||
assert len(headers) == 20 | ||
|
||
assert event.queryStringParameters is None | ||
|
||
assert event.isBase64Encoded is False | ||
assert event.body is None | ||
assert event.pathParameters is None | ||
assert event.stageVariables is None | ||
|
||
request_context = event.requestContext | ||
|
||
assert request_context.accountId == "anonymous" | ||
assert request_context.apiId is not None | ||
assert request_context.domainName == "<url-id>.lambda-url.us-east-1.on.aws" | ||
assert request_context.domainPrefix == "<url-id>" | ||
assert request_context.requestId == "id" | ||
assert request_context.routeKey == "$default" | ||
assert request_context.stage == "$default" | ||
assert request_context.time is not None | ||
convert_time = int(round(request_context.timeEpoch.timestamp() * 1000)) | ||
assert convert_time == 1659687279885 | ||
assert request_context.authorizer is None | ||
|
||
http = request_context.http | ||
assert http.method == "GET" | ||
assert http.path == "/" | ||
assert http.protocol == "HTTP/1.1" | ||
assert str(http.sourceIp) == "123.123.123.123/32" | ||
assert http.userAgent == "agent" | ||
|
||
assert request_context.authorizer is None | ||
|
||
|
||
def test_lambda_function_url_event_iam(): | ||
json_event = load_event("lambdaFunctionUrlIAMEvent.json") | ||
event: LambdaFunctionUrlModel = handle_lambda_func_url_event(json_event, LambdaContext()) | ||
|
||
assert event.version == "2.0" | ||
assert event.routeKey == "$default" | ||
|
||
assert event.rawQueryString == "parameter1=value1¶meter1=value2¶meter2=value" | ||
|
||
cookies = event.cookies | ||
assert len(cookies) == 2 | ||
assert cookies[0] == "cookie1" | ||
|
||
headers = event.headers | ||
assert len(headers) == 2 | ||
|
||
query_string_parameters = event.queryStringParameters | ||
assert len(query_string_parameters) == 2 | ||
assert query_string_parameters.get("parameter2") == "value" | ||
|
||
assert event.isBase64Encoded is False | ||
assert event.body == "Hello from client!" | ||
assert event.pathParameters is None | ||
assert event.stageVariables is None | ||
|
||
request_context = event.requestContext | ||
|
||
assert request_context.accountId == "123456789012" | ||
assert request_context.apiId is not None | ||
assert request_context.domainName == "<url-id>.lambda-url.us-west-2.on.aws" | ||
assert request_context.domainPrefix == "<url-id>" | ||
assert request_context.requestId == "id" | ||
assert request_context.routeKey == "$default" | ||
assert request_context.stage == "$default" | ||
assert request_context.time is not None | ||
convert_time = int(round(request_context.timeEpoch.timestamp() * 1000)) | ||
assert convert_time == 1583348638390 | ||
|
||
http = request_context.http | ||
assert http.method == "POST" | ||
assert http.path == "/my/path" | ||
assert http.protocol == "HTTP/1.1" | ||
assert str(http.sourceIp) == "123.123.123.123/32" | ||
assert http.userAgent == "agent" | ||
|
||
authorizer = request_context.authorizer | ||
assert authorizer is not None | ||
assert authorizer.jwt is None | ||
assert authorizer.lambda_value is None | ||
|
||
iam = authorizer.iam | ||
assert iam is not None | ||
assert iam.accessKey == "AKIA..." | ||
assert iam.accountId == "111122223333" | ||
assert iam.callerId == "AIDA..." | ||
assert iam.cognitoIdentity is None | ||
assert iam.principalOrgId is None | ||
assert iam.userId == "AIDA..." | ||
assert iam.userArn == "arn:aws:iam::111122223333:user/example-user" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.