Created
June 11, 2017 05:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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