10

I am trying to create dynamo db table with python. Below is the script i have. I am trying to create one partition key and sort key and bunch of columns.

What I tried:

import boto3

dynamodb = boto3.resource('dynamodb')

table = dynamodb.create_table(
    TableName='g_view_data',
    KeySchema=[
        {
            'AttributeName': 'customer_id',
            'KeyType': 'HASH'  #Partition key
        },
        {
            'AttributeName': 'key_id',
            'KeyType': 'RANGE'  #Sort key
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': 'cusotmer_id',
            'AttributeType': 'N'
        },
        {
            'AttributeName': 'key_id',
            'AttributeType': 'N'
        },
        {
            'AttributeName': 'dashboard_name',
            'AttributeType': 'S'
        },

        {
            'AttributeName': 'tsm',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'security_block',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'core_block',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'type',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'subscription',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'account_id',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'region',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'NAT',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'jb',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'dc',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'av',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'gl',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'backup',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'cpm',
            'AttributeType': 'S'
        },
            {
            'AttributeName': 'zb',
            'AttributeType': 'S'
        },
    ],
    ProvisionedThroughput={
        'ReadCapacityUnits': 10,
        'WriteCapacityUnits': 10
    }
)

print("Table status:", table.table_status)

Output i am getting:

 invalid One or more parameter values were invalid: Some index key attributes are not defined in AttributeDefinitions. 

can some one suggest what wrong with my code..just trying to create a simple table. why it complains parameters are missing. not sure..can some one suggest pls

Edit1: after adding customer_id,key_id in attributes i am getting different error

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the CreateTable operation: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions
asp
  • 777
  • 3
  • 14
  • 33

2 Answers2

17

As stated in the AWS documentation, the attributes in KeySchema must also be defined in the AttributeDefinitions. Please try adding customer_id and key_id to your AttributeDefinitions as well.

As for the AttributeDefinitions, they are used for the keys only (primary key and indexes). So here is an example that worked for me:

import boto3

dynamodb = boto3.resource('dynamodb')

table = dynamodb.create_table(
    TableName='g_view_data',
    KeySchema=[
        {
            'AttributeName': 'customer_id',
            'KeyType': 'HASH'
        },
        {
            'AttributeName': 'key_id',
            'KeyType': 'RANGE'
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': 'customer_id',
            'AttributeType': 'N'
        },
        {
            'AttributeName': 'key_id',
            'AttributeType': 'N'
        },
    ],
    ProvisionedThroughput={
        'ReadCapacityUnits': 10,
        'WriteCapacityUnits': 10
    }
)

print("Table status:", table.table_status)

Then after creating that table you can add items with any attributes that you want. For example add this code to your scripts:

table = dynamodb.Table('g_view_data')
item = table.put_item(
    Item={
        'customer_id': 234,
        'key_id': 123,
        'dashboard_name': 'test',
    }
)
berenbums
  • 1,239
  • 1
  • 5
  • 11
  • 1
    Hi, after adding customer_id and key_id i am getting different error `botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the CreateTable operation: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions` – asp Jul 15 '20 at 15:43
  • what about others columns, can't i include other columns ? – asp Jul 15 '20 at 16:14
  • 1
    you can add other attributes, but as they are all non-key attributes they don't need to be specified when creating the table. Please see my edit – berenbums Jul 15 '20 at 16:38
7

This is super late but you have a typo in customer_id in your attribute definitions

    KeySchema=[
        {
            'AttributeName': 'customer_id',
            'KeyType': 'HASH'  #Partition key
        },
        {
            'AttributeName': 'key_id',
            'KeyType': 'RANGE'  #Sort key
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': 'cusotmer_id',
            'AttributeType': 'N'
        },


Sibel DeShong
  • 93
  • 1
  • 5