amazon web services - Pagination with secondary index with DynamoDB/Node.js

I have a problem with the pagination of a global secondary index in Dynamodb :/

My DynamoDB schema is:

Resources:
  ImportsTable:
    Type: AWS::DynamoDB::Table
    Properties:
      # Generate a name based on the stage
      TableName: ${self:service}-${self:custom.stage}-imports
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
        - AttributeName: fixedKey
          AttributeType: S
        - AttributeName: timestamp
          AttributeType: N
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      StreamSpecification:
        StreamViewType: NEW_IMAGE
      ProvisionedThroughput:
        ReadCapacityUnits: ${self:custom.app.tableThroughput.imports}
        WriteCapacityUnits: ${self:custom.app.tableThroughput.imports}
      TimeToLiveSpecification:
        AttributeName: expirationTime
        Enabled: true
      GlobalSecondaryIndexes:
        - IndexName: time-index
          KeySchema:
            - AttributeName: fixedKey
              KeyType: HASH
            - AttributeName: timestamp
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
          ProvisionedThroughput:
            ReadCapacityUnits: ${self:custom.app.tableThroughput.imports}
            WriteCapacityUnits: ${self:custom.app.tableThroughput.imports}
      SSESpecification:
        SSEEnabled: true

My query params :

let params = {
            TableName: process.env.importsTableName,
            IndexName: 'time-index',
            KeyConditionExpression: 'fixedKey = :fk',
            Limit: 5,
            ProjectionExpression: "timeBasedId, importFileS3Key, meta, #st, #ch, success, errors, #ty, #id, email",
            ScanIndexForward: true,
            ExpressionAttributeNames: {
                "#ch": "Attributes",
                "#st": "status",
                "#ty": "type",
                "#id": "identity",
            },
            ExpressionAttributeValues: {
                ":fk" : "fixedKey",
            },
 };

When i run this, i get a response with this :

   LastEvaluatedKey: {
    id: 88de14a0-2475-11e9-a0ee-d317558aa61b
    fixedKey: fixedKey
    timestamp: 1548842283754
}

So for the next call, i added this in my params :

ExclusiveStartKey: {
                    id: event.queryStringParameters.id,
                }

The event.queryStringParameters.id looks like a good Key

88de14a0-2475-11e9-a0ee-d317558aa61b

But when i run it, i get a 500 error and this message :

The provided starting key is invalid

I found a lead that told me to add the whole LastEvaluatedKey to the request but when i run my query with this :

ExclusiveStartKey: {
                    id: event.queryStringParameters.id,
                    fixedKey: event.queryStringParameters.fixedKey,
                    timestamp: event.queryStringParameters.timestamp
                }

When event.queryStringParameters looks like :

{ fixedKey: 'fixedKey',
id: '88de14a0-2475-11e9-a0ee-d317558aa61b',
timestamp: '1548842283754' }

I get this error :

The provided key element element does not math the schema

1 Answer

  1. Kevin- Reply

    2019-11-13

    I finnaly found the solution, timestamp is a number in my schema, so i needed to parseInt() it in params :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>