【问题标题】:for_each through a object in Terraform 0.12for_each 通过 Terraform 0.12 中的对象
【发布时间】:2021-03-09 11:30:09
【问题描述】:

我想用下一种方式调用 terraform 模块:

module "database_role" {
 source = "modules/roles"

 project_id = "testid"
 role_name = "testrole"

 actions = {
   action: ["ENABLE_PROFILER", "DROP_DATABASE"]
   database_name: "test_db"
 }

我创建的角色模块定义是:

resource "mongodbatlas_custom_db_role" "custom_role" {
  project_id = var.project_id
  role_name  = var.role_name

  dynamic "actions" {
    for_each = [for item in [var.actions] : item]
      content {
        actions {
          action = lookup(actions.value, "action")

          resources {
            cluster = "false"
            database_name = lookup(actions.value, "database_name")
          }
        }
      }
    }
  }

因此我希望看到正确生成的操作:

 actions {
   action = "ENABLE_PROFILER"
   resources {
     cluster         = "false"
     database_name   = "test_db"
   }
 }


 actions {
   action = "DROP_DATABASE"
   resources {
     cluster         = "false"
     database_name   = "test_db"
   }
 }

我收到错误:给定的值不适合子模块变量“actions”。 我在模块动态资源中做错了什么?谢谢

【问题讨论】:

    标签: terraform terraform-template-file terraform-modules


    【解决方案1】:

    flatten 函数可以在这里使用。

    我在下面actions local var::

    locals {
      actions = {
        action = ["ENABLE_PROFILER", "DROP_DATABASE"]
        database_name = "test_db"
      }
    }
    

    现在,我将根据local.actions["action"] 的大小进行展平,以达到预期的效果。有一次,我得到了扁平化的列表,我会遍历列表来创建动态块。

    resource "mongodbatlas_custom_db_role" "custom_role" {
      project_id = "xxx-xxx"
      role_name  = "yyy-yyy"
    
      dynamic "actions" {
        for_each = flatten([
          for item in range(length(local.actions["action"])): {
            act = local.actions["action"][item]
            db_name = local.actions.database_name
          }
         ])
         content {
           action = actions.value.act
           resources {
             cluster = "false"
             database_name = actions.value.db_name
           }
         }
       }
     }
    

    这将产生我喜欢的所需数量的块::

    Harshas-MBP:mongo harshavmb$ terraform plan
    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    
    
    ------------------------------------------------------------------------
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # mongodbatlas_custom_db_role.custom_role will be created
      + resource "mongodbatlas_custom_db_role" "custom_role" {
          + id         = (known after apply)
          + project_id = "xxx-xxx"
          + role_name  = "yyy-yyy"
    
          + actions {
              + action = "ENABLE_PROFILER"
    
              + resources {
                  + cluster       = false
                  + database_name = "test_db"
                }
            }
          + actions {
              + action = "DROP_DATABASE"
    
              + resources {
                  + cluster       = false
                  + database_name = "test_db"
                }
            }
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    
    ------------------------------------------------------------------------
    
    Note: You didn't specify an "-out" parameter to save this plan, so Terraform
    can't guarantee that exactly these actions will be performed if
    "terraform apply" is subsequently run.
    

    这也可以应用于创建资源。使用嵌套或复杂循环的关键思想是将它们展平为资源或块列表,我们创建并迭代具有唯一索引的展平列表,如上例所示。

    【讨论】:

    • 谢谢,如果变量为空或未设置,如何跳过动态动作?谢谢
    • 只在长度 > 0 时放置一个条件运算符循环。length(local.actions["action"]) > 0 ? for item in range(length(local.actions["action"])): { act = local.actions["action"][item] db_name = local.actions.database_name } : {} 类似这样的东西。语法可能不正确,但我认为您可以尝试
    猜你喜欢
    • 1970-01-01
    • 2020-06-23
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多