terraform-provider-bigip icon indicating copy to clipboard operation
terraform-provider-bigip copied to clipboard

Problem to add pool atttachments with for_each

Open Hoonigan-013 opened this issue 2 years ago • 6 comments

Hello,

I am using a different for_each in pool resources to create more than one pool with the same resource and another for_each in node to create new nodes with the same resources. So, if I try to use any of these two for_each in pool attachment I receive index error because pool is using one for_each and nodes another for_each.

Could you help me, how can I solve this issue? Is it possible create pool attachments with for in both resources (pool and node)?

Hoonigan-013 avatar Sep 29 '23 18:09 Hoonigan-013

Hi @Hoonigan-013,

Can you share your TF file with the for_each?

pgouband avatar Oct 02 '23 06:10 pgouband

Hi, @pgouband

resource "bigip_ltm_pool" "pool" {
  for_each = var.vs_creation
  name                   = "/Part/${format("%s-%s-%s","pool","${each.value.service_name}","${each.value.pool_port}")}"
  load_balancing_mode    = "${each.value.pool_lb_mode}"
  minimum_active_members = 1
  monitors               = "${each.value.pool_monitor}"
  slow_ramp_time         = 0
  depends_on             = [bigip_ltm_node.node]
}

resource "bigip_ltm_pool_attachment" "attach_node" {
  for_each = var.node_creation
  pool = "${bigip_ltm_pool.pool[each.key].name}"
  node = "${bigip_ltm_node.node[each.key].name}:${each.value.pool_port}"
}


resource "bigip_ltm_node" "node" {
  for_each = var.node_creation
  name             = "/Part/${each.value.node_name}"
  #name             = "/Part/${var.node_name}"
  address          = "${each.value.node_address}"
  connection_limit = "0"
  dynamic_ratio    = "1"
  monitor          = "${each.value.node_monitor}"
  rate_limit       = "disabled"

Just to explain Why I am using different for_eachs. In vs_creation there's attributes to create all pool and vs configuration and in node_creation all configuration to node creation. It was separeted because node is a string type and when I need to create more than one node with string variable inside vs_creation it is not possible create more than one node. So, the ideia is create a for each to nodes to create how many nodes I need and after it create pool attachment with the same quantity as nodes creation.

For example: One pool creation Two nodes creation Two attachment creation

So, the nodes will be attached in that single pool that was created.

This way, I receive errors with this each values because they belongs a var.vs_creation not var.node_creation : "${bigip_ltm_pool.pool[each.key].name}" "${each.value.pool_port}"

This correlation is being the problem for me :(

Hoonigan-013 avatar Oct 02 '23 13:10 Hoonigan-013

Hi @Hoonigan-013,

Thanks for sharing. Could you also share one example of each variable and tfvars?

pgouband avatar Oct 03 '23 09:10 pgouband


variables.tf

variable "vs_creation" {
  type = map(object({
    service_name = string
    vs_port         = number
    vs_profile      = list(string)
    vs_persistence  = optional(list(string))
    pool_lb_mode    = string
    pool_monitor    = list(string)
    pool_port       = string
  }))
  default = {
    service_name    = null
    vs_port         = null
    vs_profile      = null
    vs_persistence  = null
    pool_lb_mode    = null
    pool_monitor    = null
    pool_port       = null
  }
}

variable "node_creation" {
  type = map(object({
    node_name       = string
    node_address    = string
    node_monitor    = string
  }))
}

terraform.tfvars

vs_creation = {
01  = {
      service_name    = "test"
      vs_port         = 443
      vs_profile      = ["/Common/tcp", "/Common/http"]
      vs_persistence  = ["/Common/source_addr"]
      pool_lb_mode    = "round-robin"
      pool_monitor    = ["/Common/gateway_icmp"]
      pool_port       = "443"
    } 

node_creation = {
    
    01 = {
      node_name       = "test.hom.si.net"
      node_address    = "172.172.10.249"
      node_monitor    = "/Common/gateway_icmp"
    }

I tried with different for_each because sometimes I need to add more than one node, so using only vs_creation I cannot add more than one node because node address and node name are string type.

Hoonigan-013 avatar Oct 03 '23 16:10 Hoonigan-013

Hi @Hoonigan-013,

In the following resource, the for_each is based on node_creation but in node_creation there is no pool_port.

resource "bigip_ltm_pool_attachment" "attach_node" {
  for_each = var.node_creation
  pool = "${bigip_ltm_pool.pool[each.key].name}"
  node = "${bigip_ltm_node.node[each.key].name}:${each.value.pool_port}"
}

Here an example of solution, adding pool_port in node_creation variable.

variables.tf

variable "node_creation" {
  type = map(object({
    node_name       = string
    node_address    = string
    node_monitor    = string
    pool_port       = string
  }))
}

terraform.tfvars

node_creation = {    
    01 = {
      node_name       = "test.hom.si.net"
      node_address    = "172.172.10.249"
      node_monitor    = "/Common/gateway_icmp"
      pool_port       = "443"
    }
}

pgouband avatar Oct 04 '23 10:10 pgouband