amazon-web-servicesamazon-dynamodbaws-cloudformationamazon-dynamodb-index

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


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.


Solution

  • - 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