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

基于Terraform创建DynamoDB表:从变量生成属性

没问题,我来帮你搭建一个完全符合需求的Terraform DynamoDB表模块——可以通过变量(从.tfvars或默认值读取)动态定义表的属性,不用在资源文件里硬编码属性名称。下面是完整的实现方案:

1. 模块文件结构

咱们先把模块的文件结构理清楚,这样更清晰:

modules/dynamodb/
├── main.tf          # 核心资源定义
├── variables.tf     # 变量声明
└── outputs.tf       # 输出配置
2. 变量定义(variables.tf)

这里定义所有可配置的变量,包括动态属性列表,还给了合理的默认值,方便你快速测试:

variable "table_name" {
  description = "Name of the DynamoDB table"
  type        = string
  default     = "GameScores"
}

variable "attributes" {
  description = "List of attributes for the DynamoDB table (each attribute has name and type)"
  type = list(object({
    name = string
    type = string # 支持S(字符串)、N(数字)、B(二进制)
  }))
  default = [
    {
      name = "UserId"
      type = "S"
    },
    {
      name = "GameTitle"
      type = "S"
    }
  ]
}

variable "hash_key" {
  description = "Name of the hash key attribute"
  type        = string
  default     = "UserId"
}

variable "range_key" {
  description = "Name of the range key attribute (optional)"
  type        = string
  default     = "GameTitle"
}

variable "read_capacity" {
  description = "Read capacity units for the table"
  type        = number
  default     = 5
}

variable "write_capacity" {
  description = "Write capacity units for the table"
  type        = number
  default     = 5
}

variable "billing_mode" {
  description = "Billing mode for the table (PROVISIONED or PAY_PER_REQUEST)"
  type        = string
  default     = "PROVISIONED"
}
3. 核心资源配置(main.tf)

这里用Terraform的dynamic块来实现你伪代码里的循环逻辑,动态生成所有属性:

resource "aws_dynamodb_table" "main" {
  name           = var.table_name
  billing_mode   = var.billing_mode
  read_capacity  = var.read_capacity
  write_capacity = var.write_capacity

  # 动态生成属性列表
  dynamic "attribute" {
    for_each = var.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  hash_key = var.hash_key
  # 只有当range_key有值时才配置
  range_key = var.range_key != "" ? var.range_key : null
}

这里的dynamic "attribute"块会遍历var.attributes列表,为每个元素生成一个attribute块,完美替代你伪代码里的for循环逻辑

4. 输出配置(outputs.tf)

输出一些常用的表属性,方便在调用模块的地方引用:

output "table_arn" {
  description = "ARN of the DynamoDB table"
  value       = aws_dynamodb_table.main.arn
}

output "table_name" {
  description = "Name of the DynamoDB table"
  value       = aws_dynamodb_table.main.name
}

output "table_id" {
  description = "ID of the DynamoDB table"
  value       = aws_dynamodb_table.main.id
}
5. 模块使用示例

在你的根Terraform目录里,这样调用这个模块:

module "game_scores_table" {
  source = "./modules/dynamodb"

  # 如果你不想用默认值,可以在这里直接赋值,或者通过terraform.tfvars传递
  table_name = "MyCustomGameScores"
  attributes = [
    {
      name = "PlayerId"
      type = "S"
    },
    {
      name = "Score"
      type = "N"
    }
  ]
  hash_key     = "PlayerId"
  billing_mode = "PAY_PER_REQUEST"
}

或者创建terraform.tfvars文件来传递变量:

table_name = "MyCustomGameScores"
attributes = [
  {
    name = "PlayerId"
    type = "S"
  },
  {
    name = "Score"
    type = "N"
  }
]
hash_key     = "PlayerId"
billing_mode = "PAY_PER_REQUEST"

这样整个模块就完全满足你的需求了:所有属性和配置都可以通过变量控制,既可以用默认值快速启动,也可以通过.tfvars文件自定义所有参数,不用修改模块内部的资源代码。

内容的提问来源于stack exchange,提问作者Baghel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:10:36