Writing to DynamoDB From Code
Start course
3h 3m

This course provides detail on the AWS Database services relevant to the AWS Certified Developer - Associate exam. This includes Amazon RDS, Aurora, DynamoDB, MemoryDB for Redis, and ElastiCache.

Want more? Try a lab playground or do a Lab Challenge!

Learning Objectives

  • Obtain a solid understanding of the following Amazon database services: Amazon RDS, Aurora, DynamoDB, MemoryDB for Redis, and ElastiCache
  • Create an Amazon RDS database
  • Create a DynamoDB database
  • Create an ElastiCache cluster

Let's take a look at a write operation into a DynamoDB table, this time from code using AWS Lambda and Python. Here we have our code in the Lambda Console and I'm populating a data structure that I'm going to use as a record to put into our table. What I'm using is the event. In this case, you know, it's a Lambda. So, it's going to get called perhaps by an S3 event, a step function, or something similar. So, let me show you, I'll go to 'Configure test'. And as you can see as the event, I'm just passing the exact same structure that we would use if we were populating this record directly in the DynamoDB Console. 

Let's go back here. Our code creates a data structure and populates the event just as I mentioned. And then, we forget about that and then we switch over to our Boto3 configuration. In this case, we request a resource from  Boto3, which is a DynamoDB resource. Once we have that, we ask for a table connection which we do here in line 17, we just pass the table name on the DynamoDB resource that we created will give us an open connection to that table. And finally, this is where you would put your real code here. In this case, I'm just calling the PutItem method, and I'm passing the item parameter with the value of our data structure.

So, if this was real code, this is where you would have all your operation, perhaps you would sanitize your data first and do other kinds of operations, perhaps a lot more PutItem calls other than just one. So, here's our table. As you can see, we have four items here. And let's go back, let me show you the record one more time. I'm passing the ID 89 and some fake values here. I'll go ahead and run it. As you can see there's no errors here. Let's go back to our DynamoDB console. I'm  going to refresh this. As you can see, we only have four items here, I'll refresh and there's our record. I'm going to click on it and switch to  JSON view. As you can see, our record is here and our code worked as expected. Of course, writing a bunch of data is completely useless unless we actually know how to later retrieve and look for items and query specific records. And I have that solution ready for you with the Lambda that scans and queries our table.


About the Author
Learning Paths

Stuart has been working within the IT industry for two decades covering a huge range of topic areas and technologies, from data center and network infrastructure design, to cloud architecture and implementation.

To date, Stuart has created 150+ courses relating to Cloud reaching over 180,000 students, mostly within the AWS category and with a heavy focus on security and compliance.

Stuart is a member of the AWS Community Builders Program for his contributions towards AWS.

He is AWS certified and accredited in addition to being a published author covering topics across the AWS landscape.

In January 2016 Stuart was awarded ‘Expert of the Year Award 2015’ from Experts Exchange for his knowledge share within cloud services to the community.

Stuart enjoys writing about cloud technologies and you will find many of his articles within our blog pages.