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