ocwirk
ocwirk

Reputation: 1089

DynamoDB table is getting created but can not insert new items

I am using the boto DynamoDBV2 interface for a script to create and populate a table in the DynamoDB. My code for it looks something like this -

my_table = Table.create(table_name, schema=[HashKey('key', data_type=STRING)], connection = self.connection)
my_table.put_item(data={
         'key': 'somekey',
         'value': value
      })

I have created the connection, and when I run it, the table is created properly and I can see it in the AWS console. But I am getting the error "Requested Resource not found" when trying to put values in the table.

I also tried reading table separately and then insert values like this -

Table.create(table_name, schema=[HashKey('key', data_type=STRING)], connection = self.connection)
my_table = Table(table_name, self.connection)
my_table.put_item(data={
         'key': 'somekey',
         'value': value
      })

but still getting the same error on the second line. What am I missing ?

Upvotes: 3

Views: 2688

Answers (2)

Zanon
Zanon

Reputation: 30800

The problem is that you need to wait a little bit after the table is created before inserting items. You can use the waitFor() event to find when the table finishes to be created.

Example with AWS Node SDK:

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB();

const createTableParams = {
  // parameters...
}

dynamodb.createTable(createTableParams, (err, data) => {
    if (err) return console.log(err, err.stack);

    const params = {
        TableName: 'MY_TABLE'
    };

    dynamodb.waitFor('tableExists', params, (err, data) => {
        if (err) console.log(err, err.stack);
        else     insertData();
    });    
});

Upvotes: 6

James
James

Reputation: 11931

Your table will not be immediately ready on creation, you have to wait for it to become active before writing to it. I found a Java code sample that illustrates how to do this, but the problem is not language-specific, it applies to any DynamoDB client.

You can see this in the AWS Management Console as the "Status" of the table, to confirm or deny this theory.

Upvotes: 3

Related Questions