CRUD OPERATIONS FOR AWS DynamoDB USING PYTHON BOTO3 SCRIPT

CRUD OPERATIONS FOR AWS DynamoDB USING PYTHON BOTO3 SCRIPT

In this blog we are going to write scripts to perform CRUD operations for DynamoDB Tables.

Boto3 is the name of the Python SDK for AWS. It allows you to directly create, update, and delete AWS resources from your Python scripts.

Table Of Content

  • Create DynamoDB Table
  • Put Items In DynamoDB Table
  • Get/Batch_Get Items From DynamoDB Table
  • Update Items In DynamoDB Table
  • Delete Items & Table In DynamoDB Table

Create DynamoDB Table

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the resource for DyanamoDB.
    dynamodb = boto3.resource('dynamodb')
    
  3. We will use create_table() function to create table in Dynamo DB with following arguments listed below. Here we will see 2 examples one with "primary keys only" and another with "primary key and sort key". You can find official documentation here.
    Example1:- Below code is to create table with primary key only
    table = dynamodb.create_table(
     TableName='employee',
     KeySchema=[
         {
             'AttributeName': 'id',
             'KeyType': 'HASH'    #Partition Key Only
         }
     ],
     AttributeDefinitions=[
         {
             'AttributeName': 'id',
             'AttributeType': 'N'
         }
     ],
     ProvisionedThroughput={
         'ReadCapacityUnits': 1,
         'WriteCapacityUnits': 1
     },
    )
    
    Example2:- Below code is to create table with primary key and sort key
    table = dynamodb.create_table(
     TableName='employee',
     KeySchema=[
         {
             'AttributeName': 'id',
             'KeyType': 'HASH'    #Partition Key
         },
         {
             'AttributeName': 'name',
             'KeyType': 'RANGE'    #Sort Key
         }
     ],
     AttributeDefinitions=[
         {
             'AttributeName': 'id',
             'AttributeType': 'N'
         },
         {
             'AttributeName': 'name',
             'AttributeType': 'S'
         }
     ],
     ProvisionedThroughput={
         'ReadCapacityUnits': 1,
         'WriteCapacityUnits': 1
     },
    )
    
    You can find working code for example in Git Repo here

Put Items In DynamoDB Table

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the resource for DyanamoDB.
    dynamodb = boto3.resource('dynamodb')
    
  3. Here first we will use dynamodb.Table('employee') function which will return our employee table information which will be saved in table variable. Make sure to check official documentation here
    table = dynamodb.Table('employee')
    
  4. Now we will use put_item() to push items into the employee table. You can find official documentation here.

Example1 :- Put single Item and create new attribute salary.

dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('employee')
table.put_item(
    Item={
        'id': 1,
        'name': 'ABC',
        'salary': 20000
    },
)

Example2:- Put multiple Items. dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('employee')
table.put_item(
    Item={
        'id': 1,
        'name': 'ABC',
        'salary': 20000
    },
)
table.put_item(
    Item={
        'id': 2,
        'name': 'DEF',
        'salary': 22000
    },
)
table.put_item(
    Item={
        'id': 3,
        'name': 'XYZ',
        'salary': 25000
    },
)

Note:- If you have only partitioned key defined while creating table that partition key is complusory element in put_item() but if you have created table with partition key along with sort key then you have to make sure both these elements are defined in put_item() otherwise it will throw error.
You can find working code for example in Git Repo here

Get/Batch_Get Items From DynamoDB Table

Get_Item

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the resource for DyanamoDB.
    dynamodb = boto3.resource('dynamodb')
    
  3. Here first we will use dynamodb.Table('employee') function which will return our employee table information which will be saved in table variable. Make sure to check official documentation here
    table = dynamodb.Table('employee')
    
  4. Now we will use get_item() to get item list available in table and print the returned value "Item". You can find official documentation here
    resp = table.get_item(
     Key={
         'id': 1
     },
    )
    print(resp['Item'])
    
    You can find working code for example in Git Repo here.
    Note:- If you have only defined partitioned key while creating table then above code will work with partition key. But if you have defined partitioned key along with sort key while creating table then make sure to use that as displayed in below code.
resp = table.get_item(
    Key={
        'id': 1  #Partition Key
        'name': "XYZ" #Sort Key
    },
)
print(resp['Item'])

Batch_Get_Item

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the resource for DyanamoDB.
    dynamodb = boto3.resource('dynamodb')
    
  3. Now we will use batch_get_item() to fetch multiple items by defining the partition keys and print response. You can find official documentation here
    response = dynamodb.batch_get_item(
         RequestItems={
             'employee': {
                 'Keys': [
                     {
                         'id': 1
                     },
                     {
                         'id': 2
                     },
                 ],
                 'ConsistentRead': True
             }
         },
         ReturnConsumedCapacity='TOTAL'
     )
    print(response['Responses'])
    
    You can find working code for example in Git Repo here

Update Items In DynamoDB Table

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the resource for DynamoDB.
    dynamodb = boto3.resource('dynamodb')
    
  3. Here first we will use dynamodb.Table('employee') function which will return our employee table information which will be saved in table variable.Make sure to check official documentation here
    table = dynamodb.Table('employee')
    
  4. Now we will use update_item() and perform salary update for one of our existing item by searching that value using key. Here its very important for us to understand below parameters
    UpdateExpression:-An expression that defines one or more attributes to be updated, the action to be performed on them, and new values for them. ExpressionAttributeValues:-One or more substitution tokens for attribute names in an expression.
    ReturnValues:-Use ReturnValues if you want to get the item attributes as they appear before or after they are updated. For UpdateItem , the valid values are NONE | ALL_OLD | UPDATED_OLD | ALL_NEW | UPDATED_NEW.
    Make sure to check official documentation here
    resp = table.update_item(
     Key={'id': 1},
     UpdateExpression="SET salary= :s",
     ExpressionAttributeValues={':s': 20},
     ReturnValues="UPDATED_NEW"
    )
    print(resp['Attributes'])
    
    You can find working code for example in Git Repo here.

Delete Items & Table In DynamoDB Table

Delete Items

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the resource for DynamoDB.
    dynamodb = boto3.resource('dynamodb')
    
  3. Here first we will use dynamodb.Table('employee') function which will return our employee table information which will be saved in table variable.Make sure to check official documentation here
    table = dynamodb.Table('employee')
    
  4. Now we will use delete_item() where we will provide key in list of argument to specific the item that we want to delete. You can find official documentation here
    resp = table.delete_item(
     Key={
         'id': 3
     }
    )
    
    You can find working code for example in Git Repo here.

Delete Table

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the resource for DynamoDB.
    dynamodb = boto3.resource('dynamodb')
    
  3. Here first we will use dynamodb.Table('employee') function which will return our employee table information which will be saved in table variable. Make sure to check official documentation here
    table = dynamodb.Table('employee')
    
  4. Now we will use delete() function to delete table. The DeleteTable operation deletes a table and all of its items. After a DeleteTable request, the specified table is in the DELETING state until DynamoDB completes the deletion. Make sure to check official documentation here
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('employee')
    table.delete()
    
    You can find working code for example in Git Repo here

Conclusion

By using Boto3 provided inbuild methods for AWS resources many task can be automated by writing a python script. Current codes snippet can be used to automate DynamoDB task like Create, Update, Get, Batch_Get , Delete Items, Delete Table etc.

Stay tuned for my next blog.....

So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks.

Did you find this article valuable?

Support Dheeraj Choudhary by becoming a sponsor. Any amount is appreciated!