Serverless配置DynamoDB全局二级索引时遇不支持ProjectionType属性报错
DynamoDB GSI配置问题排查与修正
现存配置问题清单
- 触发当前部署报错的直接原因:属性层级嵌套错误。
ProjectionType是Projection配置块的内部字段,不属于全局二级索引的顶层属性。该字段的合法取值为ALL | KEYS_ONLY | INCLUDE,你设置的取值本身符合规范,但当前将ProjectionType与Projection平级放置在GSI配置下,CloudFormation解析时无法在GSI的顶层属性中找到对应合法定义,因此抛出"Encountered unsupported property ProjectionType"的错误。 - 未暴露的结构合规性问题:表主键配置不符合DynamoDB规则。DynamoDB表的主键最多支持2个字段——1个必填的HASH分区键、1个可选的RANGE排序键,不支持同时配置3个HASH类型的主键字段。你当前为ConnectionsTable设置的KeySchema将
connectionId、sessionId、clientType全部标记为HASH键,即使解决了当前的Projection报错,这部分配置也会触发主键结构不合法的部署失败。 - 需保持的配置一致性要求:所有在表主键、GSI键中引用的属性,必须在
AttributeDefinitions块中提前声明对应属性类型。你当前的属性声明覆盖了后续用到的所有键字段,只要和修正后的键Schema保持匹配,不会出现引用未定义属性的报错。
修正后的可部署配置
按照DynamoDB的CloudFormation语法规范调整后的完整配置如下:
resources: Resources: ConnectionsTable: Type: AWS::DynamoDB::Table Properties: AttributeDefinitions: - AttributeName: connectionId AttributeType: S - AttributeName: sessionId AttributeType: S - AttributeName: clientType AttributeType: S BillingMode: PAY_PER_REQUEST # 修正:表主键仅保留业务必需的HASH键,符合DynamoDB主键上限要求 KeySchema: - AttributeName: connectionId KeyType: HASH GlobalSecondaryIndexes: - IndexName: SessionIdIndex KeySchema: - AttributeName: sessionId KeyType: HASH # 修正:将ProjectionType移入Projection配置块内部,与NonKeyAttributes平级 Projection: ProjectionType: INCLUDE NonKeyAttributes: - connectionId - clientType SSESpecification: SSEEnabled: true TimeToLiveSpecification: AttributeName: ttl Enabled: true SessionHistoryTable: Type: AWS::DynamoDB::Table Properties: AttributeDefinitions: - AttributeName: sessionId AttributeType: S BillingMode: PAY_PER_REQUEST KeySchema: - AttributeName: sessionId KeyType: HASH SSESpecification: SSEEnabled: true TimeToLiveSpecification: AttributeName: ttl Enabled: true SessionsTable: Type: AWS::DynamoDB::Table Properties: AttributeDefinitions: - AttributeName: sessionId AttributeType: S BillingMode: PAY_PER_REQUEST KeySchema: - AttributeName: sessionId KeyType: HASH SSESpecification: SSEEnabled: true TimeToLiveSpecification: AttributeName: ttl Enabled: true
补充说明:当表的BillingMode设为
PAY_PER_REQUEST时,全局二级索引无需单独配置读写吞吐量参数,你当前未设置这部分字段的配置是正确的。
内容的提问来源于stack exchange,提问作者Kieran Crown
相关产品推荐
相关产品推荐

