github.com/hugorut/terraform@v1.1.3/website/docs/language/functions/csvdecode.mdx (about)

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