Cloudformation DynamoDB index with GSI

Db structure for this example described in the hint Create a secondary index in DynamoDB

Cloudformation example:

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Resources" : {
    "GameScoresTable" : {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions" : [
          {
            "AttributeName" : "UserId",
            "AttributeType" : "S"   
          },
          {
            "AttributeName" : "GameTitle",
            "AttributeType" : "S"
          },
          {
            "AttributeName" : "TopScore",
            "AttributeType" : "N"
          }
        ],
        "KeySchema" : [
          {
            "AttributeName" : "UserId",
            "KeyType" : "HASH"
          },
          {
            "AttributeName" : "GameTitle",
            "KeyType" : "RANGE"
          }
        ],
        "ProvisionedThroughput" : {
          "ReadCapacityUnits" : "5",
          "WriteCapacityUnits" : "5"
        },
        "TableName" : "GameScores",
        "GlobalSecondaryIndexes" : [{
          "IndexName" : "GameTitleIndex",
          "KeySchema" : [
            {
              "AttributeName" : "GameTitle",
              "KeyType" : "HASH"
            },
            {
              "AttributeName" : "TopScore",
              "KeyType" : "RANGE"
            }
          ],                         
          "Projection" : {
            "NonKeyAttributes" : ["UserId"],
            "ProjectionType" : "INCLUDE"
          },
          "ProvisionedThroughput" : {
            "ReadCapacityUnits" : "5",
            "WriteCapacityUnits" : "5"
          }
        }]
      }
    }
  }
}

Please note that we don't put all attributes of table records in AttributeDefinitions section! You should put only the attributes that you use in any of the keys, primary or secondary! Otherwise, you will get the error:

Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes

This is because DynamoDB is schemaless (except the key schema)

The result will look on the UI console like this:

DynamoDB index with GSI

#dynamodb
0
Ivan Borshchov profile picture
Feb 28, 2017
by Ivan Borshchov
Did it help you?
Yes !
No

Best related