Member-only story
Automated DynamoDB Data Marshaling
Marshal your own data when the higher level APIs will not.
If you’re familiar with DynamoDB, you know that transforming (or as AWS calls it, “marshaling”) your data into DynamoDB ‘s consumable form can be cumbersome.
Luckily for us developers, AWS offers higher level APIs that will handle data marshaling for us (see DocumentClient for NodeJS and DynamoDB Table for Python). So why am I writing this article?
In one of mypython
stacks, I needed to use DynamoDB transactional writes. While Node’s aws-sdk
has a higher level implementation of transactional data operations through the DocumentClient, Python’s boto3
does not.
In my particular case, any given transactional operation would involve 5–10 DynamoDB items whose structures were not identical. One option would be to check the type of item that I wanted to post and then manually create the DynamoDB marshalled data based on the item type. However, that process is messy and not very flexible — if one the item type’s data structures change even a little bit, I’d have to go back and adjust the way we marshal that item. To solve my problem, I created a function that would recursively “marshal” my data.