Search code examples
node.jsamazon-dynamodbaws-sdk-js

How to use waitUntilTableNotExists in DynamoDb describe table


I am trying to delete and then create a DynamoDB table using nodejs aws sdk (version 3.142.0) and I wanted to use the waiters (waitUntilTableNotExists / waitUntilTableExists), but I don't understand how they are supposed to be used and I cannot find a good example online.

Regards


Solution

  • Here is one way after a createTable command in aws-sdk-js-v3 to wait for the table to complete. A note is that if you do NOT use waitUntilTableExists and instead attempt to use DescribeTableCommand it will incorrectly report TableStatus == 'ACTIVE' even though you cannot Read/Write to the table, you must use waitUntilTableExists.

    import {
      CreateTableCommandInput,
      CreateTableCommandOutput,  
      waitUntilTableExists
    } from "@aws-sdk/client-dynamodb";
    
    const client = new DynamoDBClient({ region: "us-east-1" });
    
    
    const data = await client.send(
          new CreateTableCommand({
            TableName: tableName,
            AttributeDefinitions: partitionAndSortKeyDefinitions,
            KeySchema: columnSchema,
            ProvisionedThroughput: {
              ReadCapacityUnits: 4,
              WriteCapacityUnits: 2,
            },
          })
    );
    
    
    // maxWaitTime - seconds
    const results = await waitUntilTableExists({client: client, maxWaitTime: 120}, {TableName: tableName})
    if (results.state == 'SUCCESS') {
      return results.reason.Table
    }
    console.error(`${results.state} ${results.reason}`);