Cloudformation DynamoDB index with GSI

#StandWithUkraine
Today, 4th December 2022, Ukraine is still bravely fighting for democratic values, human rights and peace in whole world. Russians ruthlessly kill all civilians in Ukraine including childs and destroy their cities. We are uniting against Putinโ€™s invasion and violence, in support of the people in Ukraine. You can help by donating to Ukrainian's army.

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