11

I was wondering if its possible to create a dynamodb table from the table definition json itself? I've looked into the aws cli tool and I couldnt find any way of doing this. Just wondering if it's possible.

aws dynamodb create-table file://tabledenifision.json

Thanks!

webber
  • 1,834
  • 5
  • 24
  • 56
  • 1
    What do you mean you "had no luck"? Did you get an error, and what did the error say? What is inside the `tabledenifision.json` file? – kurtzbot Aug 20 '14 at 16:22
  • 1
    Sorry about that. I meant I couldnt find any way of doing it. There's no command to read in an entire table schema as json – webber Aug 20 '14 at 21:01
  • The first two searches on Google for "aws create table json" seem really promising, both directly from AWS itself. [1](http://docs.aws.amazon.com/cli/latest/userguide/shorthand-syntax.html) [2](http://docs.aws.amazon.com/cli/latest/reference/dynamodb/create-table.html). Do neither of these accomplish what you want? – kurtzbot Aug 21 '14 at 01:28
  • No, neither of the links accomplish it. – Amit Naidu Jun 18 '19 at 18:28
  • 1
    If you don't know what to put inside `tabledefinition.json` file, you might use https://dynobase.dev/dynamodb-table-schema-design-tool/. It helps me a lot. – Rafal Wiliński May 06 '20 at 08:00
  • Additional answers on this duplicate question: https://stackoverflow.com/questions/42100210/how-to-export-an-existing-dynamo-table-schema-to-json#answer-67695947 – Curtis Yallop May 27 '21 at 16:55

2 Answers2

36

I found your unanswered question today, and kept looking until I found an answer. Probably too late for you but may help others.

aws dynamodb create-table --cli-input-json file://tabledefinition.json

This command expects a json object in the file in the format outlined by the following command:

aws dynamodb create-table --generate-cli-skeleton

This approach seems to work with many aws commands.

Keith Branton
  • 361
  • 3
  • 5
  • is it possible to reverse-engineer the json file from an existing dynamodb table? – Anthony Kong Nov 04 '20 at 13:59
  • 2
    @AnthonyKong Yes, use aws dynamo describe-table --table-name existing_table_name and the output is JSON `Table` property is in the same format – Luke Sep 17 '21 at 11:27
0

I wrote a Python script for this. My version is for yaml, but can easily be adapted to json.

Single dependency for parsing the template, with thanks to this SO post

pip install cfn_flip

#!/usr/bin/env python

import subprocess
import time
import yaml
from cfn_tools import load_yaml, dump_yaml

template_file = open('./template.yaml')
template = load_yaml(template_file.read())

for resource_key in template["Resources"]:
  resource = template["Resources"][resource_key]
  if resource["Type"] != 'AWS::DynamoDB::Table':
    continue
  
  table_name = resource["Properties"]["TableName"]
  print(f'Creating table "{table_name}"')
  
  table_definition = template["Resources"][resource_key]["Properties"]
  time_to_live_definition = table_definition.get("TimeToLiveSpecification")
  if time_to_live_definition:
    # TimeToLiveSpecification is not accepted by create-table, it is a separate command
    del(table_definition["TimeToLiveSpecification"])

  subprocess.run(["aws", "dynamodb", "create-table", "--cli-input-yaml",dump_yaml(table_definition)])
  if time_to_live_definition:
    time_to_live_definition = {
      "TableName": table_name,
      "TimeToLiveSpecification": time_to_live_definition
    }
    subprocess.run(["aws", "dynamodb", "update-time-to-live", "--cli-input-yaml", dump_yaml(time_to_live_definition)])
  
  # Tables are created asynch, give some time to complete
  time.sleep(1)

The sleep at the end is for following warning mentioned in the help of create-table. I have only tested this locally so far, so a smarter way to wait for completion might be needed.

You can optionally define secondary indexes on the new table, as part of the "CreateTable" operation. If you want to create multiple tables with secondary indexes on them, you must create the tables sequentially. Only one table with secondary indexes can be in the "CREATING" state at any given time.

DieterDP
  • 4,039
  • 2
  • 29
  • 38