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

Terraform报错:DynamoDB全局二级索引缺必填range_key致流水线失败

解决Terraform DynamoDB模块range_key必填错误

问题核心

你的子模块modules/dynamoDBItem/variables.tf中,table_global_secondary_index变量的元素结构被强制要求包含range_key字段,但DynamoDB全局二级索引(GSI)允许仅配置分区键、不设置排序键,这就导致传参时因缺少该字段触发报错。

解决方法

方法1:修改子模块变量定义(推荐)

打开modules/dynamoDBItem/variables.tf,找到第85行附近的table_global_secondary_index变量定义,将range_key字段标记为可选(根据Terraform版本选择方式):

原定义示例:

variable "table_global_secondary_index" {
  type = list(object({
    name               = string
    partition_key      = string
    range_key          = string  # 此处为必填
    projection_type    = string
    read_capacity      = number
    write_capacity     = number
  }))
  default = []
}

修改为(Terraform 1.3+支持):

variable "table_global_secondary_index" {
  type = list(object({
    name               = string
    partition_key      = string
    range_key          = optional(string)  # 标记为可选字段
    projection_type    = string
    read_capacity      = number
    write_capacity     = number
  }))
  default = []
}

若使用旧版Terraform,可改为:

range_key          = string = null  # 通过默认值null设为可选

修改后模块即可接受不包含range_key的GSI配置,符合DynamoDB的实际规则。

方法2:调用模块时显式传null

如果暂时无法修改子模块,可在main.tf中给不需要排序键的GSI显式设置range_key = null:

table_global_secondary_index = [
  {
    name               = "your-gsi-name"
    partition_key      = "your-partition-col"
    range_key          = null  # 显式传入null满足变量类型要求
    projection_type    = "ALL"
    read_capacity      = 5
    write_capacity     = 5
  }
]

Terraform会忽略null值,最终创建仅含分区键的GSI。

内容的提问来源于stack exchange,提问作者Varun M S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 22:25:10