How to check if DynamoDB table exists?

PythonAmazon DynamodbBoto3

Python Problem Overview


I'm a new user in boto3 and i'm using DynamoDB.

I went through over the DynamoDB api and I couldn't find any method which tell me if a table is already exists.

What is the best approach dealing this issue?

Should I try to create a new table and wrap it using try catch ?

Python Solutions


Solution 1 - Python

From reading the documentation, I can see that there are three methods by which you can check if a table exists.

  1. The CreateTable API throws an error ResourceInUseException if the table already exists. Wrap the create_table method with try except to catch this
  2. You can use the ListTables API to get the list of table names associated with the current account and endpoint. Check if the table name is present in the list of table names you get in the response.
  3. The DescribeTable API will throw an error ResourceNotFoundException if the table name you request doesn't exist.

To me, the first option sounds better if you just want to create a table.

Edit: I see that some people are finding it difficult to catch the exceptions. I will put some code below for you to know how to handle exceptions in boto3.

Example 1

import boto3

dynamodb_client = boto3.client('dynamodb')

try:
    response = dynamodb_client.create_table(
        AttributeDefinitions=[
            {
                'AttributeName': 'Artist',
                'AttributeType': 'S',
            },
            {
                'AttributeName': 'SongTitle',
                'AttributeType': 'S',
            },
        ],
        KeySchema=[
            {
                'AttributeName': 'Artist',
                'KeyType': 'HASH',
            },
            {
                'AttributeName': 'SongTitle',
                'KeyType': 'RANGE',
            },
        ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 5,
            'WriteCapacityUnits': 5,
        },
        TableName='test',
    )
except dynamodb_client.exceptions.ResourceInUseException:
    # do something here as you require
    pass

Example 2

import boto3

dynamodb_client = boto3.client('dynamodb')


table_name = 'test'
existing_tables = dynamodb_client.list_tables()['TableNames']

if table_name not in existing_tables:
    response = dynamodb_client.create_table(
        AttributeDefinitions=[
            {
                'AttributeName': 'Artist',
                'AttributeType': 'S',
            },
            {
                'AttributeName': 'SongTitle',
                'AttributeType': 'S',
            },
        ],
        KeySchema=[
            {
                'AttributeName': 'Artist',
                'KeyType': 'HASH',
            },
            {
                'AttributeName': 'SongTitle',
                'KeyType': 'RANGE',
            },
        ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 5,
            'WriteCapacityUnits': 5,
        },
        TableName=table_name,
    )

Example 3

import boto3

dynamodb_client = boto3.client('dynamodb')

try:
    response = dynamodb_client.describe_table(TableName='test')
except dynamodb_client.exceptions.ResourceNotFoundException:
    # do something here as you require
    pass

Solution 2 - Python

import boto3

from botocore.exceptions import ClientError

TABLE_NAME = "myTableName"
dynamodb = boto3.resource('dynamodb', endpoint_url="https://dynamodb.us-east-1.amazonaws.com")

table = dynamodb.Table(TABLE_NAME)

try:
    response = client.describe_table(TableName=TABLE_NAME)

except ClientError as ce:
if ce.response['Error']['Code'] == 'ResourceNotFoundException':
    print "Table " + TABLE_NAME + " does not exist. Create the table first and try again."
else:
    print "Unknown exception occurred while querying for the " + TABLE_NAME + " table. Printing full error:"
    pprint.pprint(ce.response)

Solution 3 - Python

Alternate approach if you do not want to use boto3.client but only boto3.resource:

import boto3

database = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")    

table_name  = 'MyTable'
table_names = [table.name for table in database.tables.all()]

if table_name in table_names:
    print('table', table_name, 'exists')

Solution 4 - Python

You can use describe table API to determine whether the table exists.

Sample code:

from __future__ import print_function # Python 2/3 compatibility
import os
os.environ["TZ"] = "UTC"
import boto3

client = boto3.client('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")



response = client.describe_table(
    TableName='Movies'
)    

print(response)

If table exists:-

  • You will get the response

If table doesn't exists:-

  • You will get ResourceNotFoundException

    botocore.errorfactory.ResourceNotFoundException: An error occurred (ResourceNotF oundException) when calling the DescribeTable operation: Cannot do operations on a non-existent table

Another way:-

> Waits until this Table is exists. This method calls > DynamoDB.Waiter.table_exists.wait() which polls. > DynamoDB.Client.describe_table() every 20 seconds until a successful > state is reached. An error is returned after 25 failed checks.

table.wait_until_exists()

Solution 5 - Python

You can use .table_status attr of any boto3 Table instance object. It returns it's status if exists (CREATING, UPDATING, DELETING, ACTIVE) or throws exception botocore.exceptions.ClientError: Requested resource not found: Table: <YOUR_TABLE_NAME> not found. You can wrap those conditions into try / except to have full info on the current table state.

import boto3
from botocore.exceptions import ClientError

dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
table = dynamodb.Table('your_table_name_str')

try:
  is_table_existing = table.table_status in ("CREATING", "UPDATING",
                                             "DELETING", "ACTIVE")
except ClientError:
  is_table_existing = False
  print "Table %s doesn't exist." % table.name

Solution 6 - Python

Note that it kind of depends on if you are using client or resource. If you use boto3.client(), you can use the 3 methods the accepted answer suggested. If you are using boto3.resource(), you can only use dynamodb_resource.create_table() and check exceptions.

try:
    table = dynamodb_resource.create_table(
        ...
    )
    table.meta.client.get_waiter('table_exists').wait(TableName=your_table_name)
except ResourceInUseException:
    # do sth here

Solution 7 - Python

I know this will have a slight risk if there are more than 10 tables between table.split(0, -1) and table. However, it does save throwing exceptions and the like. Alas the documentation https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_ListTables.html (examples) imply the the first table in the returned list will be the search item, it is not the case..

class Dynamo {

private m_db!: DynamoDB;

private async ensure_table(name: string) {
    const search = await this.db().listTables({
        ExclusiveStartTableName: name.slice(0, -1),
        Limit: 10 });
    const exists = search.TableNames?.includes(name);
    exists || await this.create_table(name);
}

private async create_table(name: string) {
   // create the table here 
}

 
private db(): DynamoDB { 
    return this.m_db || (this.m_db = this.create_db()); 
}


private create_db = (): DynamoDB => { 
    return new DynamoDB({apiVersion: "2012-08-10"}); }
}

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionroeygolView Question on Stackoverflow
Solution 1 - PythonanupsabrahamView Answer on Stackoverflow
Solution 2 - PythonfIwJlxSzApHEZIlView Answer on Stackoverflow
Solution 3 - PythonsscView Answer on Stackoverflow
Solution 4 - PythonnotionquestView Answer on Stackoverflow
Solution 5 - PythonjuggernautView Answer on Stackoverflow
Solution 6 - PythonZ.WeiView Answer on Stackoverflow
Solution 7 - PythonJohnView Answer on Stackoverflow