1

I'm trying to use terraform variable data (CSV file) to create a resource group and the name of the resource group are added into the CSV file.

I'm currently experiencing the below error.

provider "azurerm" {
    features{}
}

locals {
      resource_groupname = csvdecode(file("./test.csv"))
    }

    resource "azurerm_resource_group" "Main" {
      count    = length(locals.resource_groupname)
      name     =  locals.resource_groupname[count.index].groupname   
      location = "North europe"
    } 

Error Message

 Error: Reference to undeclared resource
│
│   on testvariable.tf line 10, in resource "azurerm_resource_group" "Customer11":
│   10:   count    = length(locals.groupname)
│
│ A managed resource "locals" "groupname" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│   on testvariable.tf line 11, in resource "azurerm_resource_group" "Customer11":
│   11:   name     = data.locals.groupname[count.index].groupname
│
│ A data resource "locals" "groupname" has not been declared in the root module.
╵

Updated Error Messgae

╷
│ Error: Reference to undeclared resource
│
│   on testtf.tf line 10, in resource "azurerm_resource_group" "Main":
│   10:       count    = length(locals.resource_groupname)
│
│ A managed resource "locals" "resource_groupname" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│   on testtf.tf line 11, in resource "azurerm_resource_group" "Main":
│   11:       name     =  locals.resource_groupname[count.index].groupname
│
│ A managed resource "locals" "resource_groupname" has not been declared in the root module.
James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

0

Your code should be (assuming this time you posted correct code):

    resource "azurerm_resource_group" "Main" {
      count    = length(local.resource_groupname)
      name     =  local.resource_groupname[count.index].groupname   
      location = "North europe"
    }

Since ./test.csv is not shown its difficult to speculate on its content and the use in your code.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • this is the link to the content of the ./test.csv https://drive.google.com/file/d/1ituKDzaMVXnyynkjLBZRzMdWK9tnkL14/view?usp=sharing – Ogbebor Akpesiri Jun 19 '21 at 11:31
  • tried the above code got this error Error: Unsupported attribute │ │ on testtf.tf line 11, in resource "azurerm_resource_group" "Main": │ 11: name = local.resource_groupname[count.index].groupname │ ├──────────────── │ │ count.index is a number, known only after apply │ │ local.resource_groupname is list of object with 3 elements │ │ This object does not have an attribute named "groupname". – Ogbebor Akpesiri Jun 19 '21 at 11:33
  • @OgbeborAkpesiri It's a new issue, thus I would suggest making a new question with example CSV file. The original error posted in your question has be successfully addressed. – Marcin Jun 19 '21 at 12:24
  • @OgbeborAkpesiri To accept you have to press "tick" button under up/down votes of the answer. Thanks. – Marcin Jun 19 '21 at 22:51
0

Suppose your CSV file has headers like name, location, ABC, XXY, CDF, your

then you use for_each as well here

locals {
      resource_groupname = csvdecode(file("./test.csv"))
}

resource "azurerm_resource_group" "Main" {
    for_each = { for inst in locals.resource_groupname) : inst.location=> inst 
    }
      
    name = each.value.name
    location =  each.value.location
} 
Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73