我有这样一个模块,需要将模块的变量输出到另一个模块中。
module "bucket" {
source = "../modules/storage_bucket"
s3_list = {
bucket_1 = {
name = bucket-1
bucket_policy_only = false
role_entity = [],
lifecycle_rule = []
},
bucket_2 = {
name = bucket-2
bucket_policy_only = false
role_entity = [],
lifecycle_rule = []
},
bucket_3 = {
name = bucket-3
bucket_policy_only = false
role_entity = [],
lifecycle_rule = []
},
bucket_4 = {
name = bucket-4
bucket_policy_only = false
role_entity = [],
lifecycle_rule = []
},
bucket_5 = {
name = bucket-5
bucket_policy_only = false
role_entity = [],
lifecycle_rule = []
}第二个模块
module "bucket_func" {
source = "../modules/functions"
func_1_name = "${length(module.bucket.s3_list.0.name) > 0 ? lookup(module.bucket.s3_list.0.name, "func_1_name ", "") : null }"
func_2_name = "${length(module.bucket.s3_list.0.name) > 0 ? lookup(module.bucket.s3_list.0.name, "func_2_name ", "") : null }"
func_5_name = "${length(module.bucket.s3_list.0.name) > 0 ? lookup(module.bucket.s3_list.0.name, "func_5_name ", "") : null }"我得到了以下错误
Error: Unsupported attribute
on main.tf line 164, in module "functions":
func_1_name = "$***length(module.bucket.s3_list.0.name) > 0 ? lookup(module.bucket.s3_list.0.name, "func_1_name", "") : null ***"
module.bucket is a object, known only after apply
This object does not have an attribute named "s3_list".来自模块storage_bucket的代码
resource "google_storage_bucket" "storage_bucket" {
for_each = { for key, restr in var.s3_list : key => restr if restr.bucket_policy_only }
name = each.value.name
storage_class = var.storage_class
location = var.location
force_destroy = var.force_destroy
uniform_bucket_level_access = each.value.bucket_policy_only
}发布于 2022-08-06 06:38:29
您正在使用的代码有几个问题:
您还没有在module.bucket.s3_list.0.name模块
for_each不会创建任何内容,因为条件if restr.bucket_policy_only永远不会是true,因为所有bucket_policy_only值都设置为false,所以即使定义了H 210H 111也可能无法工作输出,即使定义了输出,也不能引用使用编号键( for_each在示例中使用编号键(0 )创建的资源的值,除非键不是:您希望拥有的输出(如果创建了资源)将是:
output "s3_list" {
value = values(google_storage_bucket.storage_bucket)[*].name
}values内置函数1将返回一个值列表,因此length函数应该可以工作,但是您正在测试它的值应该从以下位置更改:
"${length(module.bucket.s3_list.0.name) > 0 ? lookup(module.bucket.s3_list.0.name, "func_1_name ", "") : null }"至
length(module.bucket.s3_list) > 0 ? contains(module.bucket.s3_list, "func_1_name") ? "func_1_name" : "" : null这将始终计算为false,因为没有一个桶有名称func_*_name,其中通配符*替换了数字1、2和5。因此,您必须选择: a)将桶命名为相同的方式,即不应该将name = bucket-1设置为name = function_1_name或b)更改条件以检查bucket-1、bucket-2和bucket-5
请确保您了解for_each 2是如何工作的,列表3是如何工作的,以及如何获取资源4的参数和属性值。
https://stackoverflow.com/questions/73253334
复制相似问题