首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套条件模块中的Terraform条件输出变量

嵌套条件模块中的Terraform条件输出变量
EN

Stack Overflow用户
提问于 2022-08-05 17:19:47
回答 1查看 332关注 0票数 0

我有这样一个模块,需要将模块的变量输出到另一个模块中。

代码语言:javascript
复制
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 = []
   }

第二个模块

代码语言:javascript
复制
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 }"

我得到了以下错误

代码语言:javascript
复制
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的代码

代码语言:javascript
复制
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
}
EN

回答 1

Stack Overflow用户

发布于 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 )创建的资源的值,除非键不是:

您希望拥有的输出(如果创建了资源)将是:

代码语言:javascript
复制
output "s3_list" {
  value = values(google_storage_bucket.storage_bucket)[*].name
}

values内置函数1将返回一个值列表,因此length函数应该可以工作,但是您正在测试它的值应该从以下位置更改:

代码语言:javascript
复制
"${length(module.bucket.s3_list.0.name) > 0 ? lookup(module.bucket.s3_list.0.name, "func_1_name ", "") : null }"

代码语言:javascript
复制
length(module.bucket.s3_list) > 0 ? contains(module.bucket.s3_list, "func_1_name") ? "func_1_name" : "" : null

这将始终计算为false,因为没有一个桶有名称func_*_name,其中通配符*替换了数字125。因此,您必须选择: a)将桶命名为相同的方式,即不应该将name = bucket-1设置为name = function_1_name或b)更改条件以检查bucket-1bucket-2bucket-5

请确保您了解for_each 2是如何工作的,列表3是如何工作的,以及如何获取资源4的参数和属性值。

1

2

3

4

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73253334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档