Create the table

Now that the primary key is designed, let’s create a table.

The code you downloaded in the initial steps include a Python script in the scripts/ directory named create_table.py. The Python script’s contents follow.

import boto3

dynamodb = boto3.client('dynamodb')

try:
    dynamodb.create_table(
        TableName='battle-royale',
        AttributeDefinitions=[
            {
                "AttributeName": "PK",
                "AttributeType": "S"
            },
            {
                "AttributeName": "SK",
                "AttributeType": "S"
            }
        ],
        KeySchema=[
            {
                "AttributeName": "PK",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "SK",
                "KeyType": "RANGE"
            }
        ],
        ProvisionedThroughput={
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        }
    )
    print("Table 'battle-royale' created successfully.")
except Exception as e:
    print("Could not create table. Error:")
    print(e)

Change the capacity units.

Edit scripts/create_table.py, set both ReadCapacityUnits and WriteCapacityUnits to 100 for battle-royale table and save the file.

The preceding script uses the CreateTable  operation using Boto 3 , the AWS SDK for Python. The operation declares two attribute definitions, which are typed attributes to be used in the primary key. Though DynamoDB is schemaless , you must declare the names and types of attributes that are used for primary keys. The attributes must be included on every item that is written to the table and thus must be specified as you are creating a table.

Because different entities are stored in a single table, you can’t use primary key attribute names such as UserId. The attribute means something different based on the type of entity being stored. For example, the primary key for a user might be its USERNAME, and the primary key for a game might be its GAMEID. Accordingly, you use generic names for the attributes, such as PK (for partition key) and SK (for sort key).

After configuring the attributes in the key schema, you specify the provisioned throughput  for the table. DynamoDB has two capacity modes: provisioned and on-demand. In provisioned capacity mode, you specify exactly the amount of read and write throughput you want. You pay for this capacity whether you use it or not.

In DynamoDB on-demand capacity mode, you pay per request. The cost per request is slightly higher than if you were to use provisioned throughput fully, but you don’t have to spend time doing capacity planning or worrying about getting throttled. On-demand mode works great for spiky or unpredictable workloads. In this lab, provisioned capacity mode is used.

You can choose to run either the create_table.py python script or the AWS CLI command below. Both are provided to show different methods of interacting with DynamoDB.

You can run the Python script with the following command in the Cloud9 Terminal.

python scripts/create_table.py

The script should return this message:

Table 'battle-royale' created successfully.

As an alternative, you can run the AWS CLI command from your Cloud9 Terminal.

aws dynamodb create-table \
--table-name battle-royale \
--attribute-definitions AttributeName=PK,AttributeType=S AttributeName=SK,AttributeType=S \
--key-schema AttributeName=PK,KeyType=HASH AttributeName=SK,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=100,WriteCapacityUnits=100

The CLI command should return this JSON:

{
  "TableDescription": {
    "AttributeDefinitions": [
      {
        "AttributeName": "PK",
        "AttributeType": "S"
      },
      {
        "AttributeName": "SK",
        "AttributeType": "S"
      }
    ],
    "TableName": "battle-royale",
    "KeySchema": [
      {
        "AttributeName": "PK",
        "KeyType": "HASH"
      },
      {
        "AttributeName": "SK",
        "KeyType": "RANGE"
      }
    ],
    "TableStatus": "CREATING",
    "CreationDateTime": "2023-12-06T13:52:52.187000-06:00",
    "ProvisionedThroughput": {
      "NumberOfDecreasesToday": 0,
      "ReadCapacityUnits": 1,
      "WriteCapacityUnits": 1
    },
    "TableSizeBytes": 0,
    "ItemCount": 0,
    "TableArn": "arn:aws:dynamodb:<AWS Region>:<Account ID>:table/battle-royale",
    "TableId": "<Unique Identifier>",
    "DeletionProtectionEnabled": false
  }
}