発生したエラー
E pynamodb.exceptions.TableError: Failed to create table: An error occurred (ValidationException) on request (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) on table (some_data) when calling the CreateTable operation: Table read and write throughput must be at least 1, and cannot be null
.venv/lib/python3.8/site-packages/pynamodb/connection/base.py:651: TableError
Table read and write throughput must be at least 1, and cannot be null
がエラーの原因っぽいことはすぐにわかった。
けれど、Table read and write throughput
をどうやって設定するのかが一見してわからない。
この対処法を本記事に記載する。
環境
ソースコード
from pynamodb.models import Model
class SomeModel(Model):
class Meta:
table_name = "some_data"
region = "us-east-1"
some_id: UnicodeAttribute = UnicodeAttribute(hash_key=True)
user_id: UnicodeAttribute = UnicodeAttribute()
def create_some_table():
model = SomeModel
model_meta_class = getattr(SomeModel, "Meta")
setattr(model_meta_class, "host", "http://localhost:8000")
setattr(model_meta_class, "region", "ap-northeast-1")
setattr(model_meta_class, "table_name", "some_data")
setattr(model_meta_class, "aws_access_key_id", "dummy")
setattr(model_meta_class, "aws_secret_access_key", "dummy")
model.create_table(wait=True)
create_some_table()
このソースコードのままcreate_some_tableを実行すると、以下のようなエラーが出る
E pynamodb.exceptions.TableError: Failed to create table: An error occurred (ValidationException) on request (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) on table (some_data) when calling the CreateTable operation: Table read and write throughput must be at least 1, and cannot be null
.venv/lib/python3.8/site-packages/pynamodb/connection/base.py:651: TableError
対処法
以下のように、setattr(model_meta_class, "billing_mode", "PAY_PER_REQUEST")
を追加したら直った。
from pynamodb.models import Model
class SomeModel(Model):
class Meta:
table_name = "some_data"
region = "us-east-1"
some_id: UnicodeAttribute = UnicodeAttribute(hash_key=True)
user_id: UnicodeAttribute = UnicodeAttribute()
def create_some_table():
model = SomeModel
model_meta_class = getattr(SomeModel, "Meta")
setattr(model_meta_class, "host", "http://localhost:8000")
setattr(model_meta_class, "billing_mode", "PAY_PER_REQUEST") # <- 追加
setattr(model_meta_class, "region", "ap-northeast-1")
setattr(model_meta_class, "table_name", "some_data")
setattr(model_meta_class, "aws_access_key_id", "dummy")
setattr(model_meta_class, "aws_secret_access_key", "dummy")
model.create_table(wait=True)
create_some_table()
これでOK。
Table read and write throughput
というのは、billing_mode
で指定できるものだったようだ。
なら、そう書いて欲しいぽよ...
参考資料