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