【问题标题】:Multiple instances using the same variable使用相同变量的多个实例
【发布时间】:2020-10-31 06:06:57
【问题描述】:

我有一个在 AWS 中创建数据库的模块:

resource "aws_rds_cluster_instance" "db_instances" {
  count      = lookup(var.argument, "count")
  identifier = lookup(var.argument, "identifier", count.index)
}

参数变量如下:

variable "argument" {
  type = map(string)
}

在我的根 main.tf 中,当我尝试创建 2 个 db 实例时出现错误,因为它们都试图使用相同的标识符名称,但是由于我在模块中使用了 count.index,所以我认为它会小心在数据库名称的末尾添加一个数字。

variable "argument" {
  default = {
    count      = 2
    identifier = "my-db-name"
  }
}

如何让我的数据库名称变为“my-db-name-0”和“my-db-name-1”?

【问题讨论】:

    标签: amazon-web-services count terraform amazon-rds


    【解决方案1】:

    从外观上看,您已经在使用 terraform 0.12...
    我建议您使用for_each,这样您就可以在 RDS 变量上拥有更多属性,下面是一个示例

    variable "rds" {
      default = {
        "my-db-name-0" = {
          engine         = "foo"
          instance_class = "db.r4.large"
        }
        "my-db-name-1" = {
          engine         = "bar"
          instance_class = "db.r4.small"
        }
    }
    
    resource "aws_rds_cluster_instance" "db_instance" {
      for_each = var.rds
    
      identifier     = each.key
      engine         = each.value.engine
      instance_class = each.value.instance_class
    }
    

    这样,标识符名称可以是您想要的任何名称...
    它使您可以灵活地自定义每个实例参数:
    https://www.terraform.io/docs/providers/aws/r/rds_cluster_instance.html#argument-reference

    【讨论】:

      【解决方案2】:

      您可以通过将join functioncount.index 值结合使用来做到这一点。

      resource "aws_rds_cluster_instance" "db_instances" {
        count      = lookup(var.argument, "count")
        cluster_identifier = join("-", [lookup(var.argument, "identifier"), count.index])
        instance_class = "db.t2.small"
      }
      

      【讨论】:

      • @ECL-94 如果这是缺少参数的问题,那么您将需要包含 cluster_identifier instance_class
      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 2017-05-18
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多