YAML Schema报错:误将AttributeName识别为AttributeType如何修复?
修复AWS::DynamoDB::Table YAML配置的Schema校验错误
这个报错的核心原因是YAML Schema校验逻辑出现混淆——错误地把AttributeType的单字符限制(只能是S/N/B)套用到了AttributeName上,导致你用的多字符属性名(比如todoId)触发了长度超限提示。下面是几种可行的修复方案:
方案1:修正Schema校验规则(编辑器/框架层面)
- 如果使用Serverless Framework,直接升级到最新稳定版本,旧版本内置的CloudFormation Schema存在这个混淆bug,新版本已修复。
- 如果是VS Code等IDE的YAML插件(比如Red Hat YAML)导致的错误,手动指定正确的AWS官方Schema:
- 在项目根目录创建
.yaml-schema文件 - 写入以下内容,关联DynamoDB Table的官方Schema:
schemas: - https://raw.githubusercontent.com/aws-cloudformation/aws-cloudformation-resource-schemas/main/src/main/resources/schema/provider.aws.com/DynamoDB/Table/2019-11-21.schema.json - 重启IDE让配置生效
- 在项目根目录创建
方案2:精简符合DynamoDB规范的AttributeDefinitions
DynamoDB只要求作为主键、索引键的属性必须在AttributeDefinitions里声明,你的配置里dueDate没有被用作任何键(主键/索引键),完全可以从列表中移除,既符合官方规范,也能减少Schema校验的触发点。修改后的完整配置如下:
TodosTable: Type: AWS::DynamoDB::Table Properties: TableName: Todos-${self:provider.stage} AttributeDefinitions: - AttributeName: todoId AttributeType: S - AttributeName: createdAt AttributeType: S - AttributeName: name AttributeType: S KeySchema: - AttributeName: todoId KeyType: HASH - AttributeName: createdAt KeyType: RANGE BillingMode: PAY_PER_REQUEST StreamSpecification: StreamViewType: NEW_IMAGE LocalSecondaryIndexes: - IndexName: todoNameIndex KeySchema: - AttributeName: todoId KeyType: HASH - AttributeName: name KeyType: RANGE Projection: ProjectionType: ALL
内容的提问来源于stack exchange,提问作者SMTP King
相关产品推荐
相关产品推荐

