简体   繁体   中英

Creating multiple RDS instances using Terraform count but with different Tags

I have hit upon this requirement of creating multiple RDS instances with all db properties remaining same. Only that the tags be different. I'm using Terraform for my deployments and count really helps me in these situations. But is there a way where my RDS Instances are created using count but Tags should be different.

Code:

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"]
  }
}

Supposingly I deploy 2 RDS and below is what I intend my tags to look like:

#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}"
  }  

Please confirm if there's any way this can be achieved.

Above code will make only one RDS or nothing. You cannot create more then 2 RDS with it.

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

And I think it is not good idea to create muliple RDS with "count" for different purpose even the spec requirements are same. For example, there are 4 RDS and if you want to scale up one of those RDS. It is hard to manage it. It is better to copy the code and paste it multiple times. Or you can create module for it.

Anyway, you can create different tags for each RDS like below. Make list variable (var.PRODUCT) and use "element" instead of var.PRODUCT1 or 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))}"
...
  }

If it is hard to create new list variable, then you can create local variable for it.

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))}"
...
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM