github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/docs/job-specification/hcl2/functions/encoding/csvdecode.mdx (about) 1 --- 2 layout: docs 3 page_title: csvdecode - Functions - Configuration Language 4 sidebar_title: csvdecode 5 description: The csvdecode function decodes CSV data into a list of maps. 6 --- 7 8 # `csvdecode` Function 9 10 `csvdecode` decodes a string containing CSV-formatted data and produces a 11 list of maps representing that data. 12 13 CSV is _Comma-separated Values_, an encoding format for tabular data. There 14 are many variants of CSV, but this function implements the format defined 15 in [RFC 4180](https://tools.ietf.org/html/rfc4180). 16 17 The first line of the CSV data is interpreted as a "header" row: the values 18 given are used as the keys in the resulting maps. Each subsequent line becomes 19 a single map in the resulting list, matching the keys from the header row 20 with the given values by index. All lines in the file must contain the same 21 number of fields, or this function will produce an error. 22 23 ## Examples 24 25 ```shell-session 26 > csvdecode("a,b,c\n1,2,3\n4,5,6") 27 [ 28 { 29 "a" = "1" 30 "b" = "2" 31 "c" = "3" 32 }, 33 { 34 "a" = "4" 35 "b" = "5" 36 "c" = "6" 37 } 38 ] 39 ``` 40 41 <!--- 42 ## TODO: revamp this section 43 44 ## Use with the `for_each` meta-argument 45 46 You can use the result of `csvdecode` with 47 [the `for_each` meta-argument](https://www.terraform.io/docs/configuration/resources.html#for_each-multiple-resource-instances-defined-by-a-map-or-set-of-strings) 48 to describe a collection of similar objects whose differences are 49 described by the rows in the given CSV file. 50 51 There must be one column in the CSV file that can serve as a unique id for each 52 row, which we can then use as the tracking key for the individual instances in 53 the `for_each` expression. For example: 54 55 ```hcl 56 locals { 57 # We've included this inline to create a complete example, but in practice 58 # this is more likely to be loaded from a file using the "file" function. 59 csv_data = <<-CSV 60 local_id,instance_type,ami 61 foo1,t2.micro,ami-54d2a63b 62 foo2,t2.micro,ami-54d2a63b 63 foo3,t2.micro,ami-54d2a63b 64 bar1,m3.large,ami-54d2a63b 65 CSV 66 67 instances = csvdecode(local.csv_data) 68 } 69 70 resource "aws_instance" "example" { 71 for_each = { for inst in local.instances : inst.local_id => inst } 72 73 instance_type = each.value.instance_type 74 ami = each.value.ami 75 } 76 ``` 77 78 The `for` expression in our `for_each` argument transforms the list produced 79 by `csvdecode` into a map using the `local_id` as a key, which tells 80 Packer to use the `local_id` value to track each instance it creates. 81 Packer will create and manage the following instance addresses: 82 83 - `aws_instance.example["foo1"]` 84 - `aws_instance.example["foo2"]` 85 - `aws_instance.example["foo3"]` 86 - `aws_instance.example["bar1"]` 87 88 If you modify a row in the CSV on a subsequent plan, Packer will interpret 89 that as an update to the existing object as long as the `local_id` value is 90 unchanged. If you add or remove rows from the CSV then Packer will plan to 91 create or destroy associated instances as appropriate. 92 93 If there is no reasonable value you can use as a unique identifier in your CSV 94 then you could instead use 95 [the `count` meta-argument](https://www.terraform.io/docs/configuration/resources.html#count-multiple-resource-instances-by-count) 96 to define an object for each CSV row, with each one identified by its index into 97 the list returned by `csvdecode`. However, in that case any future updates to 98 the CSV may be disruptive if they change the positions of particular objects in 99 the list. We recommend using `for_each` with a unique id column to make 100 behavior more predictable on future changes. 101 -->