How To: Insert In DynamoDB Using Lambda AWS Function (3 Min)

Описание к видео How To: Insert In DynamoDB Using Lambda AWS Function (3 Min)

In this tutorial, you'll learn how to insert into DynamoDB using Lambda AWS function.


Facebook:   / gokcedbsql  

Video Transcript:

Hi guys, this is Abhi fromGokcedb. In this video, you are going to learn how to insert data into the Dynamodb using a Lambda function. Let’s start by searching for Dynamodb on the AWS Console then hit Create Table. 

Enter the name of the table partition key and sort key and for Table Setting, I’m going to leave everything to default and then click on create a table. Once the table is created, click on explore items in the left menu.

Click on the Create Item button and give a value for the property underscore state and property id. 
Looks like our insert went through as expected. Now let’s go back to AWS Console and search for IAM which stands for Identity and Access Management.

Click on roles in the left menu then hit the Create Role button. Choose Lambda and hit next. Search for Dynamodb and Select Dyamodb full access then click next.

Give this role a name and then the Create Role button. This will ensure that our Lambda function will have full access to the Dynamodb. Go back to AWS Console and this time search for Lambda.

Click on create function and give your function a name. For runtime, I’m going to select Python and for Default Execution Role, we're going to select the full Dynamodb Role that we just created. In the Code Source Section, I’m going to copy-paste pre-written Python Code. On line 2, I’m importing the borto 3 libraries which is the SDK for AWS. 

On line 4, I’m creating the Dynamodb Client. And on line 5, I'm calling the property table. Inside the lambda underscore handler function, I’m using the put underscore item method to insert the event in the property table. 

On line 10, I’m returning the table. The scan will return all the items from the property table. Next, click on the test button and give your Test Event a name. 

For event Json key-value pairs, I’m going to specify the values for property ID, State, and County. 
Hit save then click on deploy. Click on deploy. Click on test again and you should see a response pack with a table dot scan value.

You can also go back to the Dynamo DB interface and hit the refresh button to see whether our insert went through or not.  Next, let's configure the test event so we can change our insert values. This time, I’m going to specify California as my State and San Diego as my County.

Click on the test button to test your Lambda function one more time and go back to the Dynamodbinterface to confirm our second insert went through. 
There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.

import json
import boto3

dynamodb_client = boto3.resource('dynamodb')
table = dynamodb_client.Table('property')

def lambda_handler(event, context):
try:
response=table.put_item(Item=event)
return table.scan()
except:
raise


#event
{
"property_id":1001,
"property_state":"New York",
"property_county":"Clinton"
}

Комментарии

Информация по комментариям в разработке