0

I'm learning DynamoDB.

I want to create a table with the following Primary Key:

  • Partition key: name
  • Sort key: creationDate

And with the following attributes:

  • name (pk)
  • address (sk)
  • creationDate
  • isActive

This is my TS interface (just in case it helps to describe what I want)

interface User {
  readonly name: string;
  readonly address: string;
  readonly creationDate: Date;
  readonly isActive: boolean;
}

I wrote the following template:

Resources:
  myDynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
      - AttributeName: 'name'
        AttributeType: 'S'
      - AttributeName: 'address'
        AttributeType: 'S'
      - AttributeName: 'creationDate'
        AttributeType: 'S'
      - AttributeName: 'isActive'
        AttributeType: 'S'
      KeySchema:
      - AttributeName: 'name'
        KeyType: HASH
      - AttributeName: 'creationDate'
        KeyType: RANGE

When I deploy it, it fails with the following error

One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions

I google it and found several answers but I still don't understand what's going and how can I have those attributes in my table (all items are going to have those attributes).

Is there a way I can do it? If so, what am I doing wrong? Thanks

Marcin
  • 215,873
  • 14
  • 235
  • 294
Peter
  • 2,004
  • 2
  • 24
  • 57
  • Only attributes used as keys have to be declared upfront – Pat Myron Jan 12 '21 at 00:53
  • Does this answer your question? [CloudFormation insists my DynamoDB creation JSON is invalid .. but I can't see how](https://stackoverflow.com/questions/38142870/cloudformation-insists-my-dynamodb-creation-json-is-invalid-but-i-cant-see-h) – Pat Myron Jan 19 '21 at 22:53

1 Answers1

1

You can't have isActive and address in your AttributeDefinitions as they are not in your KeySchema. You are also missing ProvisionedThroughput. So the correct table definition is (example):

Resources:
  myDynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
      - AttributeName: 'name'
        AttributeType: 'S'
      - AttributeName: 'creationDate'
        AttributeType: 'S'
      KeySchema:
      - AttributeName: 'name'
        KeyType: HASH
      - AttributeName: 'creationDate'
        KeyType: RANGE
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
Marcin
  • 215,873
  • 14
  • 235
  • 294