loopsfor-loopif-statementterraform

How to use if/else statement in terraform for loop


Is it possible to do this in terraform?


for i in range(10):
    if var != "" and i > 2:
        # Something to do
    elif var != "" and i < 2:
        # Something to do
    else:
        # Something else to do

What i want to achieve is to create list but i need if/else statement in for loop. What i have achieved so far is:


for i in range(10):
    if var != "":
        # Something to do


Solution

  • It's hard to answer this question because it seems like you already decided on a solution to a problem but you haven't actually stated what the underlying problem is. However, I will try to answer directly what you asked, nonetheless.

    When thinking about approaching problems in Terraform it's best to think from the perspective of constructing values from expressions using other values, rather than writing imperative statements to describe how to construct those. In this case, I would try to pick your problem into two parts:

    1. Doing something different when var is set to the empty string vs. other cases.
    2. Doing something different when constructing the first two elements of a list, but only in the non-empty string case.

    The first of those sounds like a conditional expression, because you want to choose between two possible outcomes based on a condition:

    locals {
      example = (
        var != "" ?
        (expression for normal case) :
        (expression for empty case)
      )
    }
    

    You haven't included details about what ought to happen in the "empty case", but since you suggested you would use a loop in an imperative language I assume that your problem would best map to a for expression in Terraform.

    locals {
      example = (
        var != "" ?
        (expression for normal case) :
        [ for i in range(10) : (something based on i) ]
      )
    }
    

    The "normal case" has the extra detail of doing something different based on whether the index is less than two:

    locals {
      example = (
        var != "" ?
        [
          for i in range(10) : (
            i < 2 ?
            (something based on i) :
            (something else based on i)
          )
        ]
        [ for i in range(10) : (something based on i) ]
      )
    }
    

    (Your original question used i < 2 and i > 2, and my answer above admittedly uses i < 2 and i >= 2 instead; I made an assumption that this interpretation was more likely, but this answer won't work if you really do need to do something special when i == 2 that's different to when it's less than or greater than.)

    I can't take this answer any further without more detail about the underlying problem you're trying to solve, but since you seem to be coming from familiarity with Python I do have two general mappings for you that might help you translate from Python-ish approaches to Terraform-ish approaches:

    In both cases these constructs have similar capabilities but different syntax. If you feel more comfortable exploring in Python first then I'd suggest using these particular Python constructs to build your solution and then you should be able to translate the result to an equivalent Terraform expression.