AWS DynamoDB with Python (Boto3)— Part 2 — Create Table & Insert Item
If you haven’t read the previous article in this series — AWS DynamoDB with Python (Boto3) — Part 1 — Intro to DynamoDB & local Installation
Boto3 — The AWS SDK for Python
Boto3 is the official SDK developed in Python which can help us to create, configure & manage AWS services
To see list of services Boto3 offers, please refer the official Boto3 documentation here
Boto3 to Access DynamoDB
First we need to add boto3 dependency in our project.
Create a file called requirements.txt under the root directory of our project. requirements.txt will contain list of dependencies we require to run our project.
For now, we need only boto3. So add the word boto3 as first line
Run the following command to install the dependencies
pip install -r requirements.txt -t dependencies/python/lib/python3.8/site-packages
To access DynamoDB using boto3,
import boto3dynamodb = boto3.resource('dynamodb')
Remember, boto3 by default will try to access the DynamoDB associated with your AWS account.
If you want to access DynamoDB installed in your local machine for development purpose
import boto3dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="us-west-2",
endpoint_url="http://localhost:8000")
Note: Its mandatory to specify at least dummy values for access, secret key, region_name to access DynamoDB in local (Don’t know why such restriction :P)
Create Table in DynamoDB
While creating DynamoDB table, we must configure Primary Keys. Here is the code to create DynamoDB table using boto3
import boto3
def create_table():
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")
table_creation_resp = dynamodb.create_table(
TableName='Student',
KeySchema=[
{
'AttributeName': 'dept',
'KeyType': 'HASH' # Partition Key
},
{
'AttributeName': 'stud_id',
'KeyType': 'RANGE' # Sort Key
}
],
AttributeDefinitions=[
{
'AttributeName': 'dept',
'AttributeType': 'S' # string data type
},
{
'AttributeName': 'stud_id',
'AttributeType': 'S' # string data type
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
print(table_creation_resp)
In above code, we created Student table with both partition & sort keys (dept, stud_id) as String type
Insert Item in DynamoDB
To insert an item, we need to use put_item() function
import boto3class Student:
dept: str
stud_id: str
stud_name: str
stud_age: int
subjects: list
def __init__(self, dept, stud_id, stud_name, stud_age, subjects):
self.dept = dept
self.stud_id = stud_id
self.stud_name = stud_name
self.stud_age = stud_age
self.subjects = subjectsdef insert_student_data(stud: Student):
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")
table = dynamodb.Table('Student')
insert_item_resp = table.put_item(
Item={
'dept': stud.dept,
'stud_id': stud.stud_id,
'stud_name': stud.stud_name,
'stud_age': stud.stud_age,
'subjects': stud.subjects
}
)
print(insert_item_resp)
Remember, we can insert only one item a time using put_item function
Thats all about create table & insert item in DynamoDB !
Next article in this series — AWS DynamoDB with Python (Boto3) — Part 3— Query Items from DynamoDB
GitHub Repo URL — https://github.com/iamvickyav/dynamodb-with-python-boto3