terraformconfluent-cloud

Iterating List of lists getting null value in terraform


Input Variable

  appr = {


    "product1" = {WO = ["6470"], RO = ["6471","5538"]},
    "product2" = {RO = ["6472"]}
  }

Error:


│ Error: Iteration over null value
│
│   on ../../modules/sa_pc/main.tf line 15, in locals:
│   14:       for co, coo in clsan : [
│   15:         for applo in coo : [
│   16:           for op in ( co == "RO" ? ["READ","DESCRIBE"] : ["WRITE"] ) :  {
│   17:              oper = op
│   18:              
│   19:              appid_lo = applo
│   20:              opic-name = opi
│   21:              process = co
│   22:             }
│   23:         ]
│   24:       ]
│
│ A null value cannot be used as the collection in a 'for' expression.

Throwing error as null value @ coo, when tried on terraform console getting desire output but failing with terraform plan.

Where am going wrong ?

Note: coo Is iterating lists using for loop

Tried this

locals {

  acl_out = flatten([
    for opi, clsan in var.appr : [
      for co, coo in clsan : [
        for applo in coo : [
           for op in ( co == "RO" ? ["READ","DESCRIBE"] : ["WRITE"]) : {
             oper = op
             appid_lo = applo
             opic-name = opi
             process = co
             
           }

        ]
      ]

    ]
  ])

}

Solution

  • Is this a replica of This question ?

    Anyways, as I said in the other question:

    Check if it is null before running the next for

    locals {
      acl_out = flatten([
        for opi, clsan in var.appr : [
          for co, coo in clsan : [
            coo == null ? [] : [
            for applo in coo : [
               for op in ( co == "RO" ? ["READ","DESCRIBE"] : ["WRITE"]) : {
                 oper = op
                 appid_lo = applo
                 opic-name = opi
                 process = co
               }
            ]
          ]]
        ]
      ])
    }
    

    Output for acl_out

    acl_out = [
          + {
              + appid_lo  = "6471"
              + oper      = "READ"
              + opic-name = "product1"
              + process   = "RO"
            },
          + {
              + appid_lo  = "6471"
              + oper      = "DESCRIBE"
              + opic-name = "product1"
              + process   = "RO"
            },
          + {
              + appid_lo  = "5538"
              + oper      = "READ"
              + opic-name = "product1"
              + process   = "RO"
            },
          + {
              + appid_lo  = "5538"
              + oper      = "DESCRIBE"
              + opic-name = "product1"
              + process   = "RO"
            },
          + {
              + appid_lo  = "6470"
              + oper      = "WRITE"
              + opic-name = "product1"
              + process   = "WO"
            },
          + {
              + appid_lo  = "6472"
              + oper      = "READ"
              + opic-name = "product2"
              + process   = "RO"
            },
          + {
              + appid_lo  = "6472"
              + oper      = "DESCRIBE"
              + opic-name = "product2"
              + process   = "RO"
            },
          + {
              + appid_lo  = "6474"
              + oper      = "READ"
              + opic-name = "product3"
              + process   = "RO"
            },
          + {
              + appid_lo  = "6474"
              + oper      = "DESCRIBE"
              + opic-name = "product3"
              + process   = "RO"
            },
          + {
              + appid_lo  = "6473"
              + oper      = "WRITE"
              + opic-name = "product3"
              + process   = "WO"
            },
        ]
    

    Cheers!