Home > Net >  Use for loop to fetch values from csv file using terraform
Use for loop to fetch values from csv file using terraform

Time:12-06

I have a csv file which has few values. How to iterate through it using terraform. Need to use for loop for the same

locals {
    vnetlist = csvdecode(file("./title.csv"))
    datalist = ([for vnets in lookup(data.azurerm_resources.spokes, "resources", []) : lookup(vnets, "name")])
    finopslist = ([for r in vnetlist : r ])
}

This code is giving me the below error. The for loop throws error. PS C:\Users\rmani\Documents\Ramya\Repo\FTest> terraform plan -var-file="subsc2.tfvars" ╷ │ Error: Invalid reference │ │ on main.tf line 9, in locals: │ │ A reference to a resource type must be followed by at least one attribute access, specifying the resource name.

My csv file is like this

enter image description here

datalist gives me the below values regA regB regC

These values should be in the column region instead of us,er,ind

CodePudding user response:

It should be (you forgot local keyword):

finopslist = ([for r in local.vnetlist : r ])
  • Related