Localstack内でDynamoを動かしてエラーで困ったこと①

Localstack内でDynamoを動かしてエラーで困ったこと①

Twitter LINEで送る Facebook はてなブログ

Dynamo DBの検証時にエラーで困ったことTips

テーブル作成時の属性のエラー

エラーメッセージ

An error occurred (ValidationException) when calling the CreateTable operation: Hash Key not specified in Attribute Definitions.  Type unknown.

エラーのコマンド

aws dynamodb create-table --table-name test --attribute-definitions AttributeName=id,AttributeType=S AttributeName=name,AttributeType=S --key-schema AttributeName=id,KeyType=HASH AttributeName=names,KeyType=RANGE --billing-mode PAY_PER_REQUEST --profile=localstack --endpoint-url=http://localhost:4566

修正後のコマンド

aws dynamodb create-table --table-name test --attribute-definitions AttributeName=id,AttributeType=N AttributeName=name,AttributeType=N --key-schema AttributeName=id,KeyType=HASH AttributeName=name,KeyType=RANGE --billing-mode PAY_PER_REQUEST --profile=localstack --endpoint-url=http://localhost:4566

原因

  1. attribute-definitionsとkey-schemaの名称が一致していないことが原因

テーブル作成時にHASHを複数個定義でエラー

エラーメッセージ

An error occurred (ValidationException) when calling the CreateTable operation: Too many hash keys specified.  All Dynamo DB tables must have exactly one hash key

エラーのコマンド

aws dynamodb create-table --table-name test --attribute-definitions AttributeName=id,AttributeType=S AttributeName=name,AttributeType=S --key-schema AttributeName=id,KeyType=HASH AttributeName=names,KeyType=HASH --billing-mode PAY_PER_REQUEST --profile=localstack --endpoint-url=http://localhost:4566

修正後のコマンド

aws dynamodb create-table --table-name test --attribute-definitions AttributeName=id,AttributeType=N AttributeName=name,AttributeType=N --key-schema AttributeName=id,KeyType=HASH AttributeName=name,KeyType=RANGE --billing-mode PAY_PER_REQUEST --profile=localstack --endpoint-url=http://localhost:4566

原因

  1. 全体でHASHは1個のみなので要注意

データINSERT時のエラー

エラーメッセージ

An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Type mismatch for key

エラーのコマンド

aws dynamodb create-table --table-name test --attribute-definitions AttributeName=id,AttributeType=S AttributeName=name,AttributeType=S --key-schema AttributeName=id,KeyType=HASH AttributeName=names,KeyType=RANGE --billing-mode PAY_PER_REQUEST --profile=localstack --endpoint-url=http://localhost:4566

修正後のコマンド

aws dynamodb put-item --table-name test --item '{"id": {"S": "1"}, "name": {"S": "user1"}, "title": {"S": "title1"}, "contents": {"S": "contents1"}, "status": {"N": "0"}}' --profile=localstack --endpoint-url=http://localhost:4566

原因

  1. 送るデータのキー「N」と「S」を間違って入力していた定義を要確認する必要がある