I'm trying to access terraform variable data using CSV file, have creating resource group and the name of resource group are added into the CSV file and trying to access into code.
Here is the code :
locals {
Resource_groupname = csvdecode(file("${path.module}/onkar.csv"))
}
//Create a resource group
resource "azurerm_resource_group" "Customer11" {
count = length(local.Resource_groupname)
name = local.Resource_groupname[count.index].resourcegroup_name
location = "North europe"
}
I get the following error :
on admin.tf line 15, in resource "azurerm_resource_group"
"Customer11": 15: name =
local.Resource_groupname[count.index].resourcegroup_name
|----------------
| local.Resource_groupname is list of object with 1 element
This object does not have an attribute named "resourcegroup_name".
Updated
Updated Code:
locals {
Resource_groupname = csvdecode(file("./test.csv"))
}
resource "azurerm_resource_group" "Customer11" {
count = length(local.Resource_groupname)
name = local.Resource_groupname[count.index].group_names
location = "North europe"
}
New Updated
locals {
Resource_groupname = csvdecode(file("./test.csv"))
}
resource "azurerm_resource_group" "Customer11" {
count = length(local.Resource_groupname)
name = local.Resource_groupname[count.index].Resource_groupname
location = "North europe"
}
New update CSV file and output
To load the input from the CSV file, I assume your CSV file with only one line and it looks like this:
test1,test2,test3
Then you can load and use them from the CSV file as below:
locals {
group_names = split(",", file("./test.csv"))
}
resource "azurerm_resource_group" "Customer11" {
count = length(local.group_names)
name = local.group_names[count.index]
location = "North europe"
}
If you use the CSV file like this:
resource_group_name
test1
test2
test3
Then the terraform code should change into this:
locals {
group_names = csvdecode(file("./test.csv"))
}
resource "azurerm_resource_group" "main" {
count = length(local.group_names)
name = local.group_names[count.index].resource_group_name
location = "East US"
}
Update:
With the CSV file you provide, you need to change the resource group name like this:
resource "azurerm_resource_group" "Customer11" {
count = length(local.Resource_groupname)
name = local.Resource_groupname[count.index].group_names # here is the change
location = "North europe"
}
Here is the screenshot of the CSV file:
You can try to output the local.Resource_groupname
to see what it looks like when you load the data from the CSV file.
Update2
I really do not understand why you cannot figure out how to do it. Here is the screenshot of my test for all the things, hope you solve it:
The content of the Terraform file and the CSV.
The Terraform plan: