Serverless Programming Cookbook
上QQ阅读APP看书,第一时间看更新

Creating a table using CLI commands

  1. We can create a simple DynamoDB table using the aws dynamodb create-table CLI command as follows:
aws dynamodb create-table \
--table-name my_table \
--attribute-definitions 'AttributeName=id, AttributeType=S' 'AttributeName=datetime, AttributeType=N' \
--key-schema 'AttributeName=id, KeyType=HASH' 'AttributeName=datetime, KeyType=RANGE' \
--provisioned-throughput 'ReadCapacityUnits=5, WriteCapacityUnits=5' \
--region us-east-1 \
--profile admin

Here, we define a table named my_table and use the attribute-definitions property to add two fields: id of type string (denoted by S) and datetime of type number (denoted by N). We then define a partition key (or hash key) and a sort key (or range key) using the key-schema property. We also define the maximum expected read and write capacity units per second using the provisioned-throughput property. I have specified the region even though us-east-1 is the default. 

  1. List tables using the aws dynamodb list-tables CLI command to verify our table was created:
aws dynamodb list-tables \
--region us-east-1 \
--profile admin
  1. Use the aws dynamodb describe-table CLI command to see the table properties:
aws dynamodb describe-table \
--table-name my_table \
--profile admin

The initial part of the response contains the table name, attribute definitions, and key schema definition we specified while creating the table:

The later part of the response contains TableStatus, CreationDateTime, ProvisionedThroughput, TableSizeBytes, ItemCount, TableArn and TableId:

  1. You may use the aws dynamodb update-table CLI command to update the table:
aws dynamodb update-table \
--table-name my_table \
--provisioned-throughput 'ReadCapacityUnits=10, WriteCapacityUnits=10' \
--profile admin
  1. Finally, you may delete the table using aws dynamodb delete-table:
aws dynamodb delete-table \
--table-name my_table \
--profile admin
We will be reusing this table in a later recipe when we work with data. If you are continuing with other recipes in this chapter now, you may delete the table after completing those recipes.