Performing CRUD Operations on DynamoDB using Lambda Function

Performing CRUD Operations on DynamoDB using Lambda Function

Hello connections , I want to share my knowledge regarding AWS in my posts. In this article i want to explain you all about "Implementation of CRUD operations on Dynamo DB and AWS lambda" .

CRUD OPERATIONS means Creating , Retrieving , Updating and Deleting.

I want to explain step by step process to order to make you understand clearly!!

First Lets create a DynamoDB Table with name as "studenttable" and also partition key as"sid" and sort key as "sname". In any DynamoDB table , partition key is also considered as primary key which is unique. DynamoDB is a NOSQL Database where we store Unstructured data.

No alt text provided for this image
here , we are creating a table with table name , partition key and sort key
No alt text provided for this image
leave remaining settings as default and create the table
No alt text provided for this image
wait till you see the status of table as active

Now, Lets create a Lambda function

No alt text provided for this image

click on create function

No alt text provided for this image

we will use a predefined template hello-world-function with python 3.7 --> configure

Let's create a IAM role to give permissions for Lambda to deal with DynamoDB

No alt text provided for this image

choose Lambda as use case --> next

No alt text provided for this image

choose AmazonDynamoDBFullAccess as permission policies--> next

No alt text provided for this image

give a role name for the role which we created

No alt text provided for this image

our role is created successfully !!! Hurrah !! ...Lets go back to our Lambda function .

No alt text provided for this image

give function name , choose "use an existing role" --> choose the role that we created just now.

No alt text provided for this image

leave the remaining settings as default and click on create function.

No alt text provided for this image

write code for inserting the record into dynamodb table.

No alt text provided for this image

create a new demo event and give the values which we want to be inserted into the table.

No alt text provided for this image

click on test button to see the sucess msg.

No alt text provided for this image

Now, lets check our dynamoDB table , to check whether the record insewrted or not. We got it !! We can see the record which we given in demoevent.

No alt text provided for this image

simillarly , Lets insert another record in our table.

No alt text provided for this image

Lets write a code to deelete the record and we should provide the partition key and sort key of the record which we want to dlt in the test tab . Then click on red Test button to see our code execution.

No alt text provided for this image

we can see that , our code compiled successfully!!

No alt text provided for this image

we can see that record got dlted .

No alt text provided for this image
No alt text provided for this image

Lets write code to display the items in the table , we can see the records which are in the table.

No alt text provided for this image
Lets create an item manually to demonstrate update operation , we cant update Partition key
No alt text provided for this image
No alt text provided for this image

code for updating a field in the table

No alt text provided for this image

in the demoevent provide parition and sort key of the record in which you want to update a filed.

No alt text provided for this image

Now, execute the code.

No alt text provided for this image

We can see the updated field in the dynamoDB!!! Congo , we have completed the CRUD operations implementations in DynamoDB.

To view or add a comment, sign in

More articles by K Lohith

Others also viewed

Explore content categories