1

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.

1
  • Which runtime/programming language are you using? Commented May 6, 2019 at 9:26

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there any better way to handle this from another Lambda function? Polling every second in the same lambda function, for around 30 seconds will not be a good idea in my case, as lambda function needs to send back the response synchronously.
There are no events (e.g. you can't invoke on CloudTrail like you can with some services) for completion finished. If you need to return you could invoke a second function to do the waiting from the first.
Thanks, this approach worked. I invoked another lambda function, in which I did polling to check if table status is "ACTIVE" after every 10 seconds. After the table status was "ACTIVE", I inserted the data into new table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.