如何修复CloudFormation中DynamoDB的GSI范围键未指定错误?
问题分析与解决方案
你的错误根源很明确:你新增的customerId-index全局二级索引使用siteId作为RANGE键,但这个属性完全没在AttributeDefinitions里定义,DynamoDB要求所有用于主键(表主键、LSI/GSI主键)的属性都必须在全局的AttributeDefinitions中声明类型。
修正步骤
- 在
AttributeDefinitions中添加siteId的属性定义,根据业务场景指定类型(示例用字符串类型S) - 确保所有索引的键属性都已在全局属性定义中声明
修正后的完整模板
ComponentsTable: Type: AWS::DynamoDB::Table Properties: TableName: ${self:custom.base}-components PointInTimeRecoverySpecification: PointInTimeRecoveryEnabled: True StreamSpecification: StreamViewType: NEW_IMAGE BillingMode: PAY_PER_REQUEST AttributeDefinitions: - AttributeName: assetId AttributeType: S - AttributeName: componentId AttributeType: S - AttributeName: componentType AttributeType: S - AttributeName: customerId AttributeType: S - AttributeName: siteId # 新增siteId的属性定义 AttributeType: S KeySchema: - AttributeName: componentId KeyType: HASH - AttributeName: assetId KeyType: RANGE LocalSecondaryIndexes: - IndexName: componentType-index KeySchema: - AttributeName: componentId KeyType: HASH - AttributeName: componentType KeyType: RANGE Projection: ProjectionType: ALL GlobalSecondaryIndexes: - IndexName: assetId-index KeySchema: - AttributeName: assetId KeyType: HASH - AttributeName: componentId KeyType: RANGE Projection: ProjectionType: ALL - IndexName: compoentType-gsi KeySchema: - AttributeName: componentType KeyType: HASH - AttributeName: componentId KeyType: RANGE Projection: ProjectionType: ALL - IndexName: customerId-index KeySchema: - AttributeName: customerId KeyType: HASH - AttributeName: siteId KeyType: RANGE Projection: ProjectionType: ALL
内容的提问来源于stack exchange,提问作者Onur
相关产品推荐
相关产品推荐

