Search code examples
amazon-dynamodbserverless-frameworkserverlessdynamodb-queries

Serverless define a Dynamodb that has GlobalSecondaryIndexes


Serverless Error --------------------------------------- I've gone back and forwards the documentation. seems like it should be a simple thing but can't figure out why it does not let me

An error occurred: documentsTable - One or more parameter values were invalid: Some index key attributes are not defined in AttributeDefinitions. Keys: [UserId], AttributeDefinitions: [userId, id] (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: K28DP2ST778DAN7CP1MV4BDKM3VV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null).

    documentsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: userId
            AttributeType: S
        KeySchema:
          - AttributeName: UserId
            KeyType: HASH
        GlobalSecondaryIndexes:
          - IndexName: userIdIndex
            KeySchema:
              - AttributeName: UserId
                KeyType: HASH
            Projection:
              ProjectionType: ALL
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:custom.env}-${self:custom.prefix}-${self:custom.documentDynamoTable2}

then ofcourse I will have a query like this

query(
            {
                TableName: documentTable,
                IndexName: 'userIdIndex',
                KeyConditionExpression: '#userId = :userId',
                ExpressionAttributeValues: {
                    ':userId': aUserId
                },
                ExpressionAttributeNames: {
                    "#userId": "userId"
                },
                Select: 'ALL_ATTRIBUTES',
            }
        ).promise();

error:

{
    "errorType": "ValidationException",
    "errorMessage": "Query condition missed key schema element: id",
    "code": "ValidationException",
    "message": "Query condition missed key schema element: id",
    "time": "2021-03-03T22:59:22.298Z",
    "requestId": "24B4KVLCT9PTIDPPN06ITUTG0JVV4KQNSO5AEMVJF66Q9ASUAAJG",
    "statusCode": 400,
    "retryable": false,
    "retryDelay": 28.89477589112033,
    "stack": [
        "ValidationException: Query condition missed key schema element: id",
..........
}

Solution

  • It looks like you are defining an attribute named userId (lowercase u) and later referring to UserId (uppercase U).

    You want this

    documentsTable:
          Type: AWS::DynamoDB::Table
          Properties:
            AttributeDefinitions:
              - AttributeName: id
                AttributeType: S
              - AttributeName: userId
                AttributeType: S
            KeySchema:
              - AttributeName: userId      // <--- CHANGE HERE
                KeyType: HASH 
            GlobalSecondaryIndexes:
              - IndexName: userIdIndex
                KeySchema:
                  - AttributeName: userId  // <--- CHANGE HERE
                    KeyType: HASH
                Projection:
                  ProjectionType: ALL
                ProvisionedThroughput:
                  ReadCapacityUnits: 1
                  WriteCapacityUnits: 1
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1
            TableName: ${self:custom.env}-${self:custom.prefix}-${self:custom.documentDynamoTable2}