Skip to content

Instantly share code, notes, and snippets.

@svmotha
Created June 11, 2017 05:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save svmotha/653ee7316b3e7259c329d15417cd5de1 to your computer and use it in GitHub Desktop.
Save svmotha/653ee7316b3e7259c329d15417cd5de1 to your computer and use it in GitHub Desktop.
Check if DynamoDB table already exists and create one if it doesn't
import boto3
class tableCreate(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
# Query client and list_tables to see if table exists or not
def queryCreate(self):
# Instantiate your dynamo client object
client = boto3.client('dynamodb')
# Get an array of table names associated with the current account and endpoint.
response = client.list_tables()
if 'followers' in response['TableNames']:
table_found = True
else:
table_found = False
# Get the service resource.
dynamodb = boto3.resource('dynamodb')
# Create the DynamoDB table called followers
table = dynamodb.create_table(
TableName ='followers',
KeySchema =
[
{
'AttributeName': 'follower_id',
'KeyType': 'HASH'
}
],
AttributeDefinitions =
[
{
'AttributeName': 'follower_id',
'AttributeType': 'N'
}
],
ProvisionedThroughput =
{
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
# Wait until the table exists.
table.meta.client.get_waiter('table_exists').wait(TableName='followers')
return table_found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment