Table Creation

When you create a DynamoDB table, it can take quite a while (especially if you add a few secondary index’s). Instead of polling describe_table yourself, boto3 came up with “waiters” that will do all the polling for you. The following snippet shows how to wait for a DynamoDB table to be created in an async way.

# Boto should get credentials from ~/.aws/credentials or the environment
import asyncio
import uuid

from aiobotocore.session import get_session


async def go():
    session = get_session()
    async with session.create_client(
        'dynamodb', region_name='us-west-2'
    ) as client:
        # Create random table name
        table_name = f'aiobotocore-{uuid.uuid4()}'

        print('Requesting table creation...')
        await client.create_table(
            TableName=table_name,
            AttributeDefinitions=[
                {'AttributeName': 'testKey', 'AttributeType': 'S'},
            ],
            KeySchema=[
                {'AttributeName': 'testKey', 'KeyType': 'HASH'},
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 10,
                'WriteCapacityUnits': 10,
            },
        )

        print("Waiting for table to be created...")
        waiter = client.get_waiter('table_exists')
        await waiter.wait(TableName=table_name)
        print(f"Table {table_name} created")


if __name__ == '__main__':
    asyncio.run(go())