LoginSignup
0
0

More than 1 year has passed since last update.

Serverless Framework で DynamoDB のスキーマを変更した時に、変更のあったDBだけ更新する方法

Last updated at Posted at 2022-06-08

そもそも

Serverless Framework + DynamoDB で、データベースを使う API を構築している時に、DynamoDB のスキーマを変更した時に、

いつものように sls deploy --stage=v1 を実行すると下のようなエラーが出ました。

 Serverless Error ----------------------------------------
 
  An error occurred: userTable - CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename your-api-user-v1 and update the stack again..

やりたい事

ただ、複数のテーブルを使っていて、スキーマを変更したテーブルだけ更新したかったのですが、エラーメッセージを読むと、カスタムネームリソースが置き換えを必要とする場合は更新できないと書いてありました。

AWS の回答を見ていても、リソースネームを置き換えろと書いてありました。
https://aws.amazon.com/jp/premiumsupport/knowledge-center/cloudformation-custom-name/

やったこと

なので、テーブルネームを、下のように変更して修正しました。

Before
AWS_DYNAMODB_USER_TABLE_NAME: your-api-user-${opt:stage, self:provider.stage}

After
AWS_DYNAMODB_USER_TABLE_NAME: your-api-user-${opt:stage, self:provider.stage}-deploy1

テーブルのデータはリセットされるので、データを再インポートしたい場合は、CSV で出力しておいて、再度インポートスクリプトを組む必要があります。

追記

上のやり方では、DynamoDB のリソース名が更新ごとに変わってしまいますが、一度別名に変更して、deploy 、その後再度最初の名前に変更して deploy する事でも解決できます。

こっちの方が良いですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0