Global Index creation failure

I have some trouble creating a index for a dynamodb table. The table default primary key is “id”, I would like to create a global secondary index based on attribute “email” (as partition key, no any sort key) .

This is the snippet of setting in serverless.yml

ClientsDynamoDbTable:
  Type: 'AWS::DynamoDB::Table'
  DeletionPolicy: ${self:custom.deletion_policy}
  Properties:
    AttributeDefinitions:
      -
        AttributeName: id
        AttributeType: S
      -
        AttributeName: name
        AttributeType: S
      -
        AttributeName: email
        AttributeType: S
      -
        AttributeName: salt
        AttributeType: S
      -
        AttributeName: password
        AttributeType: S
      -
        AttributeName: enabled
        AttributeType: N
      -
        AttributeName: guid
        AttributeType: S
      -
        AttributeName: checked
        AttributeType: N
      -
        AttributeName: createdAt
        AttributeType: S
      -
        AttributeName: updatedAt
        AttributeType: S
    KeySchema:
      -
        AttributeName: id
        KeyType: HASH
    GlobalSecondaryIndexes:
      -
        IndexName: email-index
        KeySchema:
        -
          AttributeName: email
          KeyType: HASH
        Projection:
          ProjectionType: KEYS_ONLY
        ProvisionedThroughput:
          ReadCapacityUnits: ${self:custom.read_capacity_units}
          WriteCapacityUnits: ${self:custom.write_capacity_units}
    ProvisionedThroughput:
      ReadCapacityUnits: ${self:custom.read_capacity_units}
      WriteCapacityUnits: ${self:custom.write_capacity_units}
    TableName: ${self:custom.service}-${self:custom.stage}-clients

I ran “serverless deploy” then got the following error. My udnerstanding is the table default primary key and partition key in global secondary index can be different, why serverless complains about the inconsistent keyschema. How to fix it.

Serverless: Operation failed!

Serverless Error ---------------------------------------

An error occurred: ClientsDynamoDbTable - Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes.

Figure out only and must only put the attributes used by index into attribute definition.
https://maketips.net/tip/156/cloudformation-dynamodb-index-with-gsi

2 Likes