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

如何修复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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 20:40:34