Amazon S3 to DynamoDB Automation

Export Object from S3 to DynamoDB using AWS Lambda

In a matter of seconds and without a zero cost using serverless Lambda, allows you to automatically send object from Amazon S3 to Amazon DynamoDB. In this blog, we are going to be creating an AWS Lambda in Python to automatically process any JSON files uploaded to a S3 bucket into a DynamoDB table.

Amazon S3

Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. This means customers of all sizes and industries can use it to store and protect any amount of data for a range of use cases, such as data lakes, websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics

Amazon DynamoDB

Amazon DynamoDB is a fully managed proprietary NoSQL database service that supports key–value and document data structures and is offered by Amazon.com as part of the Amazon Web Services portfolio. Hundreds of thousands of AWS customers have chosen DynamoDB as their key-value and document database for mobile, web, gaming, ad tech, IoT, and other applications that need low-latency data access at any scale. Create a new table for your application and let DynamoDB handle the rest.

Use Case

You might have a requirement to automate whenever a new JSON file is uploaded to S3 bucket then we need to keep the json object in the DynamoDB table. JSON Object cannot directly ingest to DynamoDB table but will have a logic in lambda.

Architectural design

s3_Dynamo
Automation Workflow


Workflow

  1. User/Application put the .json Object to S3 bucket
  2. Lambda will trigger when an .json Object is created in the given S3 bucket
  3. Lambda will convert and put the data in DynamoDB table

How to Setup this WorkFlow?

  • Create the Dynamo DB table, for this blog, I have named it as "employees"
DynamoDB_table

Note: If you want to change the DynamoDB table name then the same name should be passed in Lambda

  • Create a S3 bucket for streaming your JSON objects
  • Lambda IAM Role required below permission:
  • S3 get object and DynamoDB put object permission
  • AWSLambdaBasicExecutionRole (Managed policy)
  • To configure the Trigger, there are two ways, either we can configure in S3 bucket properties by creating an event notification or you can add the Trigger as S3 in lambda console.

    image
lambdatrigger


Testing

Upload a json object in the S3 bucket, and automatically that will put the object in NoSQL DynamoDB table.

  • Uploaded below json object in S3 bucket
    { "emp_id":"4", "Name":"TestUser2", "Age":26, "Location":["USA"] }
  • From DynamoDB table, we see item has been automatically added within few secondstestDB

Conclusion

As you can see with some very basic lambda code, we were able to move the data from S3 to DynamoDB table. Refer the boto3 documentation for proper syntax when you're configuring the individual python modules like boto3. 

Thank you for reading!

Comments

Popular posts from this blog

Connect to Linux EC2 Instance if Key pair is lost after Initial Launch

Setup Grafana on AWS EKS and integrate with AWS Cloudwatch

Start or Stop services in multiple Windows EC2 Instances using AWS Systems Manager

Concourse CI Installation and Configuration in Windows

Install SSM Agent in Amazon EC2 Instance

Automate Permission Boundary Attachment to IAM roles and Users

Deploy AWS infrastructure using Terraform and GitHub Actions

AWS Route 53 Inbound Resolver to resolve DNS for Multi Account Organization

Import Existing Resources using Terraform

Hosting AWS VPC Interface Endpoints in Shared Model