使用Terraform计数但使用不同的标签创建多个RDS实例

时间:2019-07-08 09:28:20

标签: amazon-web-services terraform

我满足了创建多个RDS实例且所有数据库属性保持不变的要求。只是标签不同。我正在使用Terraform进行部署,在这种情况下,计数确实可以为我提供帮助。但是有没有一种方法可以使用count创建我的RDS实例,但是标签应该不同。

代码:

resource "aws_db_instance" "rds-mysql" {
  count = "${var.RDS_INSTANCE["deploy"] == "true" ? 1 : 0}"
  allocated_storage           = "${var.RDS_INSTANCE[format("allocated_storage.%s",var.ENVIRONMENT)]}"
  auto_minor_version_upgrade  = true
  backup_retention_period     = "${var.RDS_INSTANCE[format("backup_retention_period.%s",var.ENVIRONMENT)]}"
  db_subnet_group_name        = "${aws_db_subnet_group.rds-mysql.id}"
  engine               = "${var.RDS_INSTANCE["engine"]}"
  final_snapshot_identifier = "${format("%s-%s-%s-rds-mysql-final-snapshot",var.PRODUCT,var.ENVIRONMENT,var.REGION_SHORT_NAME)}"
  engine_version       = "${var.RDS_INSTANCE["engine_version"]}"
  instance_class       = "${var.RDS_INSTANCE[format("instance_class.%s",var.ENVIRONMENT)]}"
  multi_az = "${var.RDS_INSTANCE[format("multi_az.%s",var.ENVIRONMENT)]}"
  parameter_group_name = "${aws_db_parameter_group.rds-mysql.id}"
  password = "${var.RDS_MASTER_USER_PASSWORD}"
  skip_final_snapshot = "${var.RDS_INSTANCE[format("skip_final_snapshot.%s",var.ENVIRONMENT)]}"
  storage_encrypted = "${var.RDS_INSTANCE[format("storage_encrypted.%s",var.ENVIRONMENT)]}"
  storage_type = "gp2"
  username = "${var.RDS_INSTANCE["username"]}"
  vpc_security_group_ids = ["${var.SG_RDS_MYSQL_ID}"]
  tags {
    Name = "${format("%s-%s-%s-rds-mysql",var.PRODUCT,var.ENVIRONMENT,var.REGION_SHORT_NAME)}"
    Project = "${format("%s-share",var.PRODUCT)}"
    Environment = "${var.ENVIRONMENT}"
  }
  #Resource lifecycle
    lifecycle {
        ignore_changes = ["allocated_storage","instance_class"]
  }
}

假设我部署了2个RDS,下面是我希望标签显示的样子:

#RDS 1

  tags {
    Name = "${format("%s-%s-%s-rds-mysql",var.PRODUCT1,var.ENVIRONMENT,var.REGION_SHORT_NAME)}"
    Project = "${format("%s-share",var.PRODUCT1)}"
    Environment = "${var.ENVIRONMENT}"
  }

#RDS2

  tags {
    Name = "${format("%s-%s-%s-rds-mysql",var.PRODUCT2,var.ENVIRONMENT,var.REGION_SHORT_NAME)}"
    Project = "${format("%s-share",var.PRODUCT2)}"
    Environment = "${var.ENVIRONMENT}"
  }  

请确认是否有任何方法可以实现。

1 个答案:

答案 0 :(得分:1)

以上代码将仅生成一个RDS或不生成任何RDS。您不能使用它创建2个以上的RDS。

  count = "${var.RDS_INSTANCE["deploy"] == "true" ? 1 : 0}"

我认为,即使规格要求相同,创建具有“ count”目的的多个RDS也不是一个好主意。例如,有4个RDS,并且如果您想扩展这些RDS之一。很难管理。最好将代码复制并粘贴多次。或者,您可以为其创建模块。

无论如何,您可以为每个RDS创建不同的标签,如下所示。 使列表变量(var.PRODUCT)并使用“元素”代替var.PRODUCT1或var.PRODUCT2

variable "PRODUCT" {
  default = [
    "test1",
    "test2",
    "test3",
  ]
}

...
  tags {
    Name = "${format("%s-%s-%s-rds-mysql", element(var.PRODUCT, count.index) ,var.ENVIRONMENT,var.REGION_SHORT_NAME)}"
    Project = "${format("%s-share", element(var.PRODUCT, count.index))}"
...
  }

如果很难创建新的列表变量,则可以为其创建局部变量。

locals {
  PRODUCT = ["${var.PRODUCT1}", "${var.PRODUCT2}", "${var.PRODUCT3}"]
}

...
  tags {
    Name = "${format("%s-%s-%s-rds-mysql", element(local.PRODUCT, count.index) ,var.ENVIRONMENT,var.REGION_SHORT_NAME)}"
    Project = "${format("%s-share", element(local.PRODUCT, count.index))}"
...
  }