无法通过JSON在Localstack创建DynamoDB表的问题
问题:Localstack中使用AWS CLI通过JSON文件创建DynamoDB表失败?
我的DynamoDB JSON配置文件内容:
{"AttributeDefinitions": [{"AttributeName": "pk","AttributeType": "S"},{"AttributeName": "sk","AttributeType": "S"}],"TableName": "MYTABLE","KeySchema": [{"AttributeName": "pk","KeyType": "HASH"},{"AttributeName": "sk","KeyType": "RANGE"}],"GlobalSecondaryIndexes": [{"IndexName": "GSIReverseIndex","KeySchema": [{"AttributeName": "pk","KeyType": "RANGE"},{"AttributeName": "sk","KeyType": "RANGE"}],"Projection": {"ProjectionType": "ALL","NonKeyAttributes": [""]},"ProvisionedThroughput": {"ReadCapacityUnits": 1,"WriteCapacityUnits": 1}}],"BillingMode": "PROVISIONED","ProvisionedThroughput": {"ReadCapacityUnits": 1,"WriteCapacityUnits": 1},"StreamSpecification": {"StreamEnabled": true,"StreamViewType": "NEW_AND_OLD_IMAGES"},"TableClass": "STANDARD"}
执行的创建表命令:
aws dynamodb create-table --endpoint-url http://localhost:4566 --region us-east-1 --cli-input-file file:///MY_TABLE.json
遇到的错误:
aws: error: the following arguments are required: --attribute-definitions, --table-name, --key-schema
问题原因及解决办法
- AWS CLI参数版本不兼容:AWS CLI v2已经废弃了
--cli-input-file参数,改用--cli-input-json来指定包含配置参数的JSON文件。你当前使用的旧参数导致CLI无法解析JSON中的配置,从而错误提示缺少必填参数。
修正后的命令:
aws dynamodb create-table --endpoint-url http://localhost:4566 --region us-east-1 --cli-input-json file:///MY_TABLE.json
检查JSON文件路径:确认
file:///MY_TABLE.json是正确的文件路径。如果是当前目录下的文件,建议使用相对路径写法file://./MY_TABLE.json;绝对路径要确保文件确实存在于对应位置。修复GSI的KeySchema配置:你的JSON中GSI的配置存在逻辑错误——
GSIReverseIndex的两个键类型都是RANGE,而DynamoDB要求所有索引(包括GSI)必须包含一个HASH类型的主键。后续创建表时会因此报错,需要修改为:
"GlobalSecondaryIndexes": [ { "IndexName": "GSIReverseIndex", "KeySchema": [ {"AttributeName": "sk","KeyType": "HASH"}, {"AttributeName": "pk","KeyType": "RANGE"} ], "Projection": {"ProjectionType": "ALL"}, "ProvisionedThroughput": {"ReadCapacityUnits": 1,"WriteCapacityUnits": 1} } ]
另外注意Projection中NonKeyAttributes设为空数组没有意义,当ProjectionType为ALL时可以直接省略这个字段。
内容的提问来源于stack exchange,提问作者Shadowman
相关产品推荐
相关产品推荐

