Dattatray
Dattatray

Reputation: 1871

Dynamo DB : inserting data while creating the table through a lambda function

I have a lambda function which I am using to create the DynamoDB table.

I have a requirement, wherein I need to insert some data in this table, after the table is created.

In DynamoDB, Create table is an asynchronous call. While the table is being created, it is in "CREATING" state and after that it goes into "ACTIVE" state.

The challenge is, I can't insert the data in this table, till it is not in "ACTIVE" state and I get ResourceNotFoundException exception.

Is there any way, I can insert this data in the table while it is created?

I want to complete the table creation and the data insertion in the same Lambda function call.

Upvotes: 1

Views: 932

Answers (1)

thomasmichaelwallace
thomasmichaelwallace

Reputation: 8482

As you've discovered, you can only write to an active(/created) table, and there is no way to provide data for 'preloading' your table using dynamodb::CreateTable.

There are no events emitted for when the table is ready. So instead you'll have to poll until the table becomes active. This should be easily achieved in a lambda, as DynamoDB rarely takes more than 30-60 seconds to provision a table.

After creating the table, you can call dynamodb::DescribeTable every second (or so) and wait until it returns Table.TableStatus === 'ACTIVE'. Once they table status has turned to active, you can insert your initial data. Just remember to increase your Lambda timeout to the full 15 minutes, in case it does take AWS longer to provision your table.

You can see an example of this in the AWS Documentation.

Upvotes: 3

Related Questions