Skip to content

Commit 346850d

Browse files
author
Ran Isenberg
committed
feat: Add alb lambda event support to Parser utility #228
1 parent fa34d82 commit 346850d

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

aws_lambda_powertools/utilities/parser/models/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
from .alb import AlbModel, AlbRequestContext, AlbRequestContextData
12
from .dynamodb import DynamoDBStreamChangedRecordModel, DynamoDBStreamModel, DynamoDBStreamRecordModel
23
from .event_bridge import EventBridgeModel
34
from .ses import SesModel, SesRecordModel
45
from .sns import SnsModel, SnsNotificationModel, SnsRecordModel
56
from .sqs import SqsModel, SqsRecordModel
67

78
__all__ = [
9+
"AlbModel",
10+
"AlbRequestContext",
11+
"AlbRequestContextData",
812
"DynamoDBStreamModel",
913
"EventBridgeModel",
1014
"DynamoDBStreamChangedRecordModel",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Dict
2+
3+
from pydantic import BaseModel
4+
5+
6+
class AlbRequestContextData(BaseModel):
7+
targetGroupArn: str
8+
9+
10+
class AlbRequestContext(BaseModel):
11+
elb: AlbRequestContextData
12+
13+
14+
class AlbModel(BaseModel):
15+
httpMethod: str
16+
path: str
17+
body: str
18+
isBase64Encoded: bool
19+
headers: Dict[str, str]
20+
queryStringParameters: Dict[str, str]
21+
requestContext: AlbRequestContext

tests/functional/parser/test_alb.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
3+
from aws_lambda_powertools.utilities.parser import ValidationError, event_parser
4+
from aws_lambda_powertools.utilities.parser.models import AlbModel
5+
from aws_lambda_powertools.utilities.typing import LambdaContext
6+
from tests.functional.parser.utils import load_event
7+
8+
9+
@event_parser(model=AlbModel)
10+
def handle_alb(event: AlbModel, _: LambdaContext):
11+
assert (
12+
event.requestContext.elb.targetGroupArn
13+
== "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/lambda-279XGJDqGZ5rsrHC2Fjr/49e9d65c45c6791a" # noqa E501
14+
)
15+
assert event.httpMethod == "GET"
16+
assert event.path == "/lambda"
17+
assert event.queryStringParameters == {"query": "1234ABCD"}
18+
assert event.headers == {
19+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
20+
"accept-encoding": "gzip",
21+
"accept-language": "en-US,en;q=0.9",
22+
"connection": "keep-alive",
23+
"host": "lambda-alb-123578498.us-east-2.elb.amazonaws.com",
24+
"upgrade-insecure-requests": "1",
25+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36", # noqa E501
26+
"x-amzn-trace-id": "Root=1-5c536348-3d683b8b04734faae651f476",
27+
"x-forwarded-for": "72.12.164.125",
28+
"x-forwarded-port": "80",
29+
"x-forwarded-proto": "http",
30+
"x-imforwards": "20",
31+
}
32+
assert event.body == "Test"
33+
assert not event.isBase64Encoded
34+
35+
36+
def test_alb_trigger_event():
37+
event_dict = load_event("albEvent.json")
38+
handle_alb(event_dict, LambdaContext())
39+
40+
41+
def test_validate_event_does_not_conform_with_model():
42+
event = {"invalid": "event"}
43+
with pytest.raises(ValidationError):
44+
handle_alb(event, LambdaContext())

0 commit comments

Comments
 (0)