You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过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

问题原因及解决办法

  1. 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
  1. 检查JSON文件路径:确认file:///MY_TABLE.json是正确的文件路径。如果是当前目录下的文件,建议使用相对路径写法file://./MY_TABLE.json;绝对路径要确保文件确实存在于对应位置。

  2. 修复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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 17:45:39