簡體   English   中英

如何使用特定的命名約定創建多個目標組 terraform

[英]How to create multiple target groups with specific naming conventions terraform

我有一個創建目標組的資源。 我想修改資源,使其能夠創建 n 個具有特定命名約定的目標組。

resource "aws_lb_target_group" "main" {
  name                          = "${var.name}-tg"
  port                          = var.forward_port
  protocol                      = var.forward_protocol
  target_type                   = var.target_type
  deregistration_delay          = var.deregistration_delay
  health_check {
    interval            = var.interval
    path                = var.path
  }
  stickiness {
    type            = var.cookie_type
  }
} 

我想以這樣一種方式修改它,如果只創建一個目標組,它應該將其命名為 test-tg,但是如果有多個目標組,那么它應該將索引添加到名稱中,如 test-tg1。 每個目標組也可以有自己的一組特定變量值。 我怎樣才能做到這一點?

在這種情況下,您可以使用 for_each,因為您想要創建具有不同配置的多個 TG。 創建一個具有 map(object()) 類型的變量,它允許您按照您期望的方式放置值。 我放了一個示例代碼。 參考以下內容。

請將以下變量的值放入 terraform.tfvars 文件中

cookie_type, path, deregistration_delay

變量.tf

variable "TG_conf" {
  type = map(object({
    port              = string
    protocol          = string
    target_type       = string
    interval          = string
    cookie_type       = string
    path              = string
    deregistration_delay = string
  }))
}

地形.tfvars

TG_conf = {
  "test-tg" = {

    port              = 80
    protocol          = HTTP
    target_type       = "instance
    interval          = "5"
    cookie_type       = string
    path              = string
    deregistration_delay = string
  }

白蛋白.tf

resource "aws_lb_target_group" "main" {
  for_each = var.TG_conf
  name                          = "${each.key}"
  port                          = each.value.port
  protocol                      = each.value.protocol
  target_type                   = each.value.target_type
  deregistration_delay          = each.value.deregistration_delay
  health_check {
    interval            = each.value.interval
    path                = each.value.path
  }
  stickiness {
    type            = each.value.cookie_type
  }
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM