Saturday, 24 March 2018

How To Connect DynamoDB With AWS Lambda

Connect To DynamoDB With AWS Lambda


AWS lambda can be used to save your data to databases like MySQL, DynamoDB. If you don't want to manage your own application instance and MongoDB then you can opt for AWS DynamoDB which is AWS managed service for NoSQL database. AWS lambda will replace your application instance and you will be only charged for the number of execution whereas if you run your own application ec2 instance then you would be charged for every second the instance is up. So if you have a small application which can fit in lambda-dynamodb use-case then this could save your cost.
Let's see how to setup Lambda with DynamoDB:

Create DynamoDB Table

  • Go to DynamoDB Dashboard. 
  • Create a table, give it any name say, "myinfo"
  • Keep the "type" to string
  • Keep other settings as default
  • Click "create" button to create the table

  • After creating table, you will see the following table details


Create Lambda Function

  • Open Lambda Dashboard, click on "create function" button
  • Do not select any blueprint, click on "Author From Scratch" button


  • Give the function name "myinfo"
  • In Role section select "choose from existing role"
  • Then select "dynamo_access" role (This role is being already created by us. You need to create this explicitly by attaching dynamodb full access policy to this role)
  • Now click on "create function" button
  • Now in the function code, paste the code from the below gist
  • Select runtime as "Node.js 6.10" and leave handler as default
  • Now click on "save and test"
  • This will open a modal to create a test-event, give the event name myinfo.
  • Put the below data in the text-pane    -    {"name": "appychip"}
  • Now click on "create"

  • Click on "Test" button and you will see the lambda is getting executed and prints out "SUCCESS" if executed successfully.

  • You can also check DynamoDB table "myinfo", the entry for {"name": "appychip"} got created when you executed this function.


This is how you can play around and create your app using lambda and dynamodb. If you want to put an API endpoint for this Lambda function then you can watch our video tutorial and go through our blog-post.

4 comments:

  1. This is a fantastic guide on connecting DynamoDB with AWS Lambda! 🚀 The step-by-step instructions make it so much easier for someone like me who's relatively new to these integrations. The screenshots and explanations are spot-on, and I appreciate how you've broken down the process into manageable chunks.

    I was struggling a bit with this integration before stumbling upon your post. Your insights on configuring roles and permissions are especially helpful. It's clear that you've put a lot of effort into making this guide beginner-friendly.

    One thing I would love to see in future posts is maybe a troubleshooting section for common issues users might encounter during the setup. Nevertheless, kudos on the great work, and thanks for sharing your expertise!

    Looking forward to more insightful content from you.

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel