I’ll use Python with the Boto3 library to interact with DynamoDB, and I’ll create a basic script that can perform CRUD (Create, Read, Update, Delete) operations.
Prerequisites AWS Account: AWS account to use DynamoDB. AWS CLI: Install and configure the AWS CLI with credentials. Python: Install Python (preferably 3.x). Boto3: Install the Boto3 library using pip (pip install boto3). Git: Install Git and create a GitHub account if you don’t have one.
Steps to Build the Application
- Set Up Environment Install Required Libraries:
pip install boto3 Configure AWS CLI (if not done already):
aws configure
- Create a Python Script Create a directory for your project and a file named dynamodb_app.py inside it.
mkdir dynamodb_project cd dynamodb_project touch dynamodb_app.py
- Write the Python Script Open dynamodb_app.py and add the following code:
python Copy code import boto3
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
def create_table(): table = dynamodb.create_table( TableName='Users', KeySchema=[ { 'AttributeName': 'username', 'KeyType': 'HASH' } ], AttributeDefinitions=[ { 'AttributeName': 'username', 'AttributeType': 'S' } ], ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 } ) print("Table status:", table.table_status)
def add_user(username, first_name, last_name): table = dynamodb.Table('Users') table.put_item( Item={ 'username': username, 'first_name': first_name, 'last_name': last_name } ) print(f"Added user: {username}")
def get_user(username): table = dynamodb.Table('Users') response = table.get_item( Key={ 'username': username } ) item = response.get('Item') print(item)
def update_user(username, first_name, last_name): table = dynamodb.Table('Users') table.update_item( Key={ 'username': username }, UpdateExpression='SET first_name = :f, last_name = :l', ExpressionAttributeValues={ ':f': first_name, ':l': last_name } ) print(f"Updated user: {username}")
def delete_user(username): table = dynamodb.Table('Users') table.delete_item( Key={ 'username': username } ) print(f"Deleted user: {username}")
if name == "main": create_table() add_user('jdoe', 'John', 'Doe') get_user('jdoe') update_user('jdoe', 'Johnny', 'Doe') get_user('jdoe') delete_user('jdoe') 4. Run the Script
python dynamodb_app.py This script will create a DynamoDB table called Users, add a user, retrieve the user’s details, update the user, and finally delete the user.
- Push the Project to GitHub Initialize Git Repository:
git init Add Files to the Repository:
git add . Commit the Changes: