Udemy初夏のビッグセール開催中(~5/23)人気教材が1,300円~

【CloudFormationトラブルシュート】DynamoDB作成時”Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions”

CloudFormationでDynamoDBを作成しようとすると“Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions”といったエラーが発生することがあります。原因と対処法について記述します。

CloudFormationスタック更新時のエラーメッセージ
目次

原因 yamlにキー以外の属性を定義できない(しなくてもよい)

DynamoDBを作成するyamlにはキーではない属性を定義できません。DynamoDBでは通常の属性(カラム)はテーブル作成時に定義する必要がありません。
なお、インデックスのキーとなっている属性についてはAttributeDefinitionsに定義する必要があります。このことについては対処法で詳しく記述してみます。

対処法 yamlのAttributeDefinitionsからキー以外の属性を取り除く

yamlファイルのAttributeDefinitionsからキー以外の属性を除きます。
例えば以下yamlであれば27行目~28行目のAttribute1はキーではないにもかかわらずAttributeDefinitionsに記載されています。これを削除すればエラーは解消されます。

#AWS CloudFormation スタックを作成のリージョンで作成されます。対象リージョンでスタックを展開してください。
#===============================================================================
# CloudFormation Template
#===============================================================================
AWSTemplateFormatVersion: 2010-09-09
Description: DynamoDB Template

#===============================================================================
# Resources
#===============================================================================
Resources:

#============================================================
#テーブル作成
#============================================================
  tableName:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: "TableName"
      AttributeDefinitions:
       - AttributeName: KeyId
         AttributeType: S
       - AttributeName: SortId
         AttributeType: S
       - AttributeName: GsiKey
         AttributeType: S
       - AttributeName: Attribute1
         AttributeType: S

      KeySchema:
       - AttributeName: KeyId
         KeyType: HASH
       - AttributeName: SortId
         KeyType: RANGE
      ProvisionedThroughput:
        ReadCapacityUnits: '1'
        WriteCapacityUnits: '1'
      PointInTimeRecoverySpecification: 
       PointInTimeRecoveryEnabled: false
      #============================================================
      #インデックス(GSI)の作成
      #============================================================
      GlobalSecondaryIndexes:
        - IndexName: Gsi1
          KeySchema:
            - AttributeName: GsiKey
              KeyType: HASH
          Projection:
            NonKeyAttributes:
              - Attribute1
              - Attribute2
            ProjectionType: INCLUDE
          ProvisionedThroughput:
            ReadCapacityUnits: '1'
            WriteCapacityUnits: '1'

補足しますが、AttributeDefinitionsで定義されている他の属性については以下の通りキーとなっています。

属性 説明
KeyId 31行目でテーブルのキーに指定されている
SortId 33行目でテーブルのソートキー(RANGE)に指定されている
GsiKey 46行目でインデックス(GSI)のキーに指定されている
Attribute1 50行目でインデックス(GSI)の射影属性に指定されているが、キーではない

以下のyamlの通りAttributeDefinitionsからAttribute1を削除したらエラーは解消しました。

#AWS CloudFormation スタックを作成のリージョンで作成されます。対象リージョンでスタックを展開してください。
#===============================================================================
# CloudFormation Template
#===============================================================================
AWSTemplateFormatVersion: 2010-09-09
Description: DynamoDB Template

#===============================================================================
# Resources
#===============================================================================
Resources:

#============================================================
#テーブル作成
#============================================================
  tableName:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: "TableName"
      AttributeDefinitions:
       - AttributeName: KeyId
         AttributeType: S
       - AttributeName: SortId
         AttributeType: S
       - AttributeName: GsiKey
         AttributeType: S

      KeySchema:
       - AttributeName: KeyId
         KeyType: HASH
       - AttributeName: SortId
         KeyType: RANGE
      ProvisionedThroughput:
        ReadCapacityUnits: '1'
        WriteCapacityUnits: '1'
      PointInTimeRecoverySpecification: 
       PointInTimeRecoveryEnabled: false
      #============================================================
      #インデックス(GSI)の作成
      #============================================================
      GlobalSecondaryIndexes:
        - IndexName: Gsi1
          KeySchema:
            - AttributeName: GsiKey
              KeyType: HASH
          Projection:
            NonKeyAttributes:
              - Attribute1
              - Attribute2
            ProjectionType: INCLUDE
          ProvisionedThroughput:
            ReadCapacityUnits: '1'
            WriteCapacityUnits: '1'
CloudFormationスタック更新のエラー解消

PR
当ブログはWordPressテーマSWELLを使用しています。非常に使いやすく、簡単にプロのようなデザインを使えるのでお勧めです!!

SWELL – シンプル美と機能性両立を両立させた、圧巻のWordPressテーマ

ランキング

ランキングに参加しています。クリックして応援いただけると嬉しいです。
にほんブログ村 IT技術ブログ クラウドコンピューティングへ
にほんブログ村
AWSランキング
AWSランキング

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次