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

Terraform创建aws_db_instance时如何配置snapshot_identifier条件参数

你可以通过Terraform原生的null参数语义实现需求,0.12及以上版本的Terraform规定:如果资源参数被赋值为null,会等同于完全不声明该参数,不会传递给云厂商API。

你可以直接修改对应参数行,显式加条件判断兼容边界情况,示例如下:

resource "aws_db_instance" "mariaDB" {
  for_each = var.rds_databases[terraform.workspace]

  identifier           = "api-mariadb-${each.value.name}"
  allocated_storage    = each.value.allocated_storage
  storage_type         = each.value.storage_type
  engine               = each.value.engine
  engine_version       = each.value.snapshot_identifier == null ? each.value.engine_version : null
  allow_major_version_upgrade = each.value.allow_major_version_upgrade
  auto_minor_version_upgrade = each.value.auto_minor_version_upgrade
  instance_class       = each.value.instance_class
  name                 = each.value.snapshot_identifier == null ? each.value.dbname : null
  username             = each.value.snapshot_identifier == null ? each.value.dbusername : null
  password             = each.value.snapshot_identifier == null ? each.value.dbpassword : null
  db_subnet_group_name = aws_db_subnet_group.subnet-mariadb[each.value.name].name
  skip_final_snapshot  = true
  vpc_security_group_ids = [aws_security_group.rds_SG.id]
  storage_encrypted    = true
  
  # 仅当snapshot_identifier不为null时参数才会生效,为null时等同于未声明该参数
  snapshot_identifier = each.value.snapshot_identifier != null ? each.value.snapshot_identifier : null
}

注意:如果使用快照创建RDS实例,name、username、password、engine_version这几个参数不能和快照中的配置冲突,所以示例中也对应加了条件判断,快照模式下这几个参数会自动设为null,避免创建时报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 07:39:03