Search code examples
terraformterraform-providerincapsula

I don't know how to make as many origin_servers as I need to have


I am using the Incapsula provider for Terraform. Specifically the resource: incapsula_data_centers_configuration. As you can see in the documentation it indicates that:

You can configure as many origin_servers, as you need. In my case I have a single data_center but I need to be able to vary the number of origin_servers depending on the application I am protecting.

Here is some of the code:

variable "ip_addresses"{
  default = [
    "1.2.3.4", "2.3.4.5"
  ] 
}

resource "incapsula_data_centers_configuration" "single_dc" {
  
    site_id = incapsula_site.pre-site.id
    site_topology = var.site_topology
    site_lb_algorithm = "BEST_CONNECTION_TIME"
    fail_over_required_monitors = "MOST"
    is_persistent = true

    data_center {
      name = var.single_dc_name
      dc_lb_algorithm = "LB_LEAST_PENDING_REQUESTS" 
      origin_server{
        for_each = toset(var.ip_addresses)
        address = each.value
      }
    }
}

And the error I have is the following:

Error: each.value cannot be used in this context │ │ on modules/pre-site/main.tf line 31, in resource "incapsula_data_centers_configuration" "single_dc": │ 31: address = each.value

Could you help me to resolve the problem?


Solution

  • You have to use dynamic blocks:

    
    resource "incapsula_data_centers_configuration" "single_dc" {
      
        site_id = incapsula_site.pre-site.id
        site_topology = var.site_topology
        site_lb_algorithm = "BEST_CONNECTION_TIME"
        fail_over_required_monitors = "MOST"
        is_persistent = true
    
        data_center {
          name = var.single_dc_name
          dc_lb_algorithm = "LB_LEAST_PENDING_REQUESTS" 
          dynamic "origin_server" {
            for_each = toset(var.ip_addresses)
            content {            
                address = origin_server.value
            }
          }
        }
    }