-
Notifications
You must be signed in to change notification settings - Fork 436
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
33f07b9
feat: Add Kinesis lambda event support to Parser utility
4716aa3
feat: Add Kinesis lambda event support to Parser utility
ran-isenberg 9b55817
cr fixes
c822713
Merge branch 'develop' into kinesis
heitorlessa 3809763
docs: add Kinesis Streams as a supported model & envelope
heitorlessa 43e175d
fix: s3 model import
heitorlessa 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
43 changes: 43 additions & 0 deletions
43
aws_lambda_powertools/utilities/parser/envelopes/kinesis.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,43 @@ | ||
import logging | ||
from typing import Any, Dict, List, Optional, Union | ||
|
||
from ..models import KinesisDataStreamModel | ||
from ..types import Model | ||
from .base import BaseEnvelope | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class KinesisDataStreamEnvelope(BaseEnvelope): | ||
"""Kinesis Data Stream Envelope to extract array of Records | ||
|
||
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 {KinesisDataStreamModel}") | ||
parsed_envelope: KinesisDataStreamModel = KinesisDataStreamModel.parse_obj(data) | ||
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 |
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,41 @@ | ||
import base64 | ||
import logging | ||
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 | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class KinesisDataStreamRecordPayload(BaseModel): | ||
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: | ||
logger.debug("Decoding base64 Kinesis data record before parsing") | ||
return base64.b64decode(value) | ||
except (BinAsciiError, TypeError): | ||
raise ValueError("base64 decode failed") | ||
|
||
|
||
class KinesisDataStreamRecord(BaseModel): | ||
eventSource: Literal["aws:kinesis"] | ||
eventVersion: str | ||
eventID: str | ||
eventName: Literal["aws:kinesis:record"] | ||
invokeIdentityArn: str | ||
awsRegion: str | ||
eventSourceARN: str | ||
kinesis: KinesisDataStreamRecordPayload | ||
|
||
|
||
class KinesisDataStreamModel(BaseModel): | ||
Records: List[KinesisDataStreamRecord] |
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,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 KinesisDataStreamModel, KinesisDataStreamRecordPayload | ||
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.KinesisDataStreamEnvelope) | ||
def handle_kinesis(event: List[MyKinesisBusiness], _: LambdaContext): | ||
assert len(event) == 1 | ||
record: KinesisDataStreamModel = event[0] | ||
assert record.message == "test message" | ||
assert record.username == "test" | ||
|
||
|
||
@event_parser(model=KinesisDataStreamModel) | ||
def handle_kinesis_no_envelope(event: KinesisDataStreamModel, _: LambdaContext): | ||
records = event.Records | ||
assert len(records) == 2 | ||
record: KinesisDataStreamModel = records[0] | ||
|
||
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: KinesisDataStreamRecordPayload = record.kinesis | ||
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()) |
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.