github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/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 ## Available Variables 23 24 **To reference user variables**, use the `var.` prefix followed by the 25 variable name. For example, `${var.foo}` will interpolate the 26 `foo` variable value. If the variable is a mapping, then you 27 can reference static keys in the map with the syntax 28 `var.MAP.KEY`. For example, `${var.amis.us-east-1}` would 29 get the value of the `us-east-1` key within the `amis` variable 30 that is a mapping. 31 32 **To reference attributes of your own resource**, the syntax is 33 `self.ATTRIBUTE`. For example `${self.private_ip_address}` will 34 interpolate that resource's private IP address. Note that this is 35 only allowed/valid within provisioners. 36 37 **To reference attributes of other resources**, the syntax is 38 `TYPE.NAME.ATTRIBUTE`. For example, `${aws_instance.web.id}` 39 will interpolate the ID attribute from the "aws\_instance" 40 resource named "web". If the resource has a `count` attribute set, 41 you can access individual attributes with a zero-based index, such 42 as `${aws_instance.web.0.id}`. You can also use the splat syntax 43 to get a list of all the attributes: `${aws_instance.web.*.id}`. 44 This is documented in more detail in the 45 [resource configuration page](/docs/configuration/resources.html). 46 47 **To reference outputs from a module**, the syntax is 48 `MODULE.NAME.OUTPUT`. For example `${module.foo.bar}` will 49 interpolate the "bar" output from the "foo" 50 [module](/docs/modules/index.html). 51 52 **To reference count information**, the syntax is `count.FIELD`. 53 For example, `${count.index}` will interpolate the current index 54 in a multi-count resource. For more information on count, see the 55 resource configuration page. 56 57 **To reference path information**, the syntax is `path.TYPE`. 58 TYPE can be `cwd`, `module`, or `root`. `cwd` will interpolate the 59 cwd. `module` will interpolate the path to the current module. `root` 60 will interpolate the path of the root module. In general, you probably 61 want the `path.module` variable. 62 63 ## Built-in Functions 64 65 Terraform ships with built-in functions. Functions are called with 66 the syntax `name(arg, arg2, ...)`. For example, 67 to read a file: `${file("path.txt")}`. The built-in functions 68 are documented below. 69 70 The supported built-in functions are: 71 72 * `concat(args...)` - Concatenates the values of multiple arguments into 73 a single string. 74 75 * `element(list, index)` - Returns a single element from a list 76 at the given index. If the index is greater than the number of 77 elements, this function will wrap using a standard mod algorithm. 78 A list is only possible with splat variables from resources with 79 a count greater than one. 80 Example: `element(aws_subnet.foo.*.id, count.index)` 81 82 * `file(path)` - Reads the contents of a file into the string. Variables 83 in this file are _not_ interpolated. The contents of the file are 84 read as-is. 85 86 * `format(format, args...)` - Formats a string according to the given 87 format. The syntax for the format is standard `sprintf` syntax. 88 Good documentation for the syntax can be [found here](http://golang.org/pkg/fmt/). 89 Example to zero-prefix a count, used commonly for naming servers: 90 `format("web-%03d", count.index+1)`. 91 92 * `join(delim, list)` - Joins the list with the delimiter. A list is 93 only possible with splat variables from resources with a count 94 greater than one. Example: `join(",", aws_instance.foo.*.id)` 95 96 * `lookup(map, key)` - Performs a dynamic lookup into a mapping 97 variable. The `map` parameter should be another variable, such 98 as `var.amis`. 99 100 * `replace(string, search, replace)` - Does a search and replace on the 101 given string. All instances of `search` are replaced with the value 102 of `replace`. If `search` is wrapped in forward slashes, it is treated 103 as a regular expression. If using a regular expression, `replace` 104 can reference subcaptures in the regular expression by using `$n` where 105 `n` is the index or name of the subcapture. If using a regular expression, 106 the syntax conforms to the [re2 regular expression syntax](https://code.google.com/p/re2/wiki/Syntax). 107 108 * `split(delim, string)` - Splits the string previously created by `join` 109 back into a list. This is useful for pushing lists through module 110 outputs since they currently only support string values. 111 Example: `split(",", module.amod.server_ids)`