github.com/markdia/terraform@v0.5.1-0.20150508012022-f1ae920aa970/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 * `length(list)` - Returns a number of members in a given list 97 or a number of characters in a given string. 98 * `${length(split(",", "a,b,c"))}` = 3 99 * `${length("a,b,c")}` = 5 100 101 * `lookup(map, key)` - Performs a dynamic lookup into a mapping 102 variable. The `map` parameter should be another variable, such 103 as `var.amis`. 104 105 * `replace(string, search, replace)` - Does a search and replace on the 106 given string. All instances of `search` are replaced with the value 107 of `replace`. If `search` is wrapped in forward slashes, it is treated 108 as a regular expression. If using a regular expression, `replace` 109 can reference subcaptures in the regular expression by using `$n` where 110 `n` is the index or name of the subcapture. If using a regular expression, 111 the syntax conforms to the [re2 regular expression syntax](https://code.google.com/p/re2/wiki/Syntax). 112 113 * `split(delim, string)` - Splits the string previously created by `join` 114 back into a list. This is useful for pushing lists through module 115 outputs since they currently only support string values. 116 Example: `split(",", module.amod.server_ids)` 117 118 ## Templates 119 120 Long strings can be managed using templates. Templates 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. 121 122 A template resource looks like: 123 124 ``` 125 resource "template_file" "example" { 126 filename = "template.txt" 127 vars { 128 hello = "goodnight" 129 world = "moon" 130 } 131 } 132 133 output "rendered" { 134 value = "${template_file.example.rendered}" 135 } 136 ``` 137 138 Assuming `template.txt` looks like this: 139 140 ``` 141 ${hello} ${world}! 142 ``` 143 144 Then the rendered value would be `goodnight moon!`. 145 146 You may use any of the built-in functions in your template.