-1

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
             
           }

        ]
      ]

    ]
  ])

}
Nabob
  • 33
  • 6
  • 1
    The code you provided works perfectly fine and does not throw any errors. It means that your actual code that you run is different then in the question. – Marcin Dec 07 '22 at 06:52
  • main code has same code, when i comment line 15, then it never throws errors. But code is incomplete without iteration in "coo". There is any other way to achieve this ? – Nabob Dec 07 '22 at 08:23
  • Its unclear what you do? So your error is from a different code that you posted in the question? – Marcin Dec 07 '22 at 08:26
  • Issue resolved, corrected variable types – Nabob Dec 09 '22 at 17:06

1 Answers1

1

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!

Leo
  • 428
  • 3
  • 11