-
Notifications
You must be signed in to change notification settings - Fork 434
feat: Add Kinesis lambda event support to Parser utility #227
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
Changes from 1 commit
33f07b9
4716aa3
9b55817
c822713
3809763
43e175d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
from .base import BaseEnvelope | ||
from .dynamodb import DynamoDBStreamEnvelope | ||
from .event_bridge import EventBridgeEnvelope | ||
from .kinesis import KinesisEnvelope | ||
from .sns import SnsEnvelope | ||
from .sqs import SqsEnvelope | ||
|
||
__all__ = ["DynamoDBStreamEnvelope", "EventBridgeEnvelope", "SnsEnvelope", "SqsEnvelope", "BaseEnvelope"] | ||
__all__ = [ | ||
"DynamoDBStreamEnvelope", | ||
"EventBridgeEnvelope", | ||
"KinesisEnvelope", | ||
"SnsEnvelope", | ||
"SqsEnvelope", | ||
"BaseEnvelope", | ||
] |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,43 @@ | ||||||
import logging | ||||||
from typing import Any, Dict, List, Optional, Union | ||||||
|
||||||
from ..models import KinesisStreamModel | ||||||
from ..types import Model | ||||||
from .base import BaseEnvelope | ||||||
|
||||||
logger = logging.getLogger(__name__) | ||||||
|
||||||
|
||||||
class KinesisEnvelope(BaseEnvelope): | ||||||
ran-isenberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
"""Kinesis Envelope to extract array of Records | ||||||
ran-isenberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
The record's data parameter is a base64 encoded string which is parsed into a bytes array, | ||||||
though it can also be a JSON encoded string. | ||||||
Regardless of its type it'll be parsed into a BaseModel object. | ||||||
|
||||||
Note: Records will be parsed the same way so if model is str, | ||||||
all items in the list will be parsed as str and npt as JSON (and vice versa) | ||||||
""" | ||||||
|
||||||
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Model) -> List[Optional[Model]]: | ||||||
"""Parses records found with model provided | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
data : Dict | ||||||
Lambda event to be parsed | ||||||
model : Model | ||||||
Data model provided to parse after extracting data using envelope | ||||||
|
||||||
Returns | ||||||
------- | ||||||
List | ||||||
List of records parsed with model provided | ||||||
""" | ||||||
logger.debug(f"Parsing incoming data with Kinesis model {KinesisStreamModel}") | ||||||
parsed_envelope: KinesisStreamModel = KinesisStreamModel.parse_obj(data) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
output = [] | ||||||
logger.debug(f"Parsing Kinesis records in `body` with {model}") | ||||||
for record in parsed_envelope.Records: | ||||||
output.append(self._parse(data=record.kinesis.data.decode("utf-8"), model=model)) | ||||||
return output |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,37 @@ | ||||||
import base64 | ||||||
from binascii import Error as BinAsciiError | ||||||
from typing import List | ||||||
|
||||||
from pydantic import BaseModel, validator | ||||||
from pydantic.types import PositiveInt | ||||||
from typing_extensions import Literal | ||||||
|
||||||
|
||||||
class KinesisStreamRecordPayload(BaseModel): | ||||||
ran-isenberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
kinesisSchemaVersion: str | ||||||
partitionKey: str | ||||||
sequenceNumber: PositiveInt | ||||||
data: bytes # base64 encoded str is parsed into bytes | ||||||
approximateArrivalTimestamp: float | ||||||
|
||||||
@validator("data", pre=True) | ||||||
def data_base64_decode(cls, value): | ||||||
try: | ||||||
return base64.b64decode(value) | ||||||
ran-isenberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
except (BinAsciiError, TypeError): | ||||||
raise ValueError("base64 decode failed") | ||||||
|
||||||
|
||||||
class KinesisStreamRecord(BaseModel): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
eventSource: Literal["aws:kinesis"] | ||||||
eventVersion: str | ||||||
eventID: str | ||||||
eventName: Literal["aws:kinesis:record"] | ||||||
invokeIdentityArn: str | ||||||
awsRegion: str | ||||||
eventSourceARN: str | ||||||
kinesis: KinesisStreamRecordPayload | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
|
||||||
class KinesisStreamModel(BaseModel): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Records: List[KinesisStreamRecord] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,106 @@ | ||||||
from typing import Any, List | ||||||
|
||||||
import pytest | ||||||
|
||||||
from aws_lambda_powertools.utilities.parser import ValidationError, envelopes, event_parser | ||||||
from aws_lambda_powertools.utilities.parser.models import KinesisStreamModel, KinesisStreamRecordPayload | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||||||
from tests.functional.parser.schemas import MyKinesisBusiness | ||||||
from tests.functional.parser.utils import load_event | ||||||
|
||||||
|
||||||
@event_parser(model=MyKinesisBusiness, envelope=envelopes.KinesisEnvelope) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
def handle_kinesis(event: List[MyKinesisBusiness], _: LambdaContext): | ||||||
assert len(event) == 1 | ||||||
record: KinesisStreamModel = event[0] | ||||||
ran-isenberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
assert record.message == "test message" | ||||||
assert record.username == "test" | ||||||
|
||||||
|
||||||
@event_parser(model=KinesisStreamModel) | ||||||
ran-isenberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
def handle_kinesis_no_envelope(event: KinesisStreamModel, _: LambdaContext): | ||||||
ran-isenberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
records = event.Records | ||||||
assert len(records) == 2 | ||||||
record: KinesisStreamModel = records[0] | ||||||
ran-isenberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
assert record.awsRegion == "us-east-2" | ||||||
assert record.eventID == "shardId-000000000006:49590338271490256608559692538361571095921575989136588898" | ||||||
assert record.eventName == "aws:kinesis:record" | ||||||
assert record.eventSource == "aws:kinesis" | ||||||
assert record.eventSourceARN == "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream" | ||||||
assert record.eventVersion == "1.0" | ||||||
assert record.invokeIdentityArn == "arn:aws:iam::123456789012:role/lambda-role" | ||||||
|
||||||
kinesis: KinesisStreamRecordPayload = record.kinesis | ||||||
ran-isenberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
assert kinesis.approximateArrivalTimestamp == 1545084650.987 | ||||||
assert kinesis.kinesisSchemaVersion == "1.0" | ||||||
assert kinesis.partitionKey == "1" | ||||||
assert kinesis.sequenceNumber == 49590338271490256608559692538361571095921575989136588898 | ||||||
assert kinesis.data == b"Hello, this is a test." | ||||||
|
||||||
|
||||||
def test_kinesis_trigger_event(): | ||||||
event_dict = { | ||||||
"Records": [ | ||||||
{ | ||||||
"kinesis": { | ||||||
"kinesisSchemaVersion": "1.0", | ||||||
"partitionKey": "1", | ||||||
"sequenceNumber": "49590338271490256608559692538361571095921575989136588898", | ||||||
"data": "eyJtZXNzYWdlIjogInRlc3QgbWVzc2FnZSIsICJ1c2VybmFtZSI6ICJ0ZXN0In0=", | ||||||
"approximateArrivalTimestamp": 1545084650.987, | ||||||
}, | ||||||
"eventSource": "aws:kinesis", | ||||||
"eventVersion": "1.0", | ||||||
"eventID": "shardId-000000000006:49590338271490256608559692538361571095921575989136588898", | ||||||
"eventName": "aws:kinesis:record", | ||||||
"invokeIdentityArn": "arn:aws:iam::123456789012:role/lambda-role", | ||||||
"awsRegion": "us-east-2", | ||||||
"eventSourceARN": "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream", | ||||||
} | ||||||
] | ||||||
} | ||||||
|
||||||
handle_kinesis(event_dict, LambdaContext()) | ||||||
|
||||||
|
||||||
def test_kinesis_trigger_bad_base64_event(): | ||||||
event_dict = { | ||||||
"Records": [ | ||||||
{ | ||||||
"kinesis": { | ||||||
"kinesisSchemaVersion": "1.0", | ||||||
"partitionKey": "1", | ||||||
"sequenceNumber": "49590338271490256608559692538361571095921575989136588898", | ||||||
"data": "bad", | ||||||
"approximateArrivalTimestamp": 1545084650.987, | ||||||
}, | ||||||
"eventSource": "aws:kinesis", | ||||||
"eventVersion": "1.0", | ||||||
"eventID": "shardId-000000000006:49590338271490256608559692538361571095921575989136588898", | ||||||
"eventName": "aws:kinesis:record", | ||||||
"invokeIdentityArn": "arn:aws:iam::123456789012:role/lambda-role", | ||||||
"awsRegion": "us-east-2", | ||||||
"eventSourceARN": "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream", | ||||||
} | ||||||
] | ||||||
} | ||||||
with pytest.raises(ValidationError): | ||||||
handle_kinesis_no_envelope(event_dict, LambdaContext()) | ||||||
|
||||||
|
||||||
def test_kinesis_trigger_event_no_envelope(): | ||||||
event_dict = load_event("kinesisStreamEvent.json") | ||||||
handle_kinesis_no_envelope(event_dict, LambdaContext()) | ||||||
|
||||||
|
||||||
def test_validate_event_does_not_conform_with_model_no_envelope(): | ||||||
event_dict: Any = {"hello": "s"} | ||||||
with pytest.raises(ValidationError): | ||||||
handle_kinesis_no_envelope(event_dict, LambdaContext()) | ||||||
|
||||||
|
||||||
def test_validate_event_does_not_conform_with_model(): | ||||||
event_dict: Any = {"hello": "s"} | ||||||
with pytest.raises(ValidationError): | ||||||
handle_kinesis(event_dict, LambdaContext()) |
Uh oh!
There was an error while loading. Please reload this page.