简体   繁体   English

需要使用 terraform 创建 3 个 ILB

[英]Need to create 3 ILB using terraform

The code works for creating one ILB but I need to create three ILB's using the following terraform code.该代码适用于创建一个 ILB,但我需要使用以下 terraform 代码创建三个 ILB。 but im unable to come up with the logic for count or for each.但我无法想出计数或每个的逻辑。 like for example if i use 'for each' in module for ilb_name how do i refer the ip address and group manager using the same for each.例如,如果我在模块中为 ilb_name 使用“for each”,我如何引用 ip 地址和组管理器,每个都使用相同的地址。 Thanks in advance for your time.在此先感谢您的时间。

Terraform tf vars Terraform tf vars


ilb_name          = ["qat-rs-web","qat-rs-lb","qat-wi-web"]
project_id        = "cloudops-dev01-sb"
#"ei-cs-qat01-ein"
project_name      = "cloudops-dev01-sb"
#"ei-cs-qat01-ein"
region            = "us-east4"
#"us-west1"
subnetwork        = "cloudops-dev01-sb-sandbox-us-east4"
health_check_port = "4000"
ports             = [4000, 5000]
load_balancer_ip_name = "test-ilb-ip"


#instance-template
name_prefix  = "rs-instance-template"
instance_description = "rs instance template"
machine_type         = "n1-standard-2"
network              = "dev-shared"
subnetwork_project   = "dev-host"
compute_ip_address   = ["10.238.159.119","10.238.159.120","10.238.159.121"]
image_family         = "gold-centos-7"
source_image_project = "dev-cietools"

group_manager_name = ["rs-web-igm","rs-lb-igm","wi-web-igm"]
base_instance_name = "qat-rs-app"
target_size        = "2" 

modules/ilb.tf模块/ilb.tf

data "google_compute_image" "default_image" {
  family  = var.image_family
  project = var.source_image_project
}
resource "google_compute_address" "load_balancer_ip" {
  project      = var.project_id
  name         = "load-balancer-ip-name-${var.project_id}"
  subnetwork   = var.subnetwork
  address_type = "INTERNAL"
  address      = var.compute_ip_address
  region       = var.region
}

resource "google_compute_instance_template" "instance_template" {
  name_prefix          = var.name_prefix
  instance_description = var.instance_description
  machine_type         = var.machine_type
  region               = var.region
  #foll.
  project = var.project_id

  labels = var.labels

  // boot disk
  disk {
    source_image = data.google_compute_image.default_image.self_link
    auto_delete  = false
  }

  network_interface {
    network            = var.network
    subnetwork         = var.subnetwork
    subnetwork_project = var.subnetwork_project
  }


}

resource "google_compute_region_instance_group_manager" "rs_igm" {
  name               = var.group_manager_name
  base_instance_name = var.base_instance_name
  region             = var.region
  target_size        = var.target_size
  project = var.project_id

  version {
    instance_template = google_compute_instance_template.instance_template.id
  }


  update_policy {
    type                  = "PROACTIVE"
    minimal_action        = "REPLACE"
    max_surge_fixed       = 4
    max_unavailable_fixed = 0
  }
  auto_healing_policies {
    health_check      = google_compute_health_check.tcp.id
    initial_delay_sec = 300
  }
}


# Forwarding Rule
resource "google_compute_forwarding_rule" "default" {
  name    = var.name
  project = var.project_id
  region  = var.region
  subnetwork            = var.subnetwork
  load_balancing_scheme = "INTERNAL"
  backend_service       = google_compute_region_backend_service.default.self_link
  ip_protocol           = "TCP"
  ip_address            = google_compute_address.load_balancer_ip.address
  ports                 = var.ports
  service_label         = var.name
}

# Backend
resource "google_compute_region_backend_service" "default" {
  name                            = var.name
  project                         = var.project_id
  region                          = var.region
  protocol                        = "TCP"
  timeout_sec                     = 10
  connection_draining_timeout_sec = 10
  session_affinity                = var.session_affinity

  /* "backend" {
    for_each = var.backends
    content {
      description = lookup(backend.value, "description", null)
      group       = lookup(backend.value, "group", null)
    }
  }*/

   backend {
    group          = google_compute_region_instance_group_manager.rs_igm.instance_group
    balancing_mode = "UTILIZATION"
  }

  health_checks = [google_compute_health_check.tcp.self_link]

}

# Health Check(s)
resource "google_compute_health_check" "tcp" {
  name    = "${var.name}-hc"
  project = var.project_id

  tcp_health_check {
    port = var.health_check_port
  }
}

main.tf主文件

module "qat_hosted_pacs_ilb" {
  source = "../modules/load_balancer/"
  for_each = toset([var.ilb_name])
  image_family = var.image_family
  source_image_project = var.source_image_project
  project_id = var.project_id
  #load_balancer_ip_name = var.load_balancer_ip_name
  compute_ip_address = each.value.compute_ip_address
  region = var.region
  network            = var.network
  subnetwork         = var.subnetwork
  subnetwork_project = var.subnetwork_project
  
  name_prefix = var.name_prefix
  instance_description = var.instance_description
  machine_type = var.machine_type
  labels = var.labels

  group_manager_name = each.value.group_manager_name
  base_instance_name = var.base_instance_name
  target_size = var.target_size

  name = each.value.ilb_name
  ports = var.ports

  
}
Error: Unsupported attribute

  on main.tf line 8, in module "qat_hosted_pacs_ilb":
   8:   compute_ip_address = each.value.compute_ip_address
    |----------------
    | each.value is "cloudops-dev01-ilb"

This value does not have any attributes.


Error: Unsupported attribute

  on main.tf line 19, in module "qat_hosted_pacs_ilb":
  19:   group_manager_name = each.value.group_manager_name
    |----------------
    | each.value is "cloudops-dev01-ilb"

This value does not have any attributes.


Error: Unsupported attribute

  on main.tf line 23, in module "qat_hosted_pacs_ilb":
  23:   name = each.value.ilb_name
    |----------------
    | each.value is "cloudops-dev01-ilb"

This value does not have any attributes.

Since you are keeping ilb_name , group_manager_name and compute_ip_address as three different lists, instead of for_each it would be easier to use count :由于您将ilb_namegroup_manager_namecompute_ip_address为三个不同的列表,因此使用for_each会更容易count

module "qat_hosted_pacs_ilb" {
  source = "../modules/load_balancer/"
  count  = length(ilb_name)
  image_family = var.image_family
  source_image_project = var.source_image_project
  project_id = var.project_id
  #load_balancer_ip_name = var.load_balancer_ip_name
  compute_ip_address = var.compute_ip_address[count.index]
  region = var.region
  network            = var.network
  subnetwork         = var.subnetwork
  subnetwork_project = var.subnetwork_project
  
  name_prefix = var.name_prefix
  instance_description = var.instance_description
  machine_type = var.machine_type
  labels = var.labels

  group_manager_name = var.group_manager_name[count.index]
  base_instance_name = var.base_instance_name
  target_size = var.target_size

  name  = var.ilb_name[count.index]
  ports = var.ports  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 terraform 创建多个 GCP 存储桶 - Create multiple GCP storage buckets using terraform 无法在 AWS 上使用 Terraform 创建 ECS 服务 - Cannot create an ECS Service using Terraform on AWS 是否可以使用 terraform 创建 SSO 用户 - AWS - Is it possible to create an SSO user using terraform - AWS 使用 Terraform 创建具有 inte.net 访问权限的计算引擎 - Create a Compute Engine with the internet access by using Terraform 使用 Terraform 创建 Azure 突触管道 - Create Azure Synapse Pipeline using Terraform 如何使用 Terraform 创建 Azure Windows 虚拟机? - How to create Azure Windows VM using Terraform? Terraform 使用 for_each 和 jsondecode 创建多个资源 - Terraform create multiple resources using for_each and jsondecode 如何使用 Terraform 在 API Gateway 中创建一个具有启用的 cloudwatch 指标的阶段? - How to create a stage in API Gateway with enabled cloudwatch metrics using terraform? 我可以使用 Terraform 的 CDK 创建 AWS state 机器吗? - Can I create an AWS state machine using the CDK for Terraform? 我可以使用 Terraform 创建 GCP API 密钥吗? - Can I create GCP API keys using Terraform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM