-
Notifications
You must be signed in to change notification settings - Fork 429
feat(event_sources): add support for VPC Lattice events #2358
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
leandrodamascena
merged 17 commits into
aws-powertools:develop
from
stephenbawks:vpclattice
Jun 8, 2023
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e6c6e69
vpc lattice event type
stephenbawks 306efc6
adding to init
stephenbawks c742470
fixing import name
stephenbawks 7d3b1f9
adding load test event
stephenbawks d53d6a6
fixing the host
stephenbawks 8ae8382
updating documentation
stephenbawks d57a500
spelling
stephenbawks 9d41a8d
Merge branch 'develop' into vpclattice
stephenbawks ab5c1c4
missed a link
stephenbawks 8043d39
Merge branch 'develop' into vpclattice
leandrodamascena 86aa432
added properties
stephenbawks 71e9043
Merge branch 'awslabs:develop' into vpclattice
stephenbawks 07310a5
test
stephenbawks 9cf1ddc
Merge branch 'develop' into vpclattice
heitorlessa 75c4ef7
chore: tests + coverage + documentation
leandrodamascena 0c208f2
chore: documentation
leandrodamascena 68fd363
Merge branch 'develop' into vpclattice
leandrodamascena 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
91 changes: 91 additions & 0 deletions
91
aws_lambda_powertools/utilities/data_classes/vpc_lattice.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,91 @@ | ||
import base64 | ||
from typing import Any, Dict, Optional | ||
|
||
from aws_lambda_powertools.utilities.data_classes.common import ( | ||
DictWrapper, | ||
get_header_value, | ||
) | ||
|
||
|
||
class VPCLatticeEvent(DictWrapper): | ||
@property | ||
def body(self) -> str: | ||
"""The VPC Lattice body.""" | ||
return self["body"] | ||
|
||
@property | ||
def json_body(self) -> Any: | ||
"""Parses the submitted body as json""" | ||
if self._json_data is None: | ||
self._json_data = self._json_deserializer(self.decoded_body) | ||
return self._json_data | ||
|
||
@property | ||
def headers(self) -> Dict[str, str]: | ||
"""The VPC Lattice event headers.""" | ||
return self["headers"] | ||
|
||
@property | ||
def is_base64_encoded(self) -> bool: | ||
"""A boolean flag to indicate if the applicable request payload is Base64-encode""" | ||
return self["is_base64_encoded"] | ||
|
||
@property | ||
def decoded_body(self) -> str: | ||
"""Dynamically base64 decode body as a str""" | ||
body: str = self["body"] | ||
if self.is_base64_encoded: | ||
return base64.b64decode(body.encode()).decode() | ||
return body | ||
|
||
@property | ||
def method(self) -> str: | ||
"""The VPC Lattice method used. Valid values include: DELETE, GET, HEAD, OPTIONS, PATCH, POST, and PUT.""" | ||
return self["method"] | ||
|
||
@property | ||
def query_string_parameters(self) -> Dict[str, str]: | ||
"""The request query string parameters.""" | ||
return self["query_string_parameters"] | ||
|
||
@property | ||
def raw_path(self) -> str: | ||
"""The raw VPC Lattice request path.""" | ||
return self["raw_path"] | ||
|
||
def get_query_string_value(self, name: str, default_value: Optional[str] = None) -> Optional[str]: | ||
"""Get query string value by name | ||
|
||
Parameters | ||
---------- | ||
name: str | ||
Query string parameter name | ||
default_value: str, optional | ||
Default value if no value was found by name | ||
Returns | ||
------- | ||
str, optional | ||
Query string parameter value | ||
""" | ||
params = self.query_string_parameters | ||
return default_value if params is None else params.get(name, default_value) | ||
|
||
def get_header_value( | ||
self, name: str, default_value: Optional[str] = None, case_sensitive: Optional[bool] = False | ||
) -> Optional[str]: | ||
"""Get header value by name | ||
|
||
Parameters | ||
---------- | ||
name: str | ||
Header name | ||
default_value: str, optional | ||
Default value if no value was found by name | ||
case_sensitive: bool | ||
Whether to use a case-sensitive look up | ||
Returns | ||
------- | ||
str, optional | ||
Header value | ||
""" | ||
return get_header_value(self.headers, name, default_value, case_sensitive) |
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,19 @@ | ||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.utilities.data_classes import VPCLatticeEvent, event_source | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
logger = Logger() | ||
|
||
|
||
@event_source(data_class=VPCLatticeEvent) | ||
def lambda_handler(event: VPCLatticeEvent, context: LambdaContext): | ||
logger.info(event.body) | ||
|
||
response = { | ||
"isBase64Encoded": False, | ||
"statusCode": 200, | ||
"headers": {"Content-Type": "application/text"}, | ||
"body": "Event Response to VPC Lattice 🔥🚀🔥", | ||
} | ||
|
||
return response |
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,15 @@ | ||
{ | ||
"raw_path": "/testpath", | ||
"method": "GET", | ||
"headers": { | ||
"user_agent": "curl/7.64.1", | ||
"x-forwarded-for": "10.213.229.10", | ||
"host": "test-lambda-service-3908sdf9u3u.dkfjd93.vpc-lattice-svcs.us-east-2.on.aws", | ||
"accept": "*/*" | ||
}, | ||
"query_string_parameters": { | ||
"order-id": "1" | ||
}, | ||
"body": "eyJ0ZXN0IjogImV2ZW50In0=", | ||
"is_base64_encoded": true | ||
} |
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,15 @@ | ||
{ | ||
"raw_path": "/testpath", | ||
"method": "GET", | ||
"headers": { | ||
"user_agent": "curl/7.64.1", | ||
"x-forwarded-for": "10.213.229.10", | ||
"host": "test-lambda-service-3908sdf9u3u.dkfjd93.vpc-lattice-svcs.us-east-2.on.aws", | ||
"accept": "*/*" | ||
}, | ||
"query_string_parameters": { | ||
"order-id": "1" | ||
}, | ||
"body": "eyJ0ZXN0IjogImV2ZW50In0=", | ||
"is_base64_encoded": true | ||
} |
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
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.