IBM-Cloud / terraform-provider-ibm

https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs
Mozilla Public License 2.0
341 stars 670 forks source link

IBM LBaaS dynamic association of instances #203

Closed FrenchBen closed 6 years ago

FrenchBen commented 6 years ago

Terraform Version

0.11.3

Affected Resource(s)

Terraform Configuration Files

Taken from: https://ibm-cloud.github.io/tf-ibm-docs/v0.8.0/r/lbaas.html

resource "ibm_compute_vm_instance" "vm_instances" {
  count = "2"
  ....
}

resource "ibm_lbaas" "lbaas" {
  name        = "terraformLB"
  description = "delete this"
  subnets     = [1511875]

  protocols = [{
    frontend_protocol     = "HTTPS"
    frontend_port         = 443
    backend_protocol      = "HTTP"
    backend_port          = 80
    load_balancing_method = "round_robin"
    tls_certificate_id    = 11670
  },
    {
      frontend_protocol     = "HTTP"
      frontend_port         = 80
      backend_protocol      = "HTTP"
      backend_port          = 80
      load_balancing_method = "round_robin"
    },
  ]

  server_instances = [
    {
      "private_ip_address" = "${ibm_compute_vm_instance.vm_instances.0.ipv4_address_private}"
    },
    {
      "private_ip_address" = "${ibm_compute_vm_instance.vm_instances.1.ipv4_address_private}"
    },
  ]
}

Expected Behavior

Use the dynamically provided VM information, to create an LB that supports all instances

Actual Behavior

The above example (seen in the documentation), provides a count for the ibm_compute_vm_instance in order to populate a certain number of VMs pretty easily, yet is incapable of dynamically allocating those VM IPs to the load balancer. If I was to switch the count = "2" to a count = "3", I would manually have to update the load balancer definition to be:

server_instances = [
    {
      "private_ip_address" = "${ibm_compute_vm_instance.vm_instances.0.ipv4_address_private}"
    },
    {
      "private_ip_address" = "${ibm_compute_vm_instance.vm_instances.1.ipv4_address_private}"
    },
    {
      "private_ip_address" = "${ibm_compute_vm_instance.vm_instances.2.ipv4_address_private}"
    },
  ]

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply
  2. Update the VM count
  3. terraform apply (your new VM isn't part of the LB, unless you update the actual code
Praveengostu commented 6 years ago

I have not came across any solution for this. Pls check the available builtin functions of terraform at https://www.terraform.io/docs/configuration/interpolation.html#built-in-functions

Praveengostu commented 6 years ago

Alternatively we can come up with a resource to attach the server instances to lbaas. Through which we can support the dynamic association.. We will look in to this

FrenchBen commented 6 years ago

@Praveengostu In your terraform deployments, how do you attach these dynamic instances then?

Could we remove the complexity of the server_instance structure, and use something like a list? How often is the "weight" used? (seems like the only reason for the complex variable)

For comparison, see the simplicity of associating an instance with an ELB within the AWS terraform module: https://www.terraform.io/docs/providers/aws/r/elb.html

Praveengostu commented 6 years ago

@FrenchBen as mentioned we will look in to the lbaas resource on removing the complexity to attach dynamic instances. Thank you

Praveengostu commented 6 years ago

To achieve the dynamic association of server intances, One of the option is to remove the server instaces from lbaas and create a seperate resource for attaching the server instance. Example:

resource "ibm_compute_vm_instance" "vm_instances" {
  count = "2"
  ....
}

resource "ibm_lbaas" "lbaas" {
  name        = "terraformLB"
  description = "delete this"
  subnets     = [1511875]

  protocols = [{
    frontend_protocol     = "HTTPS"
    frontend_port         = 443
    backend_protocol      = "HTTP"
    backend_port          = 80
    load_balancing_method = "round_robin"
    tls_certificate_id    = 11670
  },
    {
      frontend_protocol     = "HTTP"
      frontend_port         = 80
      backend_protocol      = "HTTP"
      backend_port          = 80
      load_balancing_method = "round_robin"
    },
  ]
}

resource "ibm_lbaas_server_instance_attachment" "server_attach" {
    count = 2
    private_ip_address = "${element(ibm_compute_vm_instance.vm_instances.*.ipv4_address_private,count.index)}"
    lbaas_id = "${ibm_lbaas.lbaas.id}
}

With this we can dynamically attach the server instances to lbaas. Let us know if this helps.

FrenchBen commented 6 years ago

@Praveengostu when do you think this can go into the plugin/release?

Praveengostu commented 6 years ago

@FrenchBen The fix is in progress and I will get back to you on the availability of release with fix.

Praveengostu commented 6 years ago

@FrenchBen The fix is available in the latest build located at https://github.com/IBM-Cloud/terraform-provider-ibm/releases/tag/v0.9.1