Serverless配置DynamoDB报Invalid KeySchema错误,哪里存在配置问题?
问题原因
AWS DynamoDB 资源部署的底层规则要求:所有作为表主键、全局二级索引/本地二级索引键使用的属性,必须提前在 AttributeDefinitions 配置块中声明属性名和对应的数据类型。
你当前的配置仅定义了表主键用到的 postId 属性,但是全局二级索引 scholarshipCategoryIndex 的哈希键 category、范围键 deadline 均未在 AttributeDefinitions 中声明,因此触发了该报错。
修正后的配置
resources: Resources: ScholarshipsTable: Type: AWS::DynamoDB::Table Properties: TableName: purrfectScholarshipsTable AttributeDefinitions: - AttributeName: postId AttributeType: S # 新增两个索引键的属性定义,类型可根据实际业务调整 - AttributeName: category AttributeType: S - AttributeName: deadline AttributeType: S # 若存储数字时间戳可改为 N KeySchema: - AttributeName: postId KeyType: HASH GlobalSecondaryIndexes: - IndexName: scholarshipCategoryIndex KeySchema: - AttributeName: category KeyType: HASH - AttributeName: deadline KeyType: RANGE Projection: ProjectionType: "ALL" BillingMode: PAY_PER_REQUEST
注意事项
- 声明的属性类型需要和你实际写入数据库时的属性类型保持一致,否则后续写入数据时会触发类型不匹配报错
- 仅作为主键/索引键的属性需要在
AttributeDefinitions中提前定义,普通业务字段无需声明,DynamoDB 本身是无表结构设计,仅对键有前置声明要求
内容的提问来源于stack exchange,提问作者ZarNiHein
相关产品推荐
相关产品推荐

