Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define DynamoDB table with global secondary index in serverless framework

My DynamoDB table

  • awsRequestID (S)
  • ttl (N)
  • createdate (S) (ISO)
  • user_id (S)
  • message (S)

What I try to achieve

I want to have a global secondary index so I can query to filter all the messages of a users and I get them ordered (using the sort key) as explained in https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

serverless.yml

resources:
  Resources:
    EventsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:custom.eventsTable}
        AttributeDefinitions:
          - AttributeName: awsRequestID
            AttributeType: S
          - AttributeName: ttl
            AttributeType: N
          - AttributeName: createdate
            AttributeType: S
          - AttributeName: user_id
            AttributeType: S
          - AttributeName: message
            AttributeType: S
        KeySchema:
          - AttributeName: awsRequestID
            KeyType: HASH
        GlobalSecondaryIndexes:
          - IndexName: UserIdIndex
            KeySchema:
              - AttributeName: user_id
                KeyType: HASH
              - AttributeName: createdate
                KeyType: RANGE
            Projection:
              ProjectionType: 'ALL'
        TimeToLiveSpecification:
          AttributeName: ttl
          Enabled: true
        BillingMode: PAY_PER_REQUEST

Error when I deploy

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

like image 741
Cecile Avatar asked Aug 02 '19 08:08

Cecile


People also ask

What is global secondary index in DynamoDB?

Global secondary index — An index with a partition key and a sort key that can be different from those on the base table. A global secondary index is considered "global" because queries on the index can span all of the data in the base table, across all partitions.

How many global secondary Indexs are there in DynamoDB?

Each table in DynamoDB can have up to 20 global secondary indexes (default quota) and 5 local secondary indexes. For more information about the differences between global secondary indexes and local secondary indexes, see Improving data access with secondary indexes.

Does GSI need to be unique?

In a DynamoDB table, each key value must be unique. However, the key values in a global secondary index do not need to be unique.


1 Answers

Apparently the right syntax is this.

These were the errors:

  • You cannot add the ttl column to AttributeDefinitions (otherwise you get the error of the question)
  • You must have the columns you need for the global secondary index in the attribute definitions (otherwise you get the exact same error)
  • You must not have extra columns (message) in the attribute definitions (all give the exact same error message)
resources:
  Resources:
    EventsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:custom.eventsTable}
        AttributeDefinitions:
          - AttributeName: awsRequestID
            AttributeType: S
          - AttributeName: user_id
            AttributeType: S
          - AttributeName: createdate
            AttributeType: S
        KeySchema:
          - AttributeName: awsRequestID
            KeyType: HASH
        GlobalSecondaryIndexes:
          - IndexName: UserIdIndex
            KeySchema:
              - AttributeName: user_id
                KeyType: HASH
              - AttributeName: createdate
                KeyType: RANGE
            Projection:
              ProjectionType: 'ALL'
        TimeToLiveSpecification:
          AttributeName: ttl
          Enabled: true
        BillingMode: PAY_PER_REQUEST
like image 73
Cecile Avatar answered Sep 20 '22 19:09

Cecile