アプリなどを開発するブログ

React Native / Swift / Ruby on Railsなどの学習メモ。


serverless.ymlでDynamoDBテーブル定義エラー

以下みたいなDynamoDBテーブルをserverless.ymlで定義し、deployしようとするとエラーが。

resources:
  Resources:
    HogeUsers:
      Type: "AWS::DynamoDB::Table"
      Properties:
        TableName: Users
        AttributeDefinitions:
          - AttributeName: Id
            AttributeType: S
          - AttributeName: UserName
            AttributeType: S
        KeySchema:
          - AttributeName: Id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
エラー内容

Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes

そうでした。
DynamoDBはスキーマレスなので、キースキーマとなる属性以外最初に定義する必要はありません。

なので、キースキーマ(プライマリキー、レンジキー) やセカンダリインデックスで使用する以外の属性を記載していると、
キースキーマとセカンダリインデックスと一致しないよ、と怒られてしまいます。
RDBばかり使っていたので、たまに混乱することがあります。