github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/hcl2/functions/encoding/csvdecode.mdx (about)

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