1

I'm trying to build a template for each of my tables and I'm testing it with one table. However, I'm getting this error and I have no idea on how to fix it: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: NRTF448TGFEUGUB3H5UH37AGHBVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)

The template that I'm using is this one:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Resources" : {
      "myddbtable": {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions" : [ 
          {
            "AttributeName": "phoneNumber",
            "AttributeType": "S" 
          },
          {
            "AttributeName": "fname",
            "AttributeType": "S" 
          },
          {
            "AttributeName": "lname",
            "AttributeType": "S" 
          }
          ],
        "BillingMode" : "PAY_PER_REQUEST",
        "KeySchema" : [
          {
            "AttributeName" : "phoneNumber",
            "KeyType": "HASH"
          }
        ],
        "TableName" : "UserTable2"
      }
    }
  }
}

I tried putting the fname and the lname attributes on the KeySchema section with a KeyType of Range and it didn't work either, any ideas?

Diego Duarte
  • 139
  • 10
  • 1
    we only need to define those attributes used in indexes. I see you are not using fname and lname anywhere. You can remove those two and try! – Balu Vyamajala Feb 02 '21 at 23:47
  • Does this answer your question? [CloudFormation insists my DynamoDB creation JSON is invalid .. but I can't see how](https://stackoverflow.com/questions/38142870/cloudformation-insists-my-dynamodb-creation-json-is-invalid-but-i-cant-see-h) – Pat Myron Feb 03 '21 at 04:22

1 Answers1

3

As DynamoDB is schema less, we can add any attributes when creating/updating individual records. Hence when creating DynamoDB tables, we only need to define attributes which are part of primary index or global secondary index.

In this case, table has only primary key of phoneNumber. We need to remove two other attributes fname and 'lname` from definition

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "myddbtable": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        "AttributeDefinitions": [
          {
            "AttributeName": "phoneNumber",
            "AttributeType": "S"
          }
        ],
        "BillingMode": "PAY_PER_REQUEST",
        "KeySchema": [
          {
            "AttributeName": "phoneNumber",
            "KeyType": "HASH"
          }
        ],
        "TableName": "UserTable2"
      }
    }
  }
}
Balu Vyamajala
  • 9,287
  • 1
  • 20
  • 42
  • But in that way I will have to define all the other fields manually rather than describing them right there. Is there a way to add such attributes? – Diego Duarte Feb 03 '21 at 00:16
  • @DiegoDuarte There is such way, as DdB does not have any schema, unless you start adding local or global secondary indices. But if you require schema, you should consider using regular databases, such as RDS. – Marcin Feb 03 '21 at 00:20
  • @Marcin Well. That makes sense. I guess I'll have to stick to that then and add any other fields on the lambda function. Thank you! – Diego Duarte Feb 03 '21 at 00:22