Search code examples
node.jsamazon-web-servicesamazon-dynamodbnosqlserverless

DynamoDB GSI empty?


I'm doing a small serverless todo app and I created a GSI in an empty table in the dynamo GUI, the table has a PK = id and SK = userId with the GSI PK = gsi1pk which is a combo of the userId and todo and SK = status.

I've added items using my api with this schema in the code

 Item: {
            id: { S: uuidv4() },
            userId: { S: todo.userId },
            title: { S: todo.title },
            description: { S: todo.description },
            createdAt: { S: currentDate.toISOString() },
            status: { S: todo.status },
            entityType: { S: 'todo' },
            gsi1pk: { S: `${todo.userId}#todo` }
        }

with the API request being

{
    "userId": "emma1",
    "title": "Schedule gym session",
    "description": "Book a workout session with a personal trainer",
    "createdAt": "2023-05-26T11:45:00Z",
    "status": "pending",
    "entityType": "todo"
}

And my GSI is still saying my item count is zero? What have I done wrong?


Solution

  • Your GSI is saying your item count is 0 because you only created it, the count is only updated every 6 hours.

    To do a live count, do a Scan with Select=COUNT to retrieve an accurate count.

    aws dynamodb scan \
    --table-name myTable \
    --index-name myIndex \
    --select COUNT
    

    If you are still seeing no items, then ensure

    1. That your items contain both the GSI PK and the GSI SK
    2. That the attribute values match the type string/number/binary
    3. That the attribute names match, casing also counts