Refer to a var.<name> where <name> is dynamically created?

I'm new to Terraform, PowerShell is my background (years of it, so it's likely impacting how I think about doing Terraform).

I'm trying to make NSGs in Azure from CSV files, following this example so far:

So far I've figured out how to foreach a given CSV containing NSG rules, this file is named bastion.nsg in my example

for rule in csvdecode(file("${path.module}/nsg/bastion.csv")) : rule.name => rule if rule.default == "FALSE" } 

I want to reuse the same bastion.csv for all my environments (dev, test, prod). All environments have their own .tfvars file, where variables exists for defining subnet CIDR and similar. VNets, subnets etc. is separated, so can't hardcode CIDR range unless I want to hardcode 3x CSVs per subnet I want a NSG on.

Where I'm stuck is trying to get the value of a variable from .tfvars dynamically, based on the value from the CSV.

Is it possible to dynamically get a var.<name>, where <name> is created based on cell value of CSV?

My logic so far is having a dynamic expression: If a value from my CSV has a semicolon (length of split by semicolon is greater than 1), then get "var.<csv\_column\_split\_by\_:\_get\_latest\_item\_in\_list>". Else split by comma. But can't really get that to work.

Something like this is my thought, but IntelliJ IDEA + Terraform addon says I'm lost.

length(split(":", each.value.source))>1 ? var.${split(":", each.value.source)[-1]} : split(",", each.value.source) 

Documentation says you reference to var.<name>, but nothing about <name> being dynamic in any way. So I kind of think this won't work.

Hmm, maybe add subnets CIDR ranges to a map, filter it like I filter the "for" thingy?

submitted by /u/olavrb
[link] [comments]

Read more here: https://www.reddit.com/r/Terraform/comments/n4m2jk/refer_to_a_varname_where_name_is_dynamically/

Content Attribution

This content was originally published by /u/olavrb at Terraform, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: