使用AWS CloudFormation创建DynamoDB表时触发"AttributeName cannot be empty"错误的问题排查
解决CloudFormation DynamoDB表的"Property AttributeName cannot be empty"错误
你的问题根源在于TimeToLiveSpecification的配置。虽然你设置了Enabled: false,但只要在CloudFormation模板中声明了TimeToLiveSpecification这个属性块,AWS就会要求你必须提供AttributeName字段——哪怕你实际上不需要启用TTL功能。
为什么会报错?
CloudFormation对AWS::DynamoDB::Table的TimeToLiveSpecification属性有严格的校验规则:
- 当启用TTL(
Enabled: true)时,必须指定AttributeName来标识存储过期时间的字段; - 即使禁用TTL(
Enabled: false),只要在模板中定义了TimeToLiveSpecification对象,就仍然需要提供AttributeName(哪怕这个字段不会被实际使用); - 更简单的处理方式是:如果不需要TTL功能,直接完全移除
TimeToLiveSpecification属性块,这样就不会触发任何校验。
修复后的模板示例
YAML版本(修正后)
DynamoDBTable: Type: "AWS::DynamoDB::Table" Properties: AttributeDefinitions: - AttributeName: "eventName" AttributeType: "S" TableName: "BlockCursorTable" Tags: - Key: "project" Value: "flow-event-monitor" KeySchema: - AttributeName: "eventName" KeyType: "HASH" ProvisionedThroughput: ReadCapacityUnits: 1 WriteCapacityUnits: 1 # 移除不需要的TimeToLiveSpecification,或者添加AttributeName(如下注释示例) # TimeToLiveSpecification: # Enabled: false # AttributeName: "ttl" # 指定任意合法字段名即可 DynamoDBTable2: Type: "AWS::DynamoDB::Table" Properties: AttributeDefinitions: - AttributeName: "listingResourceID" AttributeType: "N" - AttributeName: "staticKey" AttributeType: "N" - AttributeName: "timestamp" AttributeType: "S" TableName: "ListingTable" Tags: - Key: "project" Value: "flow-event-monitor" KeySchema: - AttributeName: "listingResourceID" KeyType: "HASH" ProvisionedThroughput: ReadCapacityUnits: 1 WriteCapacityUnits: 1 GlobalSecondaryIndexes: - IndexName: "staticKey-timestamp-index" KeySchema: - AttributeName: "staticKey" KeyType: "HASH" - AttributeName: "timestamp" KeyType: "RANGE" Projection: ProjectionType: "ALL" ProvisionedThroughput: ReadCapacityUnits: 1 WriteCapacityUnits: 1 # 同样移除该块,或者添加AttributeName
JSON版本(修正后)
{ "DynamoDBTable": { "Type": "AWS::DynamoDB::Table", "Properties": { "AttributeDefinitions": [ { "AttributeName": "eventName", "AttributeType": "S" } ], "TableName": "BlockCursorTable", "Tags": [ { "Key": "project", "Value": "flow-event-monitor" } ], "KeySchema": [ { "AttributeName": "eventName", "KeyType": "HASH" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 } // 移除TimeToLiveSpecification,或者添加如下内容: // "TimeToLiveSpecification": { // "Enabled": false, // "AttributeName": "ttl" // } } }, "DynamoDBTable2": { "Type": "AWS::DynamoDB::Table", "Properties": { "AttributeDefinitions": [ { "AttributeName": "listingResourceID", "AttributeType": "N" }, { "AttributeName": "staticKey", "AttributeType": "N" }, { "AttributeName": "timestamp", "AttributeType": "S" } ], "TableName": "ListingTable", "Tags": [ { "Key": "project", "Value": "flow-event-monitor" } ], "KeySchema": [ { "AttributeName": "listingResourceID", "KeyType": "HASH" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 }, "GlobalSecondaryIndexes": [ { "IndexName": "staticKey-timestamp-index", "KeySchema": [ { "AttributeName": "staticKey", "KeyType": "HASH" }, { "AttributeName": "timestamp", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "ProvisionedThroughput": { "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 } } ] // 移除TimeToLiveSpecification,或者添加AttributeName } } }
总结
- 最简洁的修复方式是删除所有
TimeToLiveSpecification属性块,因为你已明确不需要启用TTL; - 若出于某种原因必须保留该属性块,只需添加一个合法的
AttributeName值(比如"ttl")即可,即使Enabled设为false。
内容的提问来源于stack exchange,提问作者pho_pho
相关产品推荐
相关产品推荐

