73
votes

I have following template that i am using in cloudformation UI to create dynamoDB table. I want to create a table with PrimaryKey as ID and sortKey as Value

{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "DB Description",

  "Resources" : {
    "TableName" : {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions": [ { 
          "AttributeName" : "ID",
          "AttributeType" : "S"
        }, { 
          "AttributeName" : "Value",
          "AttributeType" : "S"
        } ],
        "KeySchema": [
          { 
            "AttributeName": "ID", 
            "KeyType": "HASH"
          }
        ]                
      },
      "TableName": "TableName"
    }
  }
}

On the CF UI, I click on new stack, point to the template file from my local computer, give stack a name and click next. After sometime, I get error that says Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes

3
CloudFormation Linter rule to help catch this quicker with more information: github.com/aws-cloudformation/cfn-python-lint/pull/1284Pat Myron

3 Answers

125
votes

The issue is that the Resources.Properties.AttributeDefinitions key must only define columns used for indexes or keys. In other words, the keys in Resources.Properties.AttributeDefinitions must match the same keys defined in Resources.Properties.KeySchema.

AWS docs:

AttributeDefinitions: A list of AttributeName and AttributeType objects that describe the key schema for the table and indexes.

so the resulting template would look like this:

{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "DB Description",

  "Resources" : {
    "TableName" : {
    "Type" : "AWS::DynamoDB::Table",
    "Properties" : {
      "AttributeDefinitions": [ { 
        "AttributeName" : "ID",
        "AttributeType" : "S"
      } ],
      "ProvisionedThroughput":{
        "ReadCapacityUnits" : 1,
        "WriteCapacityUnits" : 1
      },
      "KeySchema": [
        { 
          "AttributeName": "ID", 
          "KeyType": "HASH"
        }
       ] ,               
      "TableName": "table5"
    }
   }
  }
}
1
votes

The accepted answer is correct in the cause of the error, but you said you wanted the sort key to be Value. So you should change your CloudFormation to include that:

{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "DB Description",

  "Resources" : {
    "TableName" : {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions": [ { 
          "AttributeName" : "ID",
          "AttributeType" : "S"
        }, { 
          "AttributeName" : "Value",
          "AttributeType" : "S"
        } ],
        "KeySchema": [
          { 
            "AttributeName": "ID", 
            "KeyType": "HASH"
          },
          { 
            "AttributeName": "Value", 
            "KeyType": "RANGE"
          }
        ]                
      },
      "TableName": "TableName"
    }
  }
}
0
votes

In AttributeDefinitions you need to define just partition and range keys not other attributes.

number of attributes in AttributeDefinitions and KeySchema should match and should be exactly same.