Thursday, 6 April 2017

Using AWS Lambda with AWS Dynamo DB





AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of the Amazon Web Services. It is a compute service that runs code in response to events and automatically manages the compute resources required by that code. AWS Lambda executes your code only when needed and scales automatically, All you need to do is supply your code in one of the languages that AWS Lambda supports (currently Node.js, Java, C#, and Python).



AWS Dynamo DB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.AWSDynamoDB lets you offload the administrative burdens of operating and scaling a distributed database so that you don't have to worry about hardware provisioning, setup, and configuration, replication, software patching, or cluster scaling.




Here in this documentation, I am going to provide a detailed explanation and step by step process of triggering AWS Lambda function to work with AWS Dynamo DB.

  • Create a Lambda function to process data to AWS Dynamo DB. Test the Lambda function by triggering it manually and checking the AWS Dynamo DB tables for the correctness of data insertion and deletion, using the AWS Management Console.

Create a Lambda Function and Invoke it Manually (Using the AWS Console)
  • We create and invoke lambda function.
  • Go to the AWS Management Console, select “Lambda” services.













































Note: Both the Lambda function and theDynamoDB must be in the same AWS region.

























  • Click on select blank function”. 
























Configure triggers:
  • You can choose to add a trigger that will invoke your function.   
  • Click on a box then drop down list will appear and select Dynamo DB”.






  • Select Dynamo DB Table Name, Starting Position.
  • Select Enable Trigger and Click on Next Button.




























Configure function:

  • Function Name: Give appropriate name to “Lambda function”.
  • Description: This should ideally tell what the function will do.
  • Runtime:”Select programming language” given to the Drop down list.






  • Give “Handler name” and Choose the Role as “ Existing role”

  • Handler : same function name given to the Handler name.
  • Role: Choose a role in given to the drop down list.
  • Existing role:  Choose an existing role what we have required given to the drop down list.








  • Click on Create a Function.

























  • IAM Role: Select a new IAM Role.
  • Enter new IAM Role.
  • Click on Allow button to the next page.



























  • The policy document is mentioned below.











  • Lambda Function code is given below for your reference.

import boto3

       def lambda_handler(event, context):
       dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
       table = dynamodb.Table('User5')
       UserName = 'Bharath'
        json =[
       {
       'Date_Query': "20170220",
      'Hours': "5",
      'ProjectName': "AWS",
     'TimeStamp' : "20170220 19:30:22",
      'Date' : "20-02-2017"

       }
        ]


    for m in json :
   ProjectName = m['ProjectName']
   Date = m['Date']
   Hours = m['Hours']
  TimeStamp = m['TimeStamp']
  Date_Query = m['Date_Query']
  response = table.put_item(
  Item={
  'Username': UserName,
  'TimeStamp': TimeStamp,
  'Date': Date,
  'Hours': Hours,
  'ProjectName': ProjectName,
   'Date_Query' : Date_Query

  }
  )
 return json



  • Here click on the test which I have highlighted so that the code runs.


































  • Here Lambda code is successfully executed and return the values.

























  • Before update below table had one record of the user.














































  • Click on refresh button successfully one more record to be updated to the same user.







































  • Here Click 0n Monitoring then you can find:
  • Invocation count: It displays the number of invocations done by Dynamo DB.

    .
  • Invocation duration: It displays the duration of the invocation, If at all the invocation fails then it displays the number of invocations failed by Dynamo DB on Lambda function.

  • Throttled invocations: Request that gets invoked asynchronously can absorb reasonable bursts of traffic for approximately 15-30minutes. 































  •   Now the function reads incoming event data and writes logs to Amazon Cloud Watch.





























  •    Now clearly watch the log entries and time stamp.











ConclusionAs mentioned in the above it is easy to connect AWS Lambda to Dynamo DB after establishing connection now it is easy to execute update and delete operations from Dynamo DB.
                    




                            Further any queries contact me,



Bharath Reddy

MOURI TECH Pvt Ltd

(M) +91-8008538004

www.mouritech.com







6 comments:

  1. Very Nice Information. This blog contains the information about Using AWS Lambda with AWS Dynamo db. This is very helpful to AWS users. Thank you so much for gathering all this information in one post.
    AWS Lambda Training

    ReplyDelete
  2. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
    Best aws training Institute in chennai

    ReplyDelete
  3. I am really happy with your blog because your article is very unique and powerful for new reader.
    AWS Training in Chennai

    ReplyDelete
  4. I read your blog it's very useful to everyone thanks for sharing for more updates AWS Online Training Hyderabad

    ReplyDelete
  5. It is really a great work and the way in which you are sharing the knowledge is excellent.
    aws training in omr | aws training in velachery | best aws training center in chennai

    ReplyDelete