github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/website/source/docs/configuration/interpolation.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Interpolation Syntax"
     4  sidebar_current: "docs-config-interpolation"
     5  description: |-
     6    Embedded within strings in Terraform, whether you're using the Terraform syntax or JSON syntax, you can interpolate other values into strings. These interpolations are wrapped in `${}`, such as `${var.foo}`.
     7  ---
     8  
     9  # Interpolation Syntax
    10  
    11  Embedded within strings in Terraform, whether you're using the
    12  Terraform syntax or JSON syntax, you can interpolate other values
    13  into strings. These interpolations are wrapped in `${}`, such as
    14  `${var.foo}`.
    15  
    16  The interpolation syntax is powerful and allows you to reference
    17  variables, attributes of resources, call functions, etc.
    18  
    19  You can also perform simple math in interpolations, allowing
    20  you to write expressions such as `${count.index+1}`.
    21  
    22  You can escape interpolation with double dollar signs: `$${foo}`
    23  will be rendered as a literal `${foo}`.
    24  
    25  ## Available Variables
    26  
    27  **To reference user variables**, use the `var.` prefix followed by the
    28  variable name. For example, `${var.foo}` will interpolate the
    29  `foo` variable value. If the variable is a mapping, then you
    30  can reference static keys in the map with the syntax
    31  `var.MAP.KEY`. For example, `${var.amis.us-east-1}` would
    32  get the value of the `us-east-1` key within the `amis` variable
    33  that is a mapping.
    34  
    35  **To reference attributes of your own resource**, the syntax is
    36  `self.ATTRIBUTE`. For example `${self.private_ip_address}` will
    37  interpolate that resource's private IP address. Note that this is
    38  only allowed/valid within provisioners.
    39  
    40  **To reference attributes of other resources**, the syntax is
    41  `TYPE.NAME.ATTRIBUTE`. For example, `${aws_instance.web.id}`
    42  will interpolate the ID attribute from the "aws\_instance"
    43  resource named "web". If the resource has a `count` attribute set,
    44  you can access individual attributes with a zero-based index, such
    45  as `${aws_instance.web.0.id}`. You can also use the splat syntax
    46  to get a list of all the attributes: `${aws_instance.web.*.id}`.
    47  This is documented in more detail in the
    48  [resource configuration page](/docs/configuration/resources.html).
    49  
    50  **To reference outputs from a module**, the syntax is
    51  `MODULE.NAME.OUTPUT`. For example `${module.foo.bar}` will
    52  interpolate the "bar" output from the "foo"
    53  [module](/docs/modules/index.html).
    54  
    55  **To reference count information**, the syntax is `count.FIELD`.
    56  For example, `${count.index}` will interpolate the current index
    57  in a multi-count resource. For more information on count, see the
    58  resource configuration page.
    59  
    60  **To reference path information**, the syntax is `path.TYPE`.
    61  TYPE can be `cwd`, `module`, or `root`. `cwd` will interpolate the
    62  cwd. `module` will interpolate the path to the current module. `root`
    63  will interpolate the path of the root module. In general, you probably
    64  want the `path.module` variable.
    65  
    66  ## Built-in Functions
    67  
    68  Terraform ships with built-in functions. Functions are called with
    69  the syntax `name(arg, arg2, ...)`. For example,
    70  to read a file: `${file("path.txt")}`. The built-in functions
    71  are documented below.
    72  
    73  The supported built-in functions are:
    74  
    75    * `concat(args...)` - Concatenates the values of multiple arguments into
    76        a single string.
    77  
    78    * `element(list, index)` - Returns a single element from a list
    79        at the given index. If the index is greater than the number of
    80        elements, this function will wrap using a standard mod algorithm.
    81        A list is only possible with splat variables from resources with
    82        a count greater than one.
    83        Example: `element(aws_subnet.foo.*.id, count.index)`
    84  
    85    * `file(path)` - Reads the contents of a file into the string. Variables
    86        in this file are _not_ interpolated. The contents of the file are
    87        read as-is.
    88  
    89    * `format(format, args...)` - Formats a string according to the given
    90        format. The syntax for the format is standard `sprintf` syntax.
    91        Good documentation for the syntax can be [found here](http://golang.org/pkg/fmt/).
    92        Example to zero-prefix a count, used commonly for naming servers:
    93        `format("web-%03d", count.index+1)`.
    94  
    95    * `formatlist(format, args...)` - Formats each element of a list
    96        according to the given format, similarly to `format`, and returns a list.
    97        Non-list arguments are repeated for each list element.
    98        For example, to convert a list of DNS addresses to a list of URLs, you might use:
    99        `formatlist("https://%s:%s/", aws_instance.foo.*.public_dns, var.port)`.
   100        If multiple args are lists, and they have the same number of elements, then the formatting is applied to the elements of the lists in parallel.
   101        Example:
   102        `formatlist("instance %v has private ip %v", aws_instance.foo.*.id, aws_instance.foo.*.private_ip)`.
   103        Passing lists with different lengths to formatlist results in an error.
   104  
   105    * `join(delim, list)` - Joins the list with the delimiter. A list is
   106        only possible with splat variables from resources with a count
   107        greater than one. Example: `join(",", aws_instance.foo.*.id)`
   108  
   109    * `length(list)` - Returns a number of members in a given list
   110        or a number of characters in a given string.
   111        * `${length(split(",", "a,b,c"))}` = 3
   112        * `${length("a,b,c")}` = 5
   113  
   114    * `lookup(map, key)` - Performs a dynamic lookup into a mapping
   115        variable. The `map` parameter should be another variable, such
   116        as `var.amis`.
   117  
   118    * `replace(string, search, replace)` - Does a search and replace on the
   119        given string. All instances of `search` are replaced with the value
   120        of `replace`. If `search` is wrapped in forward slashes, it is treated
   121        as a regular expression. If using a regular expression, `replace`
   122        can reference subcaptures in the regular expression by using `$n` where
   123        `n` is the index or name of the subcapture. If using a regular expression,
   124        the syntax conforms to the [re2 regular expression syntax](https://code.google.com/p/re2/wiki/Syntax).
   125  
   126    * `split(delim, string)` - Splits the string previously created by `join`
   127        back into a list. This is useful for pushing lists through module
   128        outputs since they currently only support string values. Depending on the
   129        use, the string this is being performed within may need to be wrapped
   130        in brackets to indicate that the output is actually a list, e.g.
   131        `a_resource_param = ["${split(",", var.CSV_STRING)}"]`.
   132        Example: `split(",", module.amod.server_ids)`
   133  
   134  ## Templates
   135  
   136  Long strings can be managed using templates. [Templates](/docs/providers/template/index.html) are [resources](/docs/configuration/resources.html) defined by a filename and some variables to use during interpolation. They have a computed `rendered` attribute containing the result.
   137  
   138  A template resource looks like:
   139  
   140  ```
   141  resource "template_file" "example" {
   142      filename = "template.txt"
   143      vars {
   144          hello = "goodnight"
   145          world = "moon"
   146      }
   147  }
   148  
   149  output "rendered" {
   150      value = "${template_file.example.rendered}"
   151  }
   152  ```
   153  
   154  Assuming `template.txt` looks like this:
   155  
   156  ```
   157  ${hello} ${world}!
   158  ```
   159  
   160  Then the rendered value would be `goodnight moon!`.
   161  
   162  You may use any of the built-in functions in your template.
   163  
   164  
   165  ### Using Templates with Count
   166  
   167  Here is an example that combines the capabilities of templates with the interpolation
   168  from `count` to give us a parametized template, unique to each resource instance:
   169  
   170  ```
   171  variable "count" {
   172    default = 2
   173  }
   174  
   175  variable "hostnames" {
   176    default = {
   177      "0" = "example1.org"
   178      "1" = "example2.net"
   179    }
   180  }
   181  
   182  resource "template_file" "web_init" {
   183    // here we expand multiple template_files - the same number as we have instances
   184    count = "${var.count}"
   185    filename = "templates/web_init.tpl"
   186    vars {
   187      // that gives us access to use count.index to do the lookup
   188      hostname = "${lookup(var.hostnames, count.index)}"
   189    }
   190  }
   191  
   192  resource "aws_instance" "web" {
   193    // ...
   194    count = "${var.count}"
   195    // here we link each web instance to the proper template_file
   196    user_data = "${element(template_file.web_init.*.rendered, count.index)}"
   197  }
   198  ```
   199  
   200  With this, we will build a list of `template_file.web_init` resources which we can
   201  use in combination with our list of `aws_instance.web` resources.