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
CodePudding user response:
Terraform is (sadly) not a programming language, so it's not easy (or possible) to convert any pseudo-code to HCL.
Anyway, for your specific case you need to combine two things:
- the equivalent of
i
of your pseudo-code in Terraform would be:count.index
(in case you usecount
) - the if-else-else is probably something like:
(local.a != "" && count.index > 2) ? "option 1" : ((local.a != "" && count.index < 2) ? "option 2" : "option 3" )
(not tested)
Also it might come across as nit-picking, but a tiny remark is "Something to do" in Terraform you rather not declare things to be done (as it's not a procedural programming language), but rather desired state of things.
CodePudding user response:
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:
- Doing something different when
var
is set to the empty string vs. other cases. - 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:
- Terraform's conditional operator
c ? t : f
is equivalent to Python's conditional expressions. - Terraform's
for
expressions are equivalent to Python's list comprehensions.
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.