LoginSignup
13
3

More than 3 years have passed since last update.

DynamoDB Localでテーブル作成できなくて少しハマった話

Last updated at Posted at 2019-11-30

経緯

個人的に、サーバレスでアプリ作りたくて、AWS SAMとDynamoDB Localで試作してた。

以下のページが参考になった。
aws-sam-cliでLambda,DynamoDBのサーバーレスアプリケーション開発に入門してみる

エラー内容

テーブル定義をjsonで作成して、aws cliでコマンド実行したらエラーで作れない。

$ aws dynamodb create-table --cli-input-json file://table/test-table.json --endpoint-url http://127.0.0.1:8000 --region ap-northeast-1

An error occurred (ValidationException) when calling the CreateTable operation: No provisioned throughput specified for the table

json定義はこんな感じ。

{
    "AttributeDefinitions": [
        {
            "AttributeName": "name",
            "AttributeType": "S"
        },
        {
            "AttributeName": "time",
            "AttributeType": "S"
        }
    ],
    "TableName": "test-table",
    "KeySchema": [
        {
            "AttributeName": "name",
            "KeyType": "HASH"
        },
        {
            "AttributeName": "time",
            "KeyType": "RANGE"
        }
    ]
}

結論

billing-modeオプションでPAY_PER_REQUESTを指定すれば作れた。

$ aws dynamodb create-table --cli-input-json file://table/test-table.json --endpoint-url http://127.0.0.1:8000 --region ap-northeast-1 --billing-mode PAY_PER_REQUEST

$ aws dynamodb list-tables --endpoint-url http://127.0.0.1:8000  --region ap-northeast-1
{
    "TableNames": [
        "test-table"
    ]
}

あとは余談

紆余曲折

  • エラーMSGでググったけど、それっぽい解が出てこない。

  • AWS CLIのドキュメントで、必須項目は「attribute」「table-name」「key-schema」で足りてるっぽい。

  • jsonがどこか間違ってるのかなと思って、以下のコマンドでテンプレート生成して作り直したけど一緒。

  aws dynamodb create-table --generate-cli-skeleton > table-template.json
  • エラーをもう一回読んで、google翻訳してみる。
    「No provisioned throughput specified for the table」
    「テーブルにプロビジョニングされたスループットが指定されていません」
    なるほど。完全に?理解した!

結局

改めてAWS CLIのcreate-tableのドキュメントを見てみると

--provisioned-throughput

Represents the provisioned throughput settings for a specified table or index. The settings can be modified using the UpdateTable operation.
If you set BillingMode as PROVISIONED , you must specify this property. If you set BillingMode as PAY_PER_REQUEST , you cannot specify this property.
For current minimum and maximum provisioned throughput values, see Limits in the Amazon DynamoDB Developer Guide .

BillingModeをPROVISIONEDに設定した場合、このプロパティを指定する必要があります。BillingModeをPAY_PER_REQUESTに設定した場合、このプロパティは指定できません。

--billing-mode

Controls how you are charged for read and write throughput and how you manage capacity. This setting can be changed later.

  • PROVISIONED - We recommend using PROVISIONED for predictable workloads. PROVISIONED sets the billing mode to Provisioned Mode .
  • PAY_PER_REQUEST - We recommend using PAY_PER_REQUEST for unpredictable workloads. PAY_PER_REQUEST sets the billing mode to On-Demand Mode . Possible values:
  • PROVISIONED
  • PAY_PER_REQUEST
  • PROVISIONED-予測可能なワークロードにはPROVISIONEDを使用することをお勧めします。PROVISIONEDは、請求モードを Provisioned Modeに設定します
  • PAY_PER_REQUEST-予測不能なワークロードにはPAY_PER_REQUESTを使用することをお勧めします。PAY_PER_REQUESTは、請求モードをオンデマンドモードに設定します

これデフォルトがプロビジョンドで、オンデマンドにするには指定しないといけない?(書いてないけど)

で解決した。

所感?

  • エラーもっとよく読めばすぐ分かったかも。。
  • デフォルトがPROVISIONEDなのはAPIのドキュメント見ても書いてなかった。
    ※オンデマンドができたの後になってからってのもあるかもしれないけど、
    「ProvisionedThroughput」は「Required: No」ってなってるからメンテされてないって訳ではなさそう。
  • CloudFormationのドキュメントには書いてあった。
    ※話がそれるけど、CloudFormationのドキュメントは
     英語版は「Default: false」で、日本語版は「デフォルト: true」ってなってる事あるから注意!
     (マジでやめてほしい。気づいたらサポートに言ってるけど。)

  • 実際のAWS環境で試してみるために、endpoint-urlオプション外して実行するとこんなエラーが……

  $ aws dynamodb create-table --cli-input-json file://table/test-table.json --region ap-northeast-1 

  An error occurred (ValidationException) when calling the CreateTable operation: One or more parameter values were invalid: ReadCapacityUnits and WriteCapacityUnits must both be specified when BillingMode is PROVISIONED

BillingModeがPROVISIONEDだとCapacityUnitsを指定しないといけないよと。
なんでLocalは出ないの。。(そしてやっぱりデフォルトはPROVISIONEDだ)

  • でも実際のAWS環境であっても、一瞬でテーブル作れたのは感動した。すげー
  $ date ; \
  > aws dynamodb create-table --cli-input-json file://table/test-table.json --region ap-northeast-1 --billing-mode PAY_PER_REQUEST 1>/dev/null ; \
  > aws dynamodb list-tables  --region ap-northeast-1 ; \
  > date
  2019年 11月 30日 土曜日 16:31:24 JST
  {
      "TableNames": [
          "test-table"
      ]
  }
  2019年 11月 30日 土曜日 16:31:35 JST

おわり

13
3
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
13
3