Home > OS >  How can I fix the error "DynamoDB returns GSI range key not specified errors"?
How can I fix the error "DynamoDB returns GSI range key not specified errors"?

Time:12-22

I have a cloudformation template regarding the dynamodb. I added new index called customerId-index as below:

ComponentsTable:
  Type: AWS::DynamoDB::Table
  Properties:
    TableName: ${self:custom.base}-components
    PointInTimeRecoverySpecification:
      PointInTimeRecoveryEnabled: True
    StreamSpecification:
      StreamViewType: NEW_IMAGE
    BillingMode: PAY_PER_REQUEST
    AttributeDefinitions:
      - AttributeName: assetId
        AttributeType: S
      - AttributeName: componentId
        AttributeType: S
      - AttributeName: componentType
        AttributeType: S
      - AttributeName: customerId
        AttributeType: S        
    KeySchema:
      - AttributeName: componentId
        KeyType: HASH
      - AttributeName: assetId
        KeyType: RANGE
    LocalSecondaryIndexes:
      - IndexName: componentType-index
        KeySchema:
          - AttributeName: componentId
            KeyType: HASH
          - AttributeName: componentType
            KeyType: RANGE
        Projection:
          ProjectionType: ALL
    GlobalSecondaryIndexes:
      - IndexName: assetId-index
        KeySchema:
          - AttributeName: assetId
            KeyType: HASH
          - AttributeName: componentId
            KeyType: RANGE
        Projection:
          ProjectionType: ALL
      - IndexName: compoentType-gsi
        KeySchema:
          - AttributeName: componentType
            KeyType: HASH
          - AttributeName: componentId
            KeyType: RANGE
        Projection:
          ProjectionType: ALL
      - IndexName: customerId-index
        KeySchema:
          - AttributeName: customerId
            KeyType: HASH
          - AttributeName: siteId
            KeyType: RANGE
        Projection:
          ProjectionType: ALL            

And although I added AttributeName for customerId in AttributeDefinitions, I am still getting the following error:

ValidationException: Global Secondary Index range key not specified in Attribute Definitions.Type unknown.

But the specified index range key is and its type already defined as below:

    AttributeDefinitions:
      - AttributeName: assetId
        AttributeType: S
      - AttributeName: componentId
        AttributeType: S
      - AttributeName: componentType
        AttributeType: S
      - AttributeName: customerId
        AttributeType: S   

I wonder if someone can help with the problem. Thanks.

CodePudding user response:

- IndexName: customerId-index
        KeySchema:
          - AttributeName: customerId
            KeyType: HASH
          - AttributeName: siteId
            KeyType: RANGE

Your final index uses siteId which is not defined in the Attribute Definitions. Try below:

    AttributeDefinitions:
      - AttributeName: assetId
        AttributeType: S
      - AttributeName: componentId
        AttributeType: S
      - AttributeName: componentType
        AttributeType: S
      - AttributeName: customerId
        AttributeType: S
      - AttributeName: siteId
        AttributeType: S  

CodePudding user response:

In the updated question there is this Global Secondary Index:

      - IndexName: customerId-index
        KeySchema:
          - AttributeName: customerId
            KeyType: HASH
          - AttributeName: siteId
            KeyType: RANGE

Here, you're referring to siteId which is missing from your AttributeDefinitions list. You probably want to add something like this:

    AttributeDefinitions:
      - AttributeName: siteId
        AttributeType: S

DynamoDB needs to know the data types to create Secondary Indexes. That's why you need to ensure all attributes that are used in indexes are specified.

  • Related