Day 4 - AWS RDS with EC2 and DynamoDB with Lambda


let’s deep dive in AWS RDS services which used for storage the data in simple way. RDS relational database services. We are creating our database here so let’s start. Go to the console type RDS then you can see the dashboard.
Here we are creating our database in ohio region so keep that in mind.In DB instances you can make 40 instances only they have limits if you want more then you can pay a price. DB cluster is one group form and inside that there is multiple small DB (Database) working together. Mostly used for production level. Snapshots means Backup of data ( in case if data crash from this we can recover)
Click on create a database.
Select here Standard create and in engine options MySQL because its open source database and cost is low. Mysql means structured query language. All data stored in structured way with sql commands.
Here we are learning so choose Free tier options and in Availability and Durability its selected by default.
By default here the name is database-1 and in creditials setting name also by default Admin its managed by self so here is Self managed set our Master password we have to set.
Here are some configuration they are by default and only we have to choose allocated 20 Gb only because its free.
All setting are by default so don’t do anything and create our database. its take 15 to 20 mins.
after 20 mins the database is created
Now we are going to EC2 instance and to connect our database which we have
Here we are trying to connect our local databse but there is no database in local
In RDS-go to database-1 - connectivity and security - and copy the endpoint url
Now we are connected to our database and we can create our mysql database with come sql commands
Or you can give a policy to EC2 to access the RDS so, we are doing here now.
RDS have cloudwatch also for monitaring so we have to give that also.Then create role.
now we are giving here IAM role which can access rds for that go to instances-actions-security-modify IAM role
Go back to rds-database-1 - scroll down- there is option connect compute resources.
Set up the EC2 connection here you can see then select the EC2 instances and continue.
Here you can see EC2 and RDS can access each other and they are in VPC means in virtual private cloud with high security only with some permission you can access this.
Here successfully setup is done
Now we are accessing our database which is mysql through the commands.
Here we are creating database which name is aws. We create the one table also where is learner_id and learner_name you can see.
Also you can delete the data also with sql commands.
Here are the simple use case from EC2 to can create data and delete data in mysql.
Now, lets deep dive in dynamoDB and lambda
Create Table give here name of table.We give a name like before learner and crete learner_id id is in number and sort key is optional but if you want you can sort with number, email id etc.
By default setting we have to give and then click on create table.
Here within a minute our table is created.
*Now we are learning Lambda which is serverless service in AWS. Lambda also used for cost optimization so we can reduce the cost of service. Lambda is working on function you can write different function for different service. So its work on serverless.
as you know lambda is working on function so we have to create function. Lets start go to create function.
Give a function name like I have given insert-into-dynamo means insert data in dynamodb. Runtime means where it is run so its run on Python programming.Now click on create function.
Here our console is coming after successfully creating function. Here our Function insert-into-dynamo.
For just test we are writing print statement. Let’s check it is working or not . one more after the writing statement deploy it then test it.
After click on deploy lets test but here we have to create event. what is event - we create a function but for running the function we need some button in general, so its called event in simple way. So now we have to create event to run our funtion. So here we gave event name insert-event and in event JSON whatever you put here its go to lambda then executed.
You can see here whatever we put in event JSON its executed. We are inserted event data in
So now we are inserting our data in Dynamodb but for that we have to establish the connection between dynamodb and python. For that we need python libarary that name is boto 3. Boto3 can work with AWS services thats why we are using it.
lets deploy and test , You can see here we have error and in error they show no any policy is allow to access so now we have to create role and attached the policy.
We create here role name dynamo-access-lambda.
now go to The lambda , configuration - permission - edit
And save ,what is the purpose of this role is when you run your code that code have access for dynamodb
Now you can see here our role
Every time when you make changes in code first deploy always and then test. Here is the code I used.
import json
import boto3
def lambda_handler(event, context):
print(event["message"])
# establish connection to DynamoDB resource
dynamodb = boto3.resource("dynamodb")
# get the table learner
table = dynamodb.Table("learner")
# item to insert into table
items_to_insert = {
'learner_id' :event["learner_id"],
'learner_name' :event["learner_name"],
'learner_location': event["learner_location"]
}
# retrieve the item from the Dynamo table
try:
response = table.put_item(Item=items_to_insert)
return {
'statusCode': 200,
'body': json.dumps('Item inserted successfully')
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps(str(e))
}
lets try one more data to insert in table in row 3.
you can see here I am able to insert the data.
Now we are creating to insert data with events
Here is changes in code now deploy and test.
Our data inserted successfully you can see.
Now how to delete the data in dynamodb.Let’s try . Go to Lambda-create function-Author from scratch-give the function name-runtime same as insert data.
Here also we are giving permission so lets attached policy and create role as we made in insert function same role we have to give here also.
Go to code create new event as I made here.
here is my code using here.
import json
import boto3
def lambda_handler(event, context):
dynamodb = boto3.resource("dynamodb")
# get the table learner
table = dynamodb.Table("learner")
# item to insert into table
key_to_delete = {
'learner_id' :event["learner_id"],
}
# retrieve the item from the Dynamo table
try:
response = table.delete_item(Key=key_to_delete)
return {
'statusCode': 200,
'body': json.dumps('Item deleted successfully')
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps(str(e))
}
Like in insert data we create the event so here also we are creating event for delete-event.Now, You can see here I am deleted the item 4 .
So,here you can see item 4 deleted.
Here in delete event I mention 1.
So you can see here number 1 item deleted.Here our projrct is done to insert and delete data in dynamodb through lambda.
Thank you so much for reading my post.
Subscribe to my newsletter
Read articles from Aishwarya Upare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
