github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration/functions/csvdecode.html.md (about)

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