What is RDS ?
Amazon Relational Database Service (RDS) is a managed database service provided by Amazon Web Services (AWS) that simplifies the setup, operation, and scaling of a relational database in the cloud. It offers cost-efficient and resizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching, and backups.
RDS supports several database instance types - optimized for memory, performance, or I/O - and provides six familiar database engines to choose from, including:
Amazon Aurora
PostgreSQL
MySQL
MariaDB
Oracle Database
SQL Server
What is Dynamodb?
DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services (AWS) that is designed for speed and flexibility. It supports both key-value and document data models, making it a versatile choice for a wide range of applications. Here are some key features and concepts related to DynamoDB:
Fully Managed
Performance at Scale
Flexible Data Model
Security
Global Tables
Provisioned and On-Demand Capacity
Use Cases: It's suited for a variety of applications, including mobile apps, gaming, IoT, and others that require low-latency data access at any scale.
What is AWS Lambda ?
AWS Lambda is a serverless compute service provided by Amazon Web Services (AWS) that allows you to run code without provisioning or managing servers. With Lambda, you can execute code for virtually any type of application or backend service with zero administration. Just upload your code, and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.
How to use lambda function to insert value and delete values in table using lambda function?
Firstly create a table using dynamodb. Enter name of table and partition_key.
The partition_key (also known as the hash key) is a core component in the architecture of many distributed data stores, including Amazon DynamoDB. Its primary purpose is to ensure data distribution across multiple nodes for scalability and availability.
Now search for lambda function service in aws dashboard and create a function as following values. and create a function.
now you can able to see a dasboard like below:-
now in configuration go to permission and edit that select the role which you have created using IAM role to access dynamodb. and save the details.
after that write the code as below :-
CODE TO INSERT VALUES IN TABLE USING EVENT
import json import boto3
Initialize a boto3 client for DynamoDB
dynamodb = boto3.resource('dynamodb', region_name='us-east-2') # Specify your region table = dynamodb.Table('Student')
def lambda_handler(event, context): # Example event: { "student_id": "123", "name": "John Doe", "age": 20, "major": "Computer Science" }
# Insert the student record into the DynamoDB table response = table.put_item(Item=event)
return { 'statusCode': 200, 'body': json.dumps('Student record inserted successfully!') }
Now when you write the code and try to test it will create an event by which we can add the values in table.
as you can status is succeeded that means our code run successfully.
now go to dynamodb you will be able to see added values in table.
Now i am adding more values to show how can we delete the values.
CODE TO DELETE VALUES IN TABLE WITHOUT USING EVENT
import boto3
Initialize a boto3 client for DynamoDB
dynamodb = boto3.resource('dynamodb', region_name='us-east-2') # Specify your region table = dynamodb.Table('Student')
def lambda_handler(event, context): # Specify the student_id of the item to delete student_id_to_delete = '1' # Change this to the ID of the student you want to delete
# Delete the item from the DynamoDB table response = table.delete_item( Key={ 'student_id': student_id_to_delete } )
return { 'statusCode': 200, 'body': f"Student with ID {student_id_to_delete} deleted successfully." }
FOR NOT USING EVENT IN FUNCTION YOU HAVE TO SPECIFY WHICH VALUE TO BE DELETED. BUT IF YOU WANT TO DELETE VALUES IN LARGE NUMBERS YOU CAN USE EVENT AS BELOW
import json import boto3
Initialize a Boto3 client for DynamoDB
dynamodb = boto3.resource('dynamodb', region_name='us-east-2) # Adjust the region accordingly table = dynamodb.Table('Student')
def lambda_handler(event, context): # Assume event contains {"student_id": ""} student_id = event.get('student_id')
# Delete the student record from the DynamoDB table response = table.delete_item( Key={ 'student_id': student_id } )
return { 'statusCode': 200, 'body': json.dumps(f'Successfully deleted student record with ID {student_id}') }
As you can see he code is Successfully executed now student_id:3 is deleted from table of dynamodb.
Thank You all for reading till here.
I hope you liked it ๐.