What is the meaning of this error "Invalid KeySchema: Some index key attribute have no definition (Service: AmazonDynamoDBv2);

Viewed 1095

Could you please tell me anything that I've missed at this code.

resources:
  Resources:
    ScholarshipsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: purrfectScholarshipsTable
        AttributeDefinitions:
          - AttributeName: postId
            AttributeType: S
        KeySchema:
          - AttributeName: postId
            KeyType: HASH
        GlobalSecondaryIndexes:
          - IndexName: scholarshipCategoryIndex
            KeySchema:
              - AttributeName: category
                KeyType: HASH
              - AttributeName: deadline
                KeyType: RANGE
            Projection:
              ProjectionType: "ALL"
        BillingMode: PAY_PER_REQUEST

I use the serverless framework and I got the error. Any mistakes that I make?

1 Answers

Every key you use in any index (GSI, LSI, etc) needs to be listed in the AttributeDefinitions block. So this should work:

resources:
  Resources:
    ScholarshipsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: purrfectScholarshipsTable
        AttributeDefinitions:
          - AttributeName: postId
            AttributeType: S
          - AttributeName: category
            AttributeType: S
          - AttributeName: deadline
            AttributeType: N
        KeySchema:
          - AttributeName: postId
            KeyType: HASH
        GlobalSecondaryIndexes:
          - IndexName: scholarshipCategoryIndex
            KeySchema:
              - AttributeName: category
                KeyType: HASH
              - AttributeName: deadline
                KeyType: RANGE
            Projection:
              ProjectionType: "ALL"
        BillingMode: PAY_PER_REQUEST

You can read more in the documentation

Related