我不知道如何创建我需要的原始服务器数量。

huangapple go评论61阅读模式
英文:

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

问题

抱歉,我理解你需要中文翻译,以下是你提供的部分内容的翻译:

我正在使用Terraform的Incapsula提供程序。具体来说,我在使用资源:incapsula_data_centers_configuration。正如您在文档中所看到的,它指出:

您可以根据需要配置多个origin_servers。在我的情况下,我只有一个数据中心,但我需要根据我要保护的应用程序的数量来变化origin_servers的数量。

以下是一些代码:

```hcl
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
      }
    }
}

而我遇到的错误是:

错误:无法在此上下文中使用each.value

│ 在modules/pre-site/main.tf的第31行,位于resource "incapsula_data_centers_configuration" "single_dc"内:
│ 31: address = each.value

你能帮助我解决这个问题吗?


<details>
<summary>英文:</summary>

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 = &quot;BEST_CONNECTION_TIME&quot;
fail_over_required_monitors = &quot;MOST&quot;
is_persistent = true

data_center {
  name = var.single_dc_name
  dc_lb_algorithm = &quot;LB_LEAST_PENDING_REQUESTS&quot; 
  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 &quot;incapsula_data_centers_configuration&quot; &quot;single_dc&quot;:
│   31:         address = each.value


Could you help me to resolve the problem?

</details>


# 答案1
**得分**: 2

你需要使用[动态块](https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks):

```hcl
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
        }
      }
    }
}
英文:

You have to use dynamic blocks:


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

    data_center {
      name = var.single_dc_name
      dc_lb_algorithm = &quot;LB_LEAST_PENDING_REQUESTS&quot; 
      dynamic &quot;origin_server&quot; {
        for_each = toset(var.ip_addresses)
        content {            
            address = origin_server.value
        }
      }
    }
}

huangapple
  • 本文由 发表于 2023年6月15日 18:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定