Getting Started with AWS and Boto3

A beginners guide to working with Amazon’s DynamoDB using Boto3

Ahmed Hersi
3 min readJan 19, 2023

Boto3 is the Amazon Web Services SDK for Python and it can be used to create, configure and manage AWS services. In this guide, we will demonstrate how to use Boto3 to:

  • Create a DynamoDB table
  • Insert items into the table
  • Query the table
  • Delete the table

You’ll need to have an AWS account and Python3 installed to follow along. We will be using Amazon’s Cloud9, an entirely Cloud-based IDE that can be used through a browser.

Installing boto3

Enter the following command into your terminal to install boto3.

pip install boto3

Creating a DynamoDB table

The following script creates a DynamoDB table that we will use to list the NBA’s All-Time leading scorers.

import boto3
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="your_access_key_ID",
aws_secret_access_key="your_secret_access_key")
table = dynamodb.create_table (
TableName = 'NBA-All-Time-Leading-Scorer',
KeySchema = [
{
'AttributeName': 'Name',
'KeyType': 'HASH'
},
{
'AttributeName': 'Points',
'KeyType': 'RANGE'
}
],
AttributeDefinitions = [
{
'AttributeName': 'Name',
'AttributeType': 'S'
},
{
'AttributeName':'Points',
'AttributeType': 'N'
}
],
ProvisionedThroughput={
'ReadCapacityUnits':1,
'WriteCapacityUnits':1
}

)
print(table)

First, we import the boto3 library and create the DynamoDB resource, using aws_access_key_id and aws_secret_access_key to authenticate the connection to the service.

The table is named ‘NBA-All-Time-Leading-Scorers’ and we use the KeySchema to define the primary key with ‘Name’ as the HASH (partition) key and ‘Points’ as the RANGE (sort) key. We then set the AttributeDefinitions for the table, ‘Name’ as an S (string) and ‘Points’ as N (number).

Finally, we set the read and write capacity units.

Newly created table

Inserting items

The put_item() method can be used to write a single item into a DynamoDB table. The following snippet inserts Kareem Abdul-Jabbar and his total points into the table.

import boto3
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="your_access_key_ID",
aws_secret_access_key="your_secret_access_key")
table = dynamodb.Table('NBA-All-Time-Leading-Scorers')
response = table.put_item(
Item = {
'Name': 'Kareem Abdul-Jabbar',
'Points': 38387
}
)

The batch_writer() method can be used to insert multiple items.

import boto3
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="your_access_key_ID",
aws_secret_access_key="your_secret_access_key")
table = dynamodb.Table('NBA-All-Time-Leading-Scorers')
with table.batch_writer() as batch:
batch.put_item(Item={"Name":"LeBron James", "Points":38024}),
batch.put_item(Item={"Name":"Karl Malone", "Points":36928}),
batch.put_item(Item={"Name":"Kobe Bryant", "Points":33643}),
batch.put_item(Item={"Name":"Michael 'GOAT' Jordan", "Points":32292}),
batch.put_item(Item={"Name":"Dirk Nowitzki", "Points":31560}),
batch.put_item(Item={"Name":"Wilt Chamberlain", "Points":31419}),
batch.put_item(Item={"Name":"Shaquille O'Neal", "Points":28596}),
batch.put_item(Item={"Name":"Carmelo Anthony", "Points":28289}),
batch.put_item(Item={"Name":"Moses Malone", "Points":27409})

Querying the table

You can use the query() method to query items in a DynamoDB, and ‘KeyConditionExpression’ can be used to specify a partition key value.

You will need to import the ‘Key’ class to build ‘KeyConditionExpression’.

import boto3
from boto3.dynamodb.conditions import Key
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="your_access_key_ID",
aws_secret_access_key="your_secret_access_key")
table = dynamodb.Table('NBA-All-Time-Leading-Scorers')
response = table.query(KeyConditionExpression=Key('Name').eq("Michael 'GOAT' Jordan"))
print("The query returned the following items:")
for item in response['Items']:
print(item)
Result of query

Deleting items and tables

You can delete an item by using the delete_item() method and specifying an item’s primary key.

import boto3
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="your_access_key_ID",
aws_secret_access_key="your_secret_access_key")
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('NBA-All-Time-Leading-Scorers')
response = table.delete_item(Key = {"Name":"LeBron James", "Points":38024})
print(response)

To delete an entire table, use the delete_table() method and the name of the table.

import boto3
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="your_access_key_ID",
aws_secret_access_key="your_secret_access_key")
client = boto3.client('dynamodb')
resp = client.delete_table(
TableName="NBA-All-Time-Leading-Scorers",
)
print("Table deleted successfully!")

--

--