DynamoDB, Demistyfied (Chapter 2)
Perform CRUD operations on dynamoDB with python

I love creating and writing about the creation of software.
Search for a command to run...
Perform CRUD operations on dynamoDB with python

I love creating and writing about the creation of software.
A Technical / Non Technical overview of the dynamoDb for Beginners
Queries and Projection Expressions
Watch the complete Video on Youtube https://www.youtube.com/watch?v=kKX-8L_R2XM Prerequisites Before proceeding make sure you have these dependencies Docker Python 3.11 and above AWS CLI Full access to a foundation model. For this workshop, we'll...

Introduction In a previous workshop, Building event-driven applications with AWS sam and python we built an e-commerce ordering service using AWS SAM, API Gateway, Lambda and Python. Here's the solutions architecture. In that workshop, whenever a ne...

Github Repository https://github.com/EducloudHQ/introduction_to_gen_ai Skill up with serverless on Educloud https://www.educloud.academy/content/da99ad07-7efa-41e7-ba50-b18e6b89e10d This post is an introductory lesson to building AI-enhanced serverle...

Introduction Serverless computing is a cloud-based approach to application development that eliminates the need for provisioning and managing servers. Instead, developers focus on writing code that is triggered by events such as HTTP requests or data...

https://github.com/trey-rosius/sam_stepfunctions Hey!!! How you doing? In this post, we’ll look at how to build a workflow, using SAM as IaC, Appsync, and python. Prerequisites Install AWS Cli (https://docs.aws.amazon.com/cli/latest/userguide/cli-c...

In Chapter 1, we ran through the core concepts surrounding the dynamoDb and I provided a link on how to install it locally on your computer. In this chapter, we'll be looking at how to perform basic CRUD(Create, Read, Update, Delete) operations on a table. I'll assume you've already installed and are running dynamo DB locally.
What we want to do is, create a Products table and perform CRUD operations on it.
Let's Begin.
id – The partition key. The AttributeType is N for number.
name – The sort key. The AttributeType is S for string.
Create a file called CreateProductsTable.py and type out this code within
import boto3
def create_products_table(dynamodb=None):
if not dynamodb:
dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")
table = dynamodb.create_table(
TableName='Products',
KeySchema=[
{
'AttributeName': 'id',
'KeyType': 'HASH' # Partition key
},
{
'AttributeName': 'name',
'KeyType': 'RANGE' # Sort key
}
],
AttributeDefinitions=[
{
'AttributeName': 'id',
'AttributeType': 'N'
},
{
'AttributeName': 'name',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
return table
if __name__ == '__main__':
product_table = create_products_table()
print("Table status:", product_table.table_status)
boto3 is the aws sdk for python.
You set the endpoint to indicate that you are creating the table in the downloadable version of DynamoDB on your computer.
The ProvisionedThroughput parameter is required. However, the downloadable version of DynamoDB ignores it. Provisioned throughput is the maximum amount of capacity that an application can consume from a table or index. If your application exceeds your provisioned throughput capacity on a table or index, it is subject to request throttling. Throttling prevents your application from consuming too many capacity units.
Make sure you have boto3 installed on your computer
pip install boto3
To run the program, enter the following command.
python CreateProductsTable.py
This should be your output in the terminal
Table status: ACTIVE
from pprint import pprint
import boto3
from decimal import Decimal
def put_product(id, name,description, price,rating, dynamodb=None):
if not dynamodb:
dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")
table = dynamodb.Table('Products')
response = table.put_item(
Item={
'id': id,
'name': name,
'description':description,
'price': Decimal(price),
'rating':Decimal(rating)
}
)
return response
if __name__ == '__main__':
product_response = put_product(1001,"Laptops",
"Any good description", "25.9", "4.5")
print("Put product succeeded:")
pprint(product_response, sort_dicts=False)
The DecimalEncoder class is used to print out numbers stored using the Decimal class. The Boto SDK uses the Decimal class to hold Amazon DynamoDB number values.
To run the program, enter the following command.
python CreateProductItem.py
This should be your output in the terminal.
Put product succeeded:
{'ResponseMetadata': {'RequestId': '83bb5a1f-07f9-48b6-88fa-813040d5c371',
'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Mon, 29 Mar 2021 14:54:47 GMT',
'content-type': 'application/x-amz-json-1.0',
'x-amz-crc32': '2745614147',
'x-amzn-requestid': '83bb5a1f-07f9-48b6-88fa-813040d5c371',
'content-length': '2',
'server': 'Jetty(9.4.18.v20190429)'},
'RetryAttempts': 0}}
from pprint import pprint
import boto3
from botocore.exceptions import ClientError
def get_product(id, name, dynamodb=None):
if not dynamodb:
dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")
table = dynamodb.Table('Products')
try:
response = table.get_item(Key={'id': id, 'name': name})
except ClientError as e:
print(e.response['Error']['Message'])
else:
return response['Item']
if __name__ == '__main__':
product = get_product(1001,"Laptops")
if product:
print("Successfully got the product:")
pprint(product, sort_dicts=False)
To run the program, enter the following command.
python ReadProductItem.py
This should be your output in the terminal.
Successfully got the product:
{'name': 'Laptops',
'rating': Decimal('4.5'),
'description': 'Any good description',
'id': Decimal('1001'),
'price': Decimal('25.9')}
Create a file called UpdateProductItem.py and type out the following code in it
from decimal import Decimal
from pprint import pprint
import boto3
def update_product(id,name,description, rating, dynamodb=None):
if not dynamodb:
dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")
table = dynamodb.Table('Products')
response = table.update_item(
Key={
'id': id,
'name': name
},
UpdateExpression="set rating=:r, description=:d",
ExpressionAttributeValues={
':r': Decimal(rating),
':d': description
},
ReturnValues="UPDATED_NEW"
)
return response
if __name__ == '__main__':
update_response = update_product(
1001,"Laptops","an updated description", 6.5)
print("Product Successfully updated:")
pprint(update_response, sort_dicts=False)
This program uses UpdateExpression to describe all updates you want to perform on the specified item.
The ExpressionAttributeValues provides value substitution.As you can see, we substituted the :r for ratings and :d for description.
The ReturnValues parameter instructs DynamoDB to return only the updated attributes (UPDATED_NEW).
To run the program, enter the following command.
python UpdateProductItem.py
This should be your output.
Product Successfully updated:
{'Attributes': {'rating': Decimal('6.5'),
'description': 'an updated description'},
'ResponseMetadata': {'RequestId': 'e1e0c9af-32e2-4052-9bc8-abd7e580fe21',
'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Mon, 29 Mar 2021 15:25:21 GMT',
'content-type': 'application/x-amz-json-1.0',
'x-amz-crc32': '1215728936',
'x-amzn-requestid': 'e1e0c9af-32e2-4052-9bc8-abd7e580fe21',
'content-length': '82',
'server': 'Jetty(9.4.18.v20190429)'},
'RetryAttempts': 0}}
from decimal import Decimal
from pprint import pprint
import boto3
from botocore.exceptions import ClientError
def delete_product(id,name, dynamodb=None):
if not dynamodb:
dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")
table = dynamodb.Table('Products')
try:
response = table.delete_item(
Key={
'id': id,
'name': name
}
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
return response
if __name__ == '__main__':
delete_response = delete_product(1001,"Laptops")
if delete_response:
print("Successfully deleted product")
pprint(delete_response, sort_dicts=False)
To run the program, enter the following command.
python DeleteProductItem.py
Your output should look like this.
Successfully deleted product
{'ResponseMetadata': {'RequestId': 'c892d40b-e044-4953-b8db-5c0ddf6eed4a',
'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Mon, 29 Mar 2021 15:38:57 GMT',
'content-type': 'application/x-amz-json-1.0',
'x-amz-crc32': '2745614147',
'x-amzn-requestid': 'c892d40b-e044-4953-b8db-5c0ddf6eed4a',
'content-length': '2',
'server': 'Jetty(9.4.18.v20190429)'},
'RetryAttempts': 0}}