两个模块之间的模块组合使用条件 for_each 双向 [英] Module composition between two modules using a conditional for_each on both ways
问题描述
在我的根模块上,我声明了两个模块(paired_regions_network 和paired_regions_app),它们都迭代了一组区域.
On my root module, I am declaring two modules (paired_regions_network and paired_regions_app), that both iterate a set of regions.
module "paired_regions_network" {
source = "./modules/network"
application_hostname = module.paired_regions_app[each.key].website_hostname
...
for_each = ( var.environment == "TEST" || var.environment == "PROD") ? { region1 = var.paired_regions.region1 } : { }
}
module "paired_regions_app" {
source = "./modules/multi-region"
wag_public_ip = module.paired_regions_network[each.key].wag_public_ip
...
for_each = (var.environment == "TEST" || var.environment == "PROD") ? var.paired_regions : { region1 = var.paired_regions.region1 }
}
output "network_outputs" {
value = module.paired_regions_network
}
output "app_outputs" {
value = module.paired_regions_app
}
迭代区域声明如下:
variable "paired_regions" {
description = "The paired regions"
default = {
region1 = {
...
},
region2 = {
...
}
}
}
从 paired_regions_network
模块我想访问来自 paired_regions_app
模块的输出,即我想要的 website_hostname
值分配给 paired_regions_network
模块的 application_hostname
参数,如上所示.
From the paired_regions_network
module I want to have access to the output coming from the paired_regions_app
module, namely the website_hostname
value, which I want to assign to the application_hostname
parameter, of the paired_regions_network
module, as shown above.
output "website_hostname" {
value = azurerm_app_service.was_app.default_site_hostname
description = "The hostname of the website"
}
从 paired_regions_app
模块我想访问来自 paired_regions_network
模块的输出,即 wag_public_ip
值,我想分配给paired_regions_app
模块的同名参数,如上图.
And from the paired_regions_app
module I want to have access to the output coming from the paired_regions_network
module, namely the wag_public_ip
value, which I want to assign to the parameter with the same name, of the paired_regions_app
module, as shown above.
output "wag_ip_address" {
value = azurerm_public_ip.network_ip.ip_address
description = "The Public IP address that will be used by the app gateway"
}
但这会导致一个依赖循环,我无法摆脱它.错误如下:
But this causes a dependency cycle, that I can't get rid off. The error is the following:
错误:循环:...
能否在两个模块之间传递输出,而不引起依赖循环?
Can I pass the output between the two modules, without causing the dependency cycle?
推荐答案
根据@Marcin 的建议,我能够通过创建仅包含公共 IP 资源的第三个模块来克服这个问题.因此,paired_regions_app
模块将依赖于新模块,而不是依赖于 paired_regions_network
模块.paired_regions_network
将依赖于其他两个模块.除了移除 paired_regions_network
模块的输出外,代码改动如下:
As per @Marcin's advice, I was able to overcome the issue by creating a third module containing only the Public IP resource. So, the paired_regions_app
module would depend on the new module instead of depending on the paired_regions_network
module. The paired_regions_network
would then depend on both the other two modules. Besides removing the output from the paired_regions_network
module, the code changes are as follows:
根模块
module "paired_regions_ips" {
source = "./modules/public-ip"
...
for_each = ( var.environment == "TEST" || var.environment == "PROD") ? { region1 = var.paired_regions.region1 } : { }
}
module "paired_regions_app" {
source = "./modules/multi-region"
wag_public_ip = length(module.paired_regions_ips) > 0 ? (lookup(module.paired_regions_ips, each.key, "") != "" ? join("/", ["${module.paired_regions_ips[each.key].ip_obj.ip_address}", "32"]) : "" ) : ""
...
for_each = (var.environment == "TEST" || var.environment == "PROD") ? var.paired_regions : { region1 = var.paired_regions.region1 }
}
module "paired_regions_network" {
source = "./modules/network"
wag_public_ip_id = module.paired_regions_ips[each.key].ip_obj.id
application_hostname = module.paired_regions_app[each.key].website_hostname
...
for_each = ( var.environment == "TEST" || var.environment == "PROD") ? { region1 = var.paired_regions.region1 } : { }
}
output "network_outputs" {
value = module.paired_regions_ips
}
output "app_outputs" {
value = module.paired_regions_app
}
新模块
output "ip_obj" {
value = azurerm_public_ip.network_ip
description = "The Public IP address"
}
一些备注:
- 由于
paired_regions_ips
模块与paired_regions_app
模块相比,在for_each
循环上的条件不同,因此我必须在获取时添加一些逻辑后者的输出 - 新模块输出一个公共 IP 对象,以便我可以访问它的 ID(来自
paired_regions_network
模块)和 IP 地址(来自paired_regions_app
模块)
- Because the
paired_regions_ips
module has different conditions on thefor_each
loop when compared to thepaired_regions_app
module, I had to add some logic when fetching the output from the latter - The new module outputs a public IP object, so that I have access to both its ID (from the
paired_regions_network
module) and to the IP address (from thepaired_regions_app
module)
这篇关于两个模块之间的模块组合使用条件 for_each 双向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!