Populating a terraform map variable from a CSV file

How to define a value for map variable in a CSV file.
Getting following error :
“Inappropriate value for attribute “tags”: map of string required.”
CSV file is like:

srl_no,create_in_rg,proximity_placement_group_name,tag_ppg,aset_name,tag_aset
1,hub-vnet-rg,test-hub-pg,division = 'XYZ' desc = 'test-ppg',test-hub-aset,division = 'ABC' desc = 'test-aset'

Thanks.

Hi @shareKnowledge,

It’s hard to know what exactly is going on here because you didn’t share your configuration, but it looks like you are intending to include a map of tags inside the tag_ppg field of your CSV file.

The CSV format can only encode strings, so the result of decoding the row you showed here will be the following mapping:

{
  srl_no = "1"
  create_in_rg = "hub-vnet-rg"
  proximity_placement_group_name = "test-hub-pg"
  tag_ppg = "division = 'XYZ' desc = 'test-ppg'"
  aset_name = "test-hub-aset"
  tag_aset = "division = 'ABC' desc = 'test-aset'"
}

If you want to use tag_ppg and tag_aset as maps then you’ll need to write an expression to parse those strings into a map, because Terraform otherwise doesn’t know what syntax they are written in.

If you’re able to change the input format to use JSON instead of whatever delimited format this is then you could use Terraform’s jsondecode function to decode into a map automatically. If you aren’t able to change the input format and need to parse whatever this key = 'value' format is then unfortunately you’d need to parse it directly in the Terraform language, perhaps using the regexall function, but I would consider that to be a last resort because the result is likely to be hard to read and maintain.

Another option would be to stop using CSV altogether and use a format like either JSON or YAML for the entire file. JSON and YAML both have built-in support for mappings, so you wouldn’t need to write anything special to decode a JSON version of that CSV file using normal JSON object syntax:

{
  "srl_no": "1",
  "create_in_rg": "hub-vnet-rg",
  "proximity_placement_group_name": "test-hub-pg",
  "tag_ppg": {
    "division": 'XYZ',
    "desc": "test-ppg"
  },
  "aset_name": "test-hub-aset",
  "tag_aset": {
    "division": "ABC",
    "desc": "test-aset"
  }
}