Storing variables for long expressions

Hi All,

I have an expression that exists in several resources in a module, and I’d like to have that expression exist in a single location, and reference it using some sort of variable (?).

That expression looks like

length(range(count.index)) > 0 ? sum([for i in range(count.index) : i % length(var.subnet_ids)]) : "0"

Is there a way to have this expression stored somewhere so I can reference it in each resource I want to use it in?

Hi @hellopat,

From what you’ve described it sounds like local values would be the best way to solve your problem.

However, since your expression involves count.index you won’t be able to make it into a local value directly, since local values are outside of the context of a particular resource or module and thus don’t have the count object in scope.

Your expression seems incomplete (the conditional expression doesn’t have an “else” arm, starting with a colon, and there’s no closing parenthesis on the sum call) so I’m not able to infer what your goal was here, but one way to extract it into a local value would be to write a for expression which mimics the same effect of the count on whatever resource(s) you are intending to use this in. For example, if you had count = var.example_count then you could write a local value like this:

locals {
  example = [
    for i in range(var.example_count) : (
      // ... whatever expression you want to evaluate once per var.example_count

Then in your resource that has count = var.example_count you can switch to something like this:

resource "example" "example" {
  count = length(local.example)

  # you can use local.example[count.index] here to
  # select the corresponding member from local.example

I hope the above contrived example gives you the building blocks you need to adapt this to your problem. If not, perhaps you could share a more complete example of what you are doing and I can try to adapt it to fit the above pattern.

Hi @apparentlymart

Thanks for the detailed response. I updated the original post to add the rest of the expression.

What I’m trying to do here is get a count of the number of instances that get distributed to each subnet. In my particular case, I have 3 EC2 instances across 2 subnets, and I want the first instance to use subnet A, the second to use subnet B, the third to use subnet A. The expression in the OP is used to track the index for each instance that gets added to a specific subnet (Instances on Subnet {subnet} in the below example).

Instance (index): 0
Subnet: A
Instances on Subnet A (index): 0

Instance (index): 1
Subnet: B
Instances on Subnet B (index): 0

Instance (index): 2
Subnet: A
Instances on Subnet A (index): 1

… if we add 3 more instances …

Instance (index): 3
Subnet: B
Instances on Subnet B (index): 1

Instance (index): 4
Subnet: A
Instances on Subnet A (index): 2

Instance (index): 5
Subnet: B
Instances on Subnet B (index): 2

… and so on …

I’ll have a look and see if I can figure out how to adapt this expression as a local variable.