Pass over the Interpolation page (#9703)

* Pass over the Interpolation page

Fixes some grammar, typos and structure. Updated some headings and fixed
a couple of spelling mistakes.

* Added proper note syntax

* Turned some notes into actual notes

* Couple of minor typos just noticed
This commit is contained in:
James Turnbull 2016-10-28 20:13:09 -04:00 committed by Paul Stack
parent 52e4b4563b
commit 18b3736ba4
3 changed files with 78 additions and 57 deletions

View File

@ -23,58 +23,75 @@ 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.
There are a variety of available variable references you can use.
**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.
### User string variables
**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]}`.
Use the `var.` prefix followed by the variable name. For example,
`${var.foo}` will interpolate the `foo` variable value.
**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.
### User map variables
**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](/docs/configuration/resources.html).
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 outputs from a module**, the syntax is
`MODULE.NAME.OUTPUT`. For example `${module.foo.bar}` will
interpolate the "bar" output from the "foo"
### 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]}`.
### 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**: The `self.ATTRIBUTE` syntax is only allowed and valid within
provisioners.
### 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](/docs/configuration/resources.html).
### 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.
### 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](/docs/configuration/resources.html).
<a id="path-variables"></a>
**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.
### Path information
The syntax is `path.TYPE`. TYPE can be `cwd`, `module`, or `root`.
`cwd` will interpolate the current working directory. `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.
<a id="functions"></a>
## 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.
Terraform ships with built-in functions. Functions are called with the
syntax `name(arg, arg2, ...)`. For example, to read a file:
`${file("path.txt")}`.
### Supported built-in functions
The supported built-in functions are:
@ -89,26 +106,26 @@ The supported built-in functions are:
**This is not equivalent** of `base64encode(sha256(string))`
since `sha256()` returns hexadecimal representation.
* `ceil(float)` - Returns the the least integer value greater than or equal
* `ceil(float)` - Returns the least integer value greater than or equal
to the argument.
* `cidrhost(iprange, hostnum)` - Takes an IP address range in CIDR notation
and creates an IP address with the given host number. For example,
``cidrhost("10.0.0.0/8", 2)`` returns ``10.0.0.2``.
`cidrhost("10.0.0.0/8", 2)` returns `10.0.0.2`.
* `cidrnetmask(iprange)` - Takes an IP address range in CIDR notation
and returns the address-formatted subnet mask format that some
systems expect for IPv4 interfaces. For example,
``cidrmask("10.0.0.0/8")`` returns ``255.0.0.0``. Not applicable
`cidrmask("10.0.0.0/8")` returns `255.0.0.0`. Not applicable
to IPv6 networks since CIDR notation is the only valid notation for
IPv4.
* `cidrsubnet(iprange, newbits, netnum)` - Takes an IP address range in
CIDR notation (like ``10.0.0.0/8``) and extends its prefix to include an
CIDR notation (like `10.0.0.0/8`) and extends its prefix to include an
additional subnet number. For example,
``cidrsubnet("10.0.0.0/8", 8, 2)`` returns ``10.2.0.0/16``;
``cidrsubnet("2607:f298:6051:516c::/64", 8, 2)`` returns
``2607:f298:6051:516c:200::/72``.
`cidrsubnet("10.0.0.0/8", 8, 2)` returns `10.2.0.0/16`;
`cidrsubnet("2607:f298:6051:516c::/64", 8, 2)` returns
`2607:f298:6051:516c:200::/72`.
* `coalesce(string1, string2, ...)` - Returns the first non-empty value from
the given arguments. At least two arguments must be provided.
@ -141,7 +158,7 @@ The supported built-in functions are:
like this: `file("${path.module}/file")`.
* `floor(float)` - Returns the greatest integer value less than or equal to
the argument
the argument.
* `format(format, args, ...)` - Formats a string according to the given
format. The syntax for the format is standard `sprintf` syntax.
@ -176,7 +193,7 @@ The supported built-in functions are:
* `keys(map)` - Returns a lexically sorted list of the map keys.
* `length(list)` - Returns a number of members in a given list or map, or a number of characters in a given string.
* `length(list)` - Returns the number of members in a given list or map, or the number of characters in a given string.
* `${length(split(",", "a,b,c"))}` = 3
* `${length("a,b,c")}` = 5
* `${length(map("key", "val"))}` = 1
@ -231,13 +248,13 @@ The supported built-in functions are:
SHA-256 hash of the given string.
Example: `"${sha256("${aws_vpc.default.tags.customer}-s3-bucket")}"`
* `signum(int)` - Returns -1 for negative numbers, 0 for 0 and 1 for positive numbers.
* `signum(int)` - Returns `-1` for negative numbers, `0` for `0` and `1` for positive numbers.
This function is useful when you need to set a value for the first resource and
a different value for the rest of the resources.
Example: `element(split(",", var.r53_failover_policy), signum(count.index))`
where the 0th index points to `PRIMARY` and 1st to `FAILOVER`
* `sort(list)` - Returns a lexographically sorted list of the strings contained in
* `sort(list)` - Returns a lexicographically sorted list of the strings contained in
the list passed as an argument. Sort may only be used with lists which contain only
strings.
Examples: `sort(aws_instance.foo.*.id)`, `sort(var.list_of_strings)`
@ -272,7 +289,11 @@ The supported built-in functions are:
<a id="templates"></a>
## Templates
Long strings can be managed using templates. [Templates](/docs/providers/template/index.html) are [data-sources](/docs/configuration/data-sources.html) defined by a filename and some variables to use during interpolation. They have a computed `rendered` attribute containing the result.
Long strings can be managed using templates.
[Templates](/docs/providers/template/index.html) are
[data-sources](/docs/configuration/data-sources.html) 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:

View File

@ -72,7 +72,7 @@ These are the parameters that can be set:
------
**Note**: Default values can be strings, lists, or maps. If a default is
-> **Note**: Default values can be strings, lists, or maps. If a default is
specified, it must match the declared type of the variable.
### Strings
@ -243,11 +243,11 @@ The `-var-file` flag can be used multiple times per command invocation:
terraform apply -var-file=foo.tfvars -var-file=bar.tfvars
```
**Note**: Variable files are evaluated in the order in which they are specified
on the command line. If a variable is defined in more than one variables file,
-> **Note**: Variable files are evaluated in the order in which they are specified
on the command line. If a variable is defined in more than one variable file,
the last value specified is effective.
### Precedence example:
### Precedence example
Both these files have the variable `baz` defined:

View File

@ -20,7 +20,7 @@ Let's first extract our access key, secret key, and region
into a few variables. Create another file `variables.tf` with
the following contents.
**Note**: that the file can be named anything, since Terraform loads all
-> **Note**: that the file can be named anything, since Terraform loads all
files ending in `.tf` in a directory.
```
@ -118,7 +118,7 @@ Terraform will ask you to input the variables interactively. These
variables are not saved, but provides a nice user experience for getting
started with Terraform.
**Note**: UI Input is only supported for string variables. List and map
-> **Note**: UI Input is only supported for string variables. List and map
variables must be populated via one of the other mechanisms.
#### Variable Defaults
@ -184,7 +184,7 @@ $ terraform plan -var 'amis={ us-east-1 = "foo", us-west-2 = "bar" }'
...
```
**Note**: Even if every key will be assigned as input, the variable must be
-> **Note**: Even if every key will be assigned as input, the variable must be
established as a map by setting its default to `{}`.
Here is an example of setting a map's keys from a file. Starting with these