Tuesday, 30 May 2023

Insert Data Using AWS Lambda

 In this article, we will look into the process of inserting data into a DynamoDB table using AWS Lambda. Amazon DynamoDB is a completely owned NoSQL proprietary provider that helps key-value and textual statistics systems and is supplied via way of means of Amazon.com as a part of Amazon Web Services. AWS Lambda is an event-driven, serverless computing platform supplied via way means of Amazon as part of Amazon Web Services. It is a computing service that runs code without us worrying about servers. Users can simply use the AWS Lambda function so as to process the records in an Amazon DynamoDB stream. With the help of DynamoDB Streams, Users can easily trigger the Lambda function so as to perform additional work every time a DynamoDB table will be updated. Some permissions are also needed  to manage the resources related to your DynamoDB stream like dynamodb:GetRecords, dynamodb:GetShardIterator, dynamodb:ListStreams and dynamodb:DescribeStream. User have to Add this into their function’s execution role. Users can create an event for source mapping to tell that their  Lambda sends the records from your stream to a Lambda function. By default, the Lambda can invokes your function as soon as the records are available. The Lambda polls also shards in your DynamoDB stream service for the records having base rate of 4 times per second.

Implementation:

Follow the below steps to insert data into the DynamoDB table using AWS lambda:

Step 1: Login into AWS console.

Step 2: Search for dynamodb.

 

Step 3: Select Dynamodb and press on create table

 

Step 4: Now give the table name and keys accordingly to your requirement 

 

Now table will be created.

Step 5: Now we need to create Identity and Access Management(IAM) role for that go and search for IAM role.

Step 6: Click on role in access management and click on create role.

 

 

Step 7: Here we need to select AWS service and lambda.

 

Step 8: Here we need to add permission, as we are using dynamo db we need to add AmazonDynamoDBFullAccess Permissions policies

 

Step 9: Now give the role name and select create role

 

Step 10: Press on create function.

 

Step 11: Give name and Runtime.

 

Step 12: Change the Execution role to Use an existing role and select your role.

 

Step 13: Now go to the code section and add the below code.

#importing packages
import json
import boto3
#function definition
def lambda_handler(event,context):
    dynamodb = boto3.resource('dynamodb')
    #table name
    table = dynamodb.Table('sample')
    #inserting values into table
    response = table.put_item(
       Item={
            'sample': 'bhagi',
             
        }
    )
    return response

Output:


No comments:

Post a Comment