FEATURES OPEN SOURCE ABOUT DOCS LOGIN REGISTER

Interpolation Syntax

Embedded within strings in Terraform, whether you’re using the Terraform syntax or JSON syntax, you can interpolate other values. 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.

You can also perform simple math in interpolations, allowing you to write expressions such as ${count.index + 1}.

You can escape interpolation with double dollar signs: $${foo} will be rendered as a literal ${foo}.

Available Variables

To reference user string variables, use the var. prefix followed by the variable name. For example, ${var.foo} will interpolate the foo variable value.

To reference user map variables, the syntax is var.MAP["KEY"]. For example, ${var.amis["us-east-1"]} would get the value of the us-east-1 key within the amis map variable.

To reference user list variables, the syntax is ["${var.LIST}"]. For example, ["${var.subnets}"] would get the value of the subnets list, as a list. You can also return list elements by index: ${var.subnets[idx]}.

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.

To reference attributes of other resources, the syntax is TYPE.NAME.ATTRIBUTE. For example, ${aws_instance.web.id} will interpolate the ID attribute from the “aws_instance” 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 to get a list of all the attributes: ${aws_instance.web.*.id}. This is documented in more detail in the resource configuration page.

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.

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.

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.

Built-in Functions

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.

The supported built-in functions are:

Templates

Long strings can be managed using templates. Templates are data-sources defined by a filename and some variables to use during interpolation. They have a computed rendered attribute containing the result.

A template data source looks like:

data "template_file" "example" {
  template = "${hello} ${world}!"
  vars {
    hello = "goodnight"
    world = "moon"
  }
}

output "rendered" {
  value = "${data.template_file.example.rendered}"
}

Then the rendered value would be goodnight moon!.

You may use any of the built-in functions in your template.

Using Templates with Count

Here is an example that combines the capabilities of templates with the interpolation from count to give us a parameterized template, unique to each resource instance:

variable "count" {
  default = 2
}

variable "hostnames" {
  default = {
    "0" = "example1.org"
    "1" = "example2.net"
  }
}

data "template_file" "web_init" {
  // here we expand multiple template_files - the same number as we have instances
  count    = "${var.count}"
  template = "${file("templates/web_init.tpl")}"
  vars {
    // that gives us access to use count.index to do the lookup
    hostname = "${lookup(var.hostnames, count.index)}"
  }
}

resource "aws_instance" "web" {
  // ...
  count = "${var.count}"
  // here we link each web instance to the proper template_file
  user_data = "${element(data.template_file.web_init.*.rendered, count.index)}"
}

With this, we will build a list of template_file.web_init data sources which we can use in combination with our list of aws_instance.web resources.

Math

Simple math can be performed in interpolations:

variable "count" {
  default = 2
}

resource "aws_instance" "web" {
  // ...
  count = "${var.count}"

  // tag the instance with a counter starting at 1, ie. web-001
  tags {
    Name = "${format("web-%03d", count.index + 1)}"
  }
}

The supported operations are:

-> Note: Since Terraform allows hyphens in resource and variable names, it’s best to use spaces between math operators to prevent confusion or unexpected behavior. For example, ${var.instance-count - 1} will subtract 1 from the instance-count variable value, while ${var.instance-count-1} will interpolate the instance-count-1 variable value.


See the source of this document at Terraform.io