部署DynamoDB全局表遇CloudFormation报错:预期JSONObject,实际为JSONArray
解决CloudFormation DynamoDB GlobalTable条件化部署多区域副本的问题
你的模板存在两个核心问题:一是!If的返回值类型错误,二是多处缩进和拼写语法错误,导致CloudFormation解析失败。
问题分析
!If返回值类型错误:Replicas是一个对象数组,每个元素必须是单个Replica配置对象。你在!If的真分支里返回了嵌套数组(- Region: ...),导致CloudFormation收到数组而非预期的对象,触发expected type: JSONObject, found: JSONArray错误。- 语法错误:
AttributeType、KeyType缩进层级错误,SESpecification拼写错误,KeyType写了无效值HASHS(正确值是HASH)。
修正后的完整模板
Conditions: HasMultipleRegions: !Not [!Equals [!Ref Environment, "development"]] Resources: MyGlobalTable: Type: 'AWS::DynamoDB::GlobalTable' Properties: TableName: MyGlobalTable BillingMode: !Ref BillingMode StreamSpecification: StreamViewType: !Ref StreamViewType AttributeDefinitions: - AttributeName: attribute_a AttributeType: N KeySchema: - AttributeName: attribute_a KeyType: HASH SSESpecification: SSEEnabled: true SSEType: KMS Replicas: - Region: !Ref Region1 PointInTimeRecoverySpecification: PointInTimeRecoveryEnabled: true SSESpecification: KMSMasterKeyId: !Ref KmsKeyIdRegion1 - !If - HasMultipleRegions - Region: !Ref Region2 PointInTimeRecoverySpecification: PointInTimeRecoveryEnabled: true SSESpecification: KMSMasterKeyId: !Ref KmsKeyIdRegion2 - !Ref "AWS::NoValue"
关键修正说明
!If逻辑调整:移除真分支的外层数组包裹,直接返回单个Replica配置对象。当条件为真时,Replicas数组会包含Region1和Region2两个副本;当条件为假时,!Ref AWS::NoValue会让CloudFormation自动忽略这个数组元素,仅保留Region1的副本。- 语法修正:调整
AttributeType、KeyType的缩进层级,修正SSESpecification拼写错误,将KeyType改为有效值HASH。
内容的提问来源于stack exchange,提问作者TheRealJoeSchmoe
相关产品推荐
相关产品推荐

