Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ceating dynamodb table says "invalid One or more parameter values were invalid: Some index key attributes are not defined in AttributeDefinitions"

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
like image 545
asp Avatar asked Jul 15 '20 15:07

asp


2 Answers

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',
    }
)
like image 94
berenbums Avatar answered Sep 30 '22 07:09

berenbums


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'
        },


like image 27
Sibel DeShong Avatar answered Sep 30 '22 06:09

Sibel DeShong