Parsing nested envelopes? #2287
-
So we have payloads that are essentially Kinesis Firehouse events containing an SQS event. Is there a way to unwrap this using |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Hi @sk0g! Thank you for opening this discussion. Indeed this is a great improvement for those who use sqs events wrapped in firehose records. I created a PR (#2294) to add this new Model, but before merging this I would like to know how do you ingest SQS into Firehose. SQS can't send messages directly to Kinesis Firehose by default, so I assume you have a lambda/eventbridge/pipe/other intermediaries to get that message from SQS and ingest. How are you doing this? Is it one on one? Is it a batch? Could you show me a Kinesis Firehose record, please? It can be in base64 anyway, but please remove any sensitive data. Thank you. |
Beta Was this translation helpful? Give feedback.
-
That's right @leandrodamascena, we have EventBridge in between. The resulting JSON is something like this: {
...
"Records": [
{
"body": { "actual_data": "some_val" }
...
}
]
} Current flow is to handle it like this: for record in event["Records"]:
PydanticModel.parse_raw(event["body"])
# OR if no batching
assert len(event["Records"]) == 1
PydanticModel.parse_raw(event["Records"][0]["body"]) Depending on the event there might be no batching, so it's more straightforward to assert length is 1, and do something like parse. In other cases Firehose dumps JSON bodies without separation so we have an awkward workaround that's not too biting us right now. That's a more complicated case I'm not expecting to be solved by Lambda Powertools. Unless... 🙏 |
Beta Was this translation helpful? Give feedback.
-
It seems to me that the event sources can be combined in any way, and we're struggling to keep up with new combinations of possible pairs (or more) of nested envelopes. I propose we take some time to consider an alternative solution where we implement a What do you think of this @sk0g ? We can work on the developer experience to see if it makes sense. |
Beta Was this translation helpful? Give feedback.
-
Absolutely, I was thinking about that as this case might be common-ish, but in the grand scheme of things you are going to have a n^2 explosion of serialiser classes if you want to address other combinations as well. That's ignoring that you can probably end up with even more nesting... @rubenfonseca If this can be dynamic it would be brilliant. There's no accounting for what people create, so not having to create functionality for each combination should both provide a simpler API, as well as less feature/ maintenance burden (I hope). If I could suggest one thing though, with such parsing, testing the handler does become a bit trickier as you have to provide more conformant events. Maybe things like |
Beta Was this translation helpful? Give feedback.
It seems to me that the event sources can be combined in any way, and we're struggling to keep up with new combinations of possible pairs (or more) of nested envelopes.
I propose we take some time to consider an alternative solution where we implement a
PipelineEnvelope
and just add as many basic models as we want. The parser would then validate and unwrap the arbitrary combination of Envelopes, thus avoiding us to continue to create and test specific combinations.What do you think of this @sk0g ? We can work on the developer experience to see if it makes sense.