Creating a dynamodb table using Lambda function (python) - error

Viewed 40

I have defined 3 attributes in that table definition. agentId, agentName, agentRole. I want to create KeySchema on agentId (partitionkey) , agentRole (range key).

In my understanding the table can have 10 attributes. All those 10 attributes don't have to be part of the KeySchema. Because Keyschema is used to identify unique records. Right?

It throws the following error:

Response

 {

     "errorMessage": "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",

    "errorType": "ClientError",

    "requestId": "d8d07c59-f36c-4989-9ac2-6ada9d8f6521",

    "stackTrace": [

               "  File \"/var/task/lambda_function.py\", line 8, in lambda_handler\n    
               response = client.create_table(\n",

              "  File \"/var/runtime/botocore/client.py\", line 391, in _api_call\n    return 
              self._make_api_call(operation_name, kwargs)\n",

              "  File \"/var/runtime/botocore/client.py\", line 719, in _make_api_call\n    
      raise error_class(parsed_response, operation_name)\n"

   ]

 }

    import json
    import boto3
    client = boto3.client("dynamodb")
    def lambda_handler(event, context):  

             response = client.create_table(

                  AttributeDefinitions=[
                 {
                      'AttributeName': 'agentId',
                      'AttributeType': 'N'
                 },
                 {
                      'AttributeName': 'agentRole',
                      'AttributeType': 'S'
                 }, 
                 {
                      'AttributeName': 'agentName',
                      'AttributeType': 'S'    
                 }

                ],

                TableName='CallCenterCallsTable,
                KeySchema=[
                {
                      'AttributeName': 'agentId',
                      'KeyType': 'HASH'                 

                },                
               {

                      'AttributeName': 'agentRole',
                      'KeyType': 'RANGE'

               }

             ],

              BillingMode='PROVISIONED',
              ProvisionedThroughput={
                  'ReadCapacityUnits': 1,
                  'WriteCapacityUnits': 1

              }
          )  

         print(response)   
1 Answers

Remove agentName from the Attribute definitions.

See the documentation for Attribute Definitions:

Represents an attribute for describing the key schema for the table and indexes.

You aren't using agentName in the key schema or indexes, so it shouldn't be included in the table definition. DynamoDB is schemaless. You only need to define the hash key and sort key at creation time. DynamoDB doesn't care about any other attributes you may want to insert into your table.

Related