DynamoDB & Python Lambda

DynamoDB & Python Lambda

DynamoDB - a non relational database developed by Amazon which supports key-value data structure. Areas where we do not need to store relational data, DynamoDB can be used there as a NoSQL database. It is faster than any relation database like MySQL, Oracle and SQL Server.

The Database and Table

Inside aws account we can have a pre-created database for each region. As per our need we can create tables. Each table consists of a partition key (we can say primary key - mandatory key) of one attribute or two attributes maximum. For example, let’s create a table called “studentTable” with a partition key “studentID”. DynamoDB supports partition key type String, Binary and Number. This case we will choose Number as partition key type.

No alt text provided for this image

We can manually insert some records from UI section, but need to make sure each time we provide unique “studentID”. Lets create a Python Lambda function to insert few records into studentTable.

import boto3
import time

def studentAdd(event, context):
    try:
        dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

        table = dynamodb.Table('studentTable')

        response = table.put_item(
            Item={
                'studentID': int(round(time.time())),
                'studentName':event['studentName'],
                'studentAddress': event['studentAddress']
            }
        )
        return {
            'status': True,
            'message': 'A new student has been inserted.'
        }
    except Exception as e:
        return {
            'status': False,
            'message': e
        }

boto3 is an aws software development kit used in python to connect S3, DynamoDB, EC2 amazon services. In the above code we used boto3 to create a dynamodb connection object, after that we created another object “table” by “dynamodb” object, and this is all to establish a connection from your python lambda code to dynamodb. The “region_name” defines which region’s dynamodb we want to connect.

*Please note we need to assign proper role to this lambda function so that it can have permission to connect dynamodb.

In order to execute the code we need to provide below json formatted data:

{
	'studentName': 'Jhon Deo',
	'studentAddress': 'xyz street address'
}

int(round(time.time())) generates current timestamp which we use as a partition key, rest of information studentName, studentAddress we receive from the json data.

The “put_item” method is used to perform a single insertion into DynamoDB. The entire code operation is covered within a try-except block for exception handling.

More Table Operations

As we can add an item to the “student” table, we may need to update, read, and delete items as per need. This link shows how a python lambda code can update, read, delete items from DynamoDB.

Inside table operations DynamoDB supports “query” and “scan” operations. The scan operation is costly as it retrieve all the items from a table, where as query is cost effective operation because it only retrieve item which match the partition key criteria.

DynamoDB and Indexing

Now question comes do we need to make a scan operation if we need a search on Non Key attribute? 

A non key means an attribute which does not belongs to partition key. Good part of DynamoDB is it supports indexing on any non key attribute. (We can make 5 maximum keys to index.)

Let's make the “studentName” attribute as an index to this table. In order to do that go to “Indexes” column of the “studentTable” and click on “Create index” button. Name attribute name which you want to make as index, then the index column will be populated automatically as {{attribute-name}}-index.

No alt text provided for this image

Index query with Python

Let’s define a python function which take studentName as input, and populate all items having that value.

import boto3
from boto3.dynamodb.conditions import Key, Attr

def getStudentByStudentName(event, context):
	try:
		dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
		
		table = dynamodb.Table('studentTable')


		studentName = event['studentName']


		response = table.query(
			IndexName='studentName-index',
			KeyConditionExpression=Key('studentName').eq(studentName)
			)
		return {
			'status': True,
			'students': response['Items']
		}
	except Exception as e:
        return {
            'status': False,
            'error': e
        }

To execute the code we need to provide below json formatted data:

{
	'studentName': 'Jhon Deo'
}

In this article I wanted to explore some basic ideas how to connect dynamodb from a python lambda code, and it various operations. Anyone having more interest to know about Python Lambda or DynamoDB, feel free to inbox me.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了