-
Notifications
You must be signed in to change notification settings - Fork 429
feat(event_handler): add VPCLatticeResolver #2601
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
rubenfonseca
merged 4 commits into
aws-powertools:develop
from
leandrodamascena:eventhandler/vpclattice
Jun 28, 2023
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9651464
feature: adding vpc lattice support
leandrodamascena fcb1579
Merge remote-tracking branch 'upstream/develop' into eventhandler/vpc…
leandrodamascena 1ac1504
docs: small fix
leandrodamascena 7412bff
addessing Ruben's feedback
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
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,53 @@ | ||
from typing import Callable, Dict, List, Optional | ||
|
||
from aws_lambda_powertools.event_handler import CORSConfig | ||
from aws_lambda_powertools.event_handler.api_gateway import ( | ||
ApiGatewayResolver, | ||
ProxyEventType, | ||
) | ||
from aws_lambda_powertools.utilities.data_classes import VPCLatticeEvent | ||
|
||
|
||
class VPCLatticeResolver(ApiGatewayResolver): | ||
"""VPC Lattice resolver | ||
|
||
Documentation: | ||
- https://docs.aws.amazon.com/lambda/latest/dg/services-vpc-lattice.html | ||
- https://docs.aws.amazon.com/lambda/latest/dg/services-vpc-lattice.html#vpc-lattice-receiving-events | ||
|
||
Examples | ||
-------- | ||
Simple example integrating with Tracer | ||
|
||
```python | ||
from aws_lambda_powertools import Tracer | ||
from aws_lambda_powertools.event_handler import VPCLatticeResolver | ||
|
||
tracer = Tracer() | ||
app = VPCLatticeResolver() | ||
|
||
@app.get("/get-call") | ||
def simple_get(): | ||
return {"message": "Foo"} | ||
|
||
@app.post("/post-call") | ||
def simple_post(): | ||
post_data: dict = app.current_event.json_body | ||
return {"message": post_data} | ||
|
||
@tracer.capture_lambda_handler | ||
def lambda_handler(event, context): | ||
return app.resolve(event, context) | ||
""" | ||
|
||
current_event: VPCLatticeEvent | ||
|
||
def __init__( | ||
self, | ||
cors: Optional[CORSConfig] = None, | ||
debug: Optional[bool] = None, | ||
serializer: Optional[Callable[[Dict], str]] = None, | ||
strip_prefixes: Optional[List[str]] = None, | ||
): | ||
"""Amazon VPC Lattice resolver""" | ||
super().__init__(ProxyEventType.VPCLatticeEvent, cors, debug, serializer, strip_prefixes) |
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
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
15 changes: 15 additions & 0 deletions
15
examples/event_handler_rest/src/getting_started_vpclattice_resolver.json
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 | ||
} |
28 changes: 28 additions & 0 deletions
28
examples/event_handler_rest/src/getting_started_vpclattice_resolver.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,28 @@ | ||
import requests | ||
from requests import Response | ||
|
||
from aws_lambda_powertools import Logger, Tracer | ||
from aws_lambda_powertools.event_handler import VPCLatticeResolver | ||
from aws_lambda_powertools.logging import correlation_paths | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
tracer = Tracer() | ||
logger = Logger() | ||
app = VPCLatticeResolver() | ||
|
||
|
||
@app.get("/todos") | ||
@tracer.capture_method | ||
def get_todos(): | ||
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos") | ||
todos.raise_for_status() | ||
|
||
# for brevity, we'll limit to the first 10 only | ||
return {"todos": todos.json()[:10]} | ||
|
||
|
||
# You can continue to use other utilities just as before | ||
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPLICATION_LOAD_BALANCER) | ||
@tracer.capture_lambda_handler | ||
def lambda_handler(event: dict, context: LambdaContext) -> dict: | ||
return app.resolve(event, context) |
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,9 @@ | ||
import json | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def json_dump(): | ||
# our serializers reduce length to save on costs; fixture to replicate separators | ||
return lambda obj: json.dumps(obj, separators=(",", ":")) |
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,77 @@ | ||
from aws_lambda_powertools.event_handler import ( | ||
Response, | ||
VPCLatticeResolver, | ||
content_types, | ||
) | ||
from aws_lambda_powertools.event_handler.api_gateway import CORSConfig | ||
from aws_lambda_powertools.utilities.data_classes import VPCLatticeEvent | ||
from tests.functional.utils import load_event | ||
|
||
|
||
def test_vpclattice_event(): | ||
# GIVEN an Application Load Balancer proxy type event | ||
leandrodamascena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
app = VPCLatticeResolver() | ||
|
||
@app.get("/testpath") | ||
def foo(): | ||
assert isinstance(app.current_event, VPCLatticeEvent) | ||
assert app.lambda_context == {} | ||
return Response(200, content_types.TEXT_HTML, "foo") | ||
|
||
# WHEN calling the event handler | ||
result = app(load_event("vpcLatticeEvent.json"), {}) | ||
|
||
# THEN process event correctly | ||
# AND set the current_event type as ALBEvent | ||
leandrodamascena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert result["statusCode"] == 200 | ||
assert result["headers"]["Content-Type"] == content_types.TEXT_HTML | ||
assert result["body"] == "foo" | ||
|
||
|
||
def test_vpclattice_event_path_trailing_slash(json_dump): | ||
# GIVEN an Application Load Balancer proxy type event | ||
leandrodamascena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
app = VPCLatticeResolver() | ||
|
||
@app.get("/testpath") | ||
def foo(): | ||
assert isinstance(app.current_event, VPCLatticeEvent) | ||
assert app.lambda_context == {} | ||
return Response(200, content_types.TEXT_HTML, "foo") | ||
|
||
# WHEN calling the event handler using path with trailing "/" | ||
result = app(load_event("vpcLatticeEventPathTrailingSlash.json"), {}) | ||
|
||
# THEN | ||
assert result["statusCode"] == 404 | ||
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON | ||
expected = {"statusCode": 404, "message": "Not found"} | ||
assert result["body"] == json_dump(expected) | ||
|
||
|
||
def test_cors_preflight_body_is_empty_not_null(): | ||
# GIVEN CORS is configured | ||
app = VPCLatticeResolver(cors=CORSConfig()) | ||
|
||
event = {"raw_path": "/my/request", "method": "OPTIONS", "headers": {}} | ||
|
||
# WHEN calling the event handler | ||
result = app(event, {}) | ||
|
||
# THEN there body should be empty strings | ||
assert result["body"] == "" | ||
|
||
|
||
def test_vpclattice_url_no_matches(): | ||
# GIVEN a Lambda Function Url type event | ||
leandrodamascena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
app = VPCLatticeResolver() | ||
|
||
@app.post("/no_match") | ||
def foo(): | ||
raise RuntimeError() | ||
|
||
# WHEN calling the event handler | ||
result = app(load_event("vpcLatticeEvent.json"), {}) | ||
|
||
# THEN process event correctly | ||
# AND return 404 because the event doesn't match any known route | ||
assert result["statusCode"] == 404 |
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.