Exploring Databases in AWS: Relational vs. Non-Relational (AWS RDS and DynamoDB covered)
Cloud computing has transformed the way we manage data, with AWS offering powerful database solutions for developers worldwide. In this blog, let’s break down the key aspects of Amazon RDS and DynamoDB, two database services tailored for distinct use cases.
1. Relational Databases with Amazon RDS
Amazon RDS (Relational Database Service) supports popular engines like MySQL, PostgreSQL, and AuroraDB.
Key Features:
Snapshots: Automated backups for point-in-time recovery.
Multi-AZ DB Clusters: High availability and fault tolerance across Availability Zones.
Connecting Amazon RDS to EC2:
Create a Service Role: Assign permissions for RDS and CloudWatch.
Attach the Role to EC2: Enable EC2 to interact with RDS seamlessly.
Access RDS: Use security groups and proper configurations to ensure secure connectivity.
2. Non-Relational Databases with DynamoDB
DynamoDB is a fully managed serverless NoSQL database, ideal for low-latency applications.
Why Partition Key Matters:
The partition key is crucial for data distribution and retrieval. It ensures even data spread across partitions and efficient query performance.
Connecting DynamoDB via AWS Lambda:
AWS Lambda provides a serverless way to interact with DynamoDB. Here’s how to establish the connection and manage data:
Runtime: Python (using the Boto3 library).
Connection Methods:
Client: It will connect to a server.
Resource: It is the server itself.
AWS Lambda Function for DynamoDB:
Create a Service Role: Grant Lambda full access to DynamoDB and CloudWatch. Attach this role to your Lambda function. Lambda function to insert data in dynamoDB
import boto3
import json
# Initialize DynamoDB resource
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('learners')
items_to_insert ={
learner_id:event["learner_id"]
learner_name:event["learner_name"]
learner_income:event["learner_income"]
}
try:
response= table.put_item(Item=items_to_insert)
return {
'statusCode':200,
'body': json.dumps("items added successfully")
}
except Exception as e:
return {
'statusCode':500,
'body': json.dumps(str(e))
}
Sample JSON Event i.e. required for the lambda function to execute.
For inserting data:
{
"learner_id": "L123",
"learner_name": "John Doe",
"learner_income": 45000
}
Delete Function Code
import boto3
import json
# Initialize DynamoDB resource
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('learners')
key_to_delete ={
learner_id:event["learner_id"]
}
try:
response= table.delete_item(Key=key_to_delete)
return {
'statusCode':200,
'body': json.dumps("items deleted successfully")
}
except Exception as e:
return {
'statusCode':500,
'body': json.dumps(str(e))
}
Sample JSON Event
{
"learner_id": "L123"
}
For deleting data:
Conclusion
Whether you’re building a robust relational database with Amazon RDS or leveraging the power of
serverless with DynamoDB, AWS provides the tools you need for success.
Start exploring today, and let the cloud take your applications to new heights! 🚀
💡 What’s your go-to AWS database? Let’s discuss in the comments!