英文:
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 = "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?
</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 "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
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论