2014-07-28 19:43:00 +02:00
---
layout: "docs"
page_title: "Interpolation Syntax"
sidebar_current: "docs-config-interpolation"
2014-10-22 05:21:56 +02:00
description: |-
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}` .
2014-07-28 19:43:00 +02:00
---
# Interpolation Syntax
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}` .
The interpolation syntax is powerful and allows you to reference
variables, attributes of resources, call functions, etc.
2014-10-03 07:11:53 +02:00
## Available Variables
**To reference user variables**, use the `var.` prefix followed by the
2014-07-28 19:43:00 +02:00
variable name. For example, `${var.foo}` will interpolate the
`foo` variable value. If the variable is a mapping, then you
can reference static keys in the map with the syntax
`var.MAP.KEY` . For example, `${var.amis.us-east-1}` would
get the value of the `us-east-1` key within the `amis` variable
that is a mapping.
2015-02-24 00:04:18 +01:00
**To reference attributes of your own resource**, the syntax is
`self.ATTRIBUTE` . For example `${self.private_ip_address}` will
interpolate that resource's private IP address. Note that this is
only allowed/valid within provisioners.
2014-10-03 07:11:53 +02:00
**To reference attributes of other resources**, the syntax is
2014-07-28 19:43:00 +02:00
`TYPE.NAME.ATTRIBUTE` . For example, `${aws_instance.web.id}`
will interpolate the ID attribute from the "aws\_instance"
2014-10-10 06:25:59 +02:00
resource named "web". If the resource has a `count` attribute set,
you can access individual attributes with a zero-based index, such
as `${aws_instance.web.0.id}` . You can also use the splat syntax
2015-01-14 18:28:25 +01:00
to get a list of all the attributes: `${aws_instance.web.*.id}` .
2014-10-10 06:25:59 +02:00
This is documented in more detail in the
[resource configuration page ](/docs/configuration/resources.html ).
2014-07-28 19:43:00 +02:00
2014-10-03 07:11:53 +02:00
**To reference outputs from a module**, the syntax is
`MODULE.NAME.OUTPUT` . For example `${module.foo.bar}` will
interpolate the "bar" output from the "foo"
[module ](/docs/modules/index.html ).
**To reference count information**, the syntax is `count.FIELD` .
For example, `${count.index}` will interpolate the current index
in a multi-count resource. For more information on count, see the
resource configuration page.
2014-07-28 19:43:00 +02:00
2014-10-08 05:15:08 +02:00
**To reference path information**, the syntax is `path.TYPE` .
TYPE can be `cwd` , `module` , or `root` . `cwd` will interpolate the
cwd. `module` will interpolate the path to the current module. `root`
will interpolate the path of the root module. In general, you probably
want the `path.module` variable.
2014-07-28 19:43:00 +02:00
## Built-in Functions
2014-10-03 07:11:53 +02:00
Terraform ships with built-in functions. Functions are called with
the syntax `name(arg, arg2, ...)` . For example,
to read a file: `${file("path.txt")}` . The built-in functions
are documented below.
2014-07-28 19:43:00 +02:00
The supported built-in functions are:
2014-08-19 22:16:29 +02:00
* `concat(args...)` - Concatenates the values of multiple arguments into
a single string.
2015-03-02 18:40:24 +01:00
* `element(list, index)` - Returns a single element from a list
at the given index. If the index is greater than the number of
elements, this function will wrap using a standard mod algorithm.
A list is only possible with splat variables from resources with
a count greater than one.
Example: `element(aws_subnet.foo.*.id, count.index)`
2014-08-19 22:16:29 +02:00
* `file(path)` - Reads the contents of a file into the string. Variables
in this file are _not_ interpolated. The contents of the file are
read as-is.
2014-07-28 19:43:00 +02:00
2015-01-14 18:28:25 +01:00
* `join(delim, list)` - Joins the list with the delimiter. A list is
2014-10-10 06:23:49 +02:00
only possible with splat variables from resources with a count
greater than one. Example: `join(",", aws_instance.foo.*.id)`
2014-07-28 19:43:00 +02:00
* `lookup(map, key)` - Performs a dynamic lookup into a mapping
2015-02-20 19:04:53 +01:00
variable. The `map` parameter should be another variable, such
as `var.amis` .
2014-11-07 19:23:02 +01:00
2015-03-02 18:40:24 +01:00
* `replace(string, search, replace)` - Does a search and replace on the
given string. All instances of `search` are replaced with the value
of `replace` . If `search` is wrapped in forward slashes, it is treated
as a regular expression. If using a regular expression, `replace`
can reference subcaptures in the regular expression by using `$n` where
`n` is the index or name of the subcapture. If using a regular expression,
the syntax conforms to the [re2 regular expression syntax ](https://code.google.com/p/re2/wiki/Syntax ).
* `split(delim, string)` - Splits the string previously created by `join`
back into a list. This is useful for pushing lists through module
outputs since they currently only support string values.
Example: `split(",", module.amod.server_ids)`