はじめに
GSIの更新がしたいだけだったのですが、色々と時間がかかってしまったのでまとめます。
状況
- テーブルに複数のGSIを追加している
- 検索を実行すると以下のエラーが発生
- [ERROR] ClientError: An error occurred (ValidationException) when calling the Query operation: One or more parameter values were invalid: Global secondary index hoge1 does not project [sample_attr]
これはGSIの「hoge1」に対してProjectionに属性の「sample_attr」を追加していなかったことが原因でした
やりたいこと
- GSIを使用して検索した際にsample_attr属性が結果に含まれているようにしたい
- GSIのhoge2に対しても同様
更新前のyml
QiitaTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Delete
Properties:
TableName: ${self:custom.dbNames.${self:provider.stage}.qiita, self:custom.dbNames.default.qiita}
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: sub_id
AttributeType: S
- AttributeName: key
AttributeType: N
- AttributeName: huga_id
AttributeType: S
- AttributeName: created_at
AttributeType: N
KeySchema:
- AttributeName: id
KeyType: HASH
- AttributeName: sub_id
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: ${self:custom.dbNames.${self:provider.stage}.qiita, self:custom.dbNames.default.qiita}_hoge1
KeySchema:
- AttributeName: key
KeyType: HASH
- AttributeName: created_at
KeyType: RANGE
Projection:
NonKeyAttributes:
- created_at
- huga_id
ProjectionType: INCLUDE
- IndexName: ${self:custom.dbNames.${self:provider.stage}.qiita, self:custom.dbNames.default.chat}_hoge2
KeySchema:
- AttributeName: huga_id
KeyType: HASH
- AttributeName: created_at
KeyType: RANGE
Projection:
NonKeyAttributes:
- sample_attr
- hogehoge
ProjectionType: INCLUDE
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
やったこと①
- それぞれのGSIのProjectionにsample_attrを追加
遭遇したエラー①
- "Cannot update GSI's properties other than Provisioned Throughput and Contributor Insights Specification. You can create a new GSI with a different name."
同じ名前のGSIで更新しないでね、と怒られました
やったこと②
- 既存GSIの設定内容をコメントアウトした状態で更新を行った後に、コメントアウトを戻して設定内容も変更してから更新
遭遇したエラー②
- Resource handler returned message: "Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions"
これはAttributeDefinitionsで設定している属性の数がKeySchemaと一致しないよというエラーでした
やったこと③
- GSIで設定していたPK/SKの属性をAttributeDefinitionsからもコメントアウトして更新
遭遇したエラー③
- Resource handler returned message: "Cannot perform more than one GSI creation or deletion in a single update"
GSIの更新などは1つずつやってね、と怒られました
やったこと④
GSIのhoge1の設定内容とAttributeDefinitionsからhoge1のPKだけコメントアウトして更新
(SKはhoge2のGSIでも利用するためコメントアウトしない)
これで問題なく更新できたのでコメントアウトを戻します
そして今回やりたかったProjectionにsample_attrを追加して更新
これで問題なく通りました。同様にしてhoge2に対しても更新を行い無事完了です
おわりに
GSIのお手入れにだいぶ時間をかけてしまったので教訓にします