-
Notifications
You must be signed in to change notification settings - Fork 402
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
feat(data_classes): add KinesisFirehoseEvent #1540
Merged
leandrodamascena
merged 17 commits into
aws-powertools:develop
from
ryandeivert:ryandeivert-firehose-event
Oct 17, 2022
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ec35c7c
add KinesisFirehoseEvent data class
ryandeivert ab36af5
add tests for KinesisFirehoseEvent
ryandeivert 4855551
update docs for KinesisFirehoseEvent
ryandeivert c9b93fc
mypy fixes
ryandeivert 642734f
fixup mypy
ryandeivert ebe6714
fix(docs): move firehose sample to examples
rubenfonseca 2416d8d
Update aws_lambda_powertools/utilities/data_classes/kinesis_firehose_…
leandrodamascena e8e6417
Update aws_lambda_powertools/utilities/data_classes/kinesis_firehose_…
leandrodamascena 237f96d
docs fix
ryandeivert a9b24dd
optionals update
ryandeivert cf13bb8
test updates
ryandeivert 085873a
rm tests
ryandeivert f04a81b
Merge branch 'develop' into ryandeivert-firehose-event
ryandeivert e10f62a
formatting nit
ryandeivert b95cd97
firehose kinesis + put test updates
ryandeivert a3d1410
test fixes
ryandeivert 2ab66fc
feat(dataclass): fix test
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 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
113 changes: 113 additions & 0 deletions
113
aws_lambda_powertools/utilities/data_classes/kinesis_firehose_event.py
This file contains 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,113 @@ | ||
import base64 | ||
import json | ||
from typing import Iterator, Optional | ||
|
||
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper | ||
|
||
|
||
class KinesisFirehoseRecordMetadata(DictWrapper): | ||
@property | ||
def _metadata(self) -> dict: | ||
"""Optional: metadata associated with this record; present only when Kinesis Stream is source""" | ||
return self["kinesisRecordMetadata"] # could raise KeyError | ||
|
||
@property | ||
def shard_id(self) -> str: | ||
"""Kinesis stream shard ID; present only when Kinesis Stream is source""" | ||
return self._metadata["shardId"] | ||
|
||
@property | ||
def partition_key(self) -> str: | ||
"""Kinesis stream partition key; present only when Kinesis Stream is source""" | ||
return self._metadata["partitionKey"] | ||
|
||
@property | ||
def approximate_arrival_timestamp(self) -> int: | ||
"""Kinesis stream approximate arrival ISO timestamp; present only when Kinesis Stream is source""" | ||
return self._metadata["approximateArrivalTimestamp"] | ||
|
||
@property | ||
def sequence_number(self) -> str: | ||
"""Kinesis stream sequence number; present only when Kinesis Stream is source""" | ||
return self._metadata["sequenceNumber"] | ||
|
||
@property | ||
def subsequence_number(self) -> str: | ||
"""Kinesis stream sub-sequence number; present only when Kinesis Stream is source | ||
|
||
Note: this will only be present for Kinesis streams using record aggregation | ||
""" | ||
return self._metadata["subsequenceNumber"] | ||
|
||
|
||
class KinesisFirehoseRecord(DictWrapper): | ||
@property | ||
def approximate_arrival_timestamp(self) -> int: | ||
"""The approximate time that the record was inserted into the delivery stream""" | ||
return self["approximateArrivalTimestamp"] | ||
|
||
@property | ||
def record_id(self) -> str: | ||
"""Record ID; uniquely identifies this record within the current batch""" | ||
return self["recordId"] | ||
|
||
@property | ||
def data(self) -> str: | ||
"""The data blob, base64-encoded""" | ||
return self["data"] | ||
|
||
@property | ||
def metadata(self) -> Optional[KinesisFirehoseRecordMetadata]: | ||
"""Optional: metadata associated with this record; present only when Kinesis Stream is source""" | ||
return KinesisFirehoseRecordMetadata(self._data) if self.get("kinesisRecordMetadata") else None | ||
|
||
@property | ||
def data_as_bytes(self) -> bytes: | ||
"""Decoded base64-encoded data as bytes""" | ||
return base64.b64decode(self.data) | ||
|
||
@property | ||
def data_as_text(self) -> str: | ||
"""Decoded base64-encoded data as text""" | ||
return self.data_as_bytes.decode("utf-8") | ||
|
||
@property | ||
def data_as_json(self) -> dict: | ||
"""Decoded base64-encoded data loaded to json""" | ||
if self._json_data is None: | ||
self._json_data = json.loads(self.data_as_text) | ||
return self._json_data | ||
|
||
|
||
class KinesisFirehoseEvent(DictWrapper): | ||
"""Kinesis Data Firehose event | ||
|
||
Documentation: | ||
-------------- | ||
- https://docs.aws.amazon.com/lambda/latest/dg/services-kinesisfirehose.html | ||
""" | ||
|
||
@property | ||
def invocation_id(self) -> str: | ||
"""Unique ID for for Lambda invocation""" | ||
return self["invocationId"] | ||
|
||
@property | ||
def delivery_stream_arn(self) -> str: | ||
"""ARN of the Firehose Data Firehose Delivery Stream""" | ||
return self["deliveryStreamArn"] | ||
|
||
@property | ||
def source_kinesis_stream_arn(self) -> Optional[str]: | ||
"""ARN of the Kinesis Stream; present only when Kinesis Stream is source""" | ||
return self.get("sourceKinesisStreamArn") | ||
|
||
@property | ||
def region(self) -> str: | ||
"""AWS region where the event originated eg: us-east-1""" | ||
return self["region"] | ||
|
||
@property | ||
def records(self) -> Iterator[KinesisFirehoseRecord]: | ||
for record in self["records"]: | ||
yield KinesisFirehoseRecord(record) |
This file contains 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
25 changes: 25 additions & 0 deletions
25
examples/event_sources/src/kinesis_firehose_delivery_stream.py
This file contains 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,25 @@ | ||
import base64 | ||
import json | ||
|
||
from aws_lambda_powertools.utilities.data_classes import KinesisFirehoseEvent, event_source | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
|
||
@event_source(data_class=KinesisFirehoseEvent) | ||
def lambda_handler(event: KinesisFirehoseEvent, context: LambdaContext): | ||
result = [] | ||
|
||
for record in event.records: | ||
# if data was delivered as json; caches loaded value | ||
data = record.data_as_json | ||
|
||
processed_record = { | ||
"recordId": record.record_id, | ||
"data": base64.b64encode(json.dumps(data).encode("utf-8")), | ||
"result": "Ok", | ||
} | ||
|
||
result.append(processed_record) | ||
|
||
# return transformed records | ||
return {"records": result} |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to review this piece of code.
This is not your fault, we have this method/property as default in all classes and this can be a problem for records that allow non-json data like
firehose put
is and maybe others.. Check this outWe'll discuss this in our daily sync on Monday and I back here with updates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EAFP