|
| 1 | +--- |
| 2 | +layout: "docs" |
| 3 | +page_title: "Interpolation Syntax" |
| 4 | +sidebar_current: "docs-config-interpolation" |
| 5 | +--- |
| 6 | + |
| 7 | +# Interpolation Syntax |
| 8 | + |
| 9 | +Embedded within strings in Terraform, whether you're using the |
| 10 | +Terraform syntax or JSON syntax, you can interpolate other values |
| 11 | +into strings. These interpolations are wrapped in `${}`, such as |
| 12 | +`${var.foo}`. |
| 13 | + |
| 14 | +The interpolation syntax is powerful and allows you to reference |
| 15 | +variables, attributes of resources, call functions, etc. |
| 16 | + |
| 17 | +To reference variables, use the `var.` prefix followed by the |
| 18 | +variable name. For example, `${var.foo}` will interpolate the |
| 19 | +`foo` variable value. If the variable is a mapping, then you |
| 20 | +can reference static keys in the map with the syntax |
| 21 | +`var.MAP.KEY`. For example, `${var.amis.us-east-1}` would |
| 22 | +get the value of the `us-east-1` key within the `amis` variable |
| 23 | +that is a mapping. |
| 24 | + |
| 25 | +To reference attributes of other resources, the syntax is |
| 26 | +`TYPE.NAME.ATTRIBUTE`. For example, `${aws_instance.web.id}` |
| 27 | +will interpolate the ID attribute from the "aws\_instance" |
| 28 | +resource named "web". |
| 29 | + |
| 30 | +Finally, Terraform ships with built-in functions. Functions |
| 31 | +are called with the syntax `name(arg, arg2, ...)`. For example, |
| 32 | +to read a file: `${file("path.txt")}`. The built-in functions |
| 33 | +are documented below. |
| 34 | + |
| 35 | +## Built-in Functions |
| 36 | + |
| 37 | +The supported built-in functions are: |
| 38 | + |
| 39 | + * `file(path)` - Reads the contents of a file into the string. |
| 40 | + |
| 41 | + * `lookup(map, key)` - Performs a dynamic lookup into a mapping |
| 42 | + variable. |
0 commit comments