Skip to content

feat: Add alb lambda event support to Parser utility #229

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 2 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aws_lambda_powertools/utilities/parser/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from .alb import AlbModel, AlbRequestContext, AlbRequestContextData
from .dynamodb import DynamoDBStreamChangedRecordModel, DynamoDBStreamModel, DynamoDBStreamRecordModel
from .event_bridge import EventBridgeModel
from .ses import SesModel, SesRecordModel
from .sns import SnsModel, SnsNotificationModel, SnsRecordModel
from .sqs import SqsModel, SqsRecordModel

__all__ = [
"AlbModel",
"AlbRequestContext",
"AlbRequestContextData",
"DynamoDBStreamModel",
"EventBridgeModel",
"DynamoDBStreamChangedRecordModel",
Expand Down
21 changes: 21 additions & 0 deletions aws_lambda_powertools/utilities/parser/models/alb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Dict

from pydantic import BaseModel


class AlbRequestContextData(BaseModel):
targetGroupArn: str


class AlbRequestContext(BaseModel):
elb: AlbRequestContextData


class AlbModel(BaseModel):
httpMethod: str
path: str
body: str
isBase64Encoded: bool
headers: Dict[str, str]
queryStringParameters: Dict[str, str]
requestContext: AlbRequestContext
44 changes: 44 additions & 0 deletions tests/functional/parser/test_alb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest

from aws_lambda_powertools.utilities.parser import ValidationError, event_parser
from aws_lambda_powertools.utilities.parser.models import AlbModel
from aws_lambda_powertools.utilities.typing import LambdaContext
from tests.functional.parser.utils import load_event


@event_parser(model=AlbModel)
def handle_alb(event: AlbModel, _: LambdaContext):
assert (
event.requestContext.elb.targetGroupArn
== "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/lambda-279XGJDqGZ5rsrHC2Fjr/49e9d65c45c6791a" # noqa E501
)
assert event.httpMethod == "GET"
assert event.path == "/lambda"
assert event.queryStringParameters == {"query": "1234ABCD"}
assert event.headers == {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"accept-encoding": "gzip",
"accept-language": "en-US,en;q=0.9",
"connection": "keep-alive",
"host": "lambda-alb-123578498.us-east-2.elb.amazonaws.com",
"upgrade-insecure-requests": "1",
"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
"x-amzn-trace-id": "Root=1-5c536348-3d683b8b04734faae651f476",
"x-forwarded-for": "72.12.164.125",
"x-forwarded-port": "80",
"x-forwarded-proto": "http",
"x-imforwards": "20",
}
assert event.body == "Test"
assert not event.isBase64Encoded


def test_alb_trigger_event():
event_dict = load_event("albEvent.json")
handle_alb(event_dict, LambdaContext())


def test_validate_event_does_not_conform_with_model():
event = {"invalid": "event"}
with pytest.raises(ValidationError):
handle_alb(event, LambdaContext())