Search code examples
terraformterraform-provider-azureterraform0.12+terraform-template-fileterraform-provider-openstack

Experiencing an error when try to output content of a csv file using terraform


I'm trying to use terraform variable data (CSV file) to create a resource group and the name of the resource group is added into the CSV file. I'm currently experiencing the below 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".

Code

provider "azurerm" {
    features{}
}

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

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

./test.csv content

https://drive.google.com/file/d/1ituKDzaMVXnyynkjLBZRzMdWK9tnkL14/view?usp=sharing


Solution

  • I think the file you provided has UTF BOM (Byte Order Mark) bytes that cause TF and/or Azure to choke.

    I recreated the csv file as plain ascii and your HCL worked ok

    I found out about the extra characters by using terraform console. It is a very simple and quick way to troubleshoot TF errors.

    I used this really basic .tf file to check the cvsdecode() behavior. (test0.csv below is your original file and test.csv my created from scratch text file):

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

    Run terraform console and inspect the local variables. Note the BOM characters before "groupname" (test0.csv):

    $ terraform console
    > local.resource_groupname0
    tolist([
      {
        "\ufeffgroupname" = "test11"
      },
      {
        "\ufeffgroupname" = "test12"
      },
      {
        "\ufeffgroupname" = "test13"
      },
    ])
    > local.resource_groupname
    tolist([
      {
        "groupname" = "test11"
      },
      {
        "groupname" = "test12"
      },
      {
        "groupname" = "test13"
      },
    ])
    
    

    Also using the unix file command:

    ## Your file
    $ file test0.csv
    test0.csv: UTF-8 Unicode (with BOM) text, with CRLF line terminators
    
    ## Created by hand with text editor
    $ file test.csv
    test.csv: ASCII text