使用 terraform 输出 CSV 文件

Output CSV file using terraform

我正在尝试使用 terraform 变量数据(CSV 文件)创建资源组,并且资源组的名称已添加到 CSV 文件中。

我目前遇到以下错误。

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: 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.
╵

已更新错误消息

╷
│ 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.

您的代码应该是(假设这次您发布了正确的代码):

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

由于未显示 ./test.csv,因此很难推测其内容和在您的代码中的使用。

假设您的 CSV 文件有 headers 姓名、地点、ABC、XXY、CDF、您的

那你在这里也用for_each

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
}