website: update website for conditionals

This commit is contained in:
Mitchell Hashimoto 2016-12-07 20:49:13 -05:00
parent f7abd6eb1c
commit 4ab5356ff9
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
1 changed files with 52 additions and 11 deletions

View File

@ -15,8 +15,10 @@ 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](#math) in interpolations, allowing
you to write expressions such as `${count.index + 1}`.
You can perform [simple math](#math) in interpolations, allowing
you to write expressions such as `${count.index + 1}`. And you can
also use [conditionals](#conditionals) to determine a value based
on some logic.
You can escape interpolation with double dollar signs: `$${foo}`
will be rendered as a literal `${foo}`.
@ -25,24 +27,24 @@ will be rendered as a literal `${foo}`.
There are a variety of available variable references you can use.
### User string variables
#### User string variables
Use the `var.` prefix followed by the variable name. For example,
`${var.foo}` will interpolate the `foo` variable value.
### User map variables
#### 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 list variables
#### 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
#### Attributes of your own resource
The syntax is `self.ATTRIBUTE`. For example `${self.private_ip_address}`
will interpolate that resource's private IP address.
@ -50,7 +52,7 @@ will interpolate that resource's private IP address.
-> **Note**: The `self.ATTRIBUTE` syntax is only allowed and valid within
provisioners.
### Attributes of other resources
#### Attributes of other resources
The syntax is `TYPE.NAME.ATTRIBUTE`. For example,
`${aws_instance.web.id}` will interpolate the ID attribute from the
@ -61,13 +63,13 @@ 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
#### 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).
### Count information
#### Count information
The syntax is `count.FIELD`. For example, `${count.index}` will
interpolate the current index in a multi-count resource. For more
@ -76,7 +78,7 @@ page](/docs/configuration/resources.html).
<a id="path-variables"></a>
### Path information
#### Path information
The syntax is `path.TYPE`. TYPE can be `cwd`, `module`, or `root`.
`cwd` will interpolate the current working directory. `module` will
@ -84,6 +86,45 @@ 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="conditionals"></a>
## Conditionals
Interpolations may contain conditionals to branch on the final value.
```
resource "aws_instance" "web" {
subnet = "${var.env == "production" ? var.prod_subnet : var.dev_subnet}"
}
```
The conditional syntax is the well-known ternary operation:
CONDITION ? TRUEVAL : FALSEVAL
The condition can be any valid interpolation syntax, such as variable
access, a function call, or even another conditional. The true and false
value can also be any valid interpolation syntax. The returned types by
the true and false side must be the same.
The support operators are:
* Equality: `==` and `!=`
* Numerical comparison: `>`, `<`, `>=`, `<=`
* Boolean logic: `&&`, `||`, unary `!`
A common use case for conditionals is to enable/disable a resource by
conditionally setting the count:
```
resource "aws_instance" "vpn" {
count = "${var.something ? 1 : 0}"
}
```
In the example above, the "vpn" resource will only be included if
"var.something" evaluates to true. Otherwise, the VPN resource will
not be created at all.
<a id="functions"></a>
## Built-in Functions
@ -266,7 +307,7 @@ The supported built-in functions are:
in brackets to indicate that the output is actually a list, e.g.
`a_resource_param = ["${split(",", var.CSV_STRING)}"]`.
Example: `split(",", module.amod.server_ids)`
* `timestamp()` - Returns a UTC timestamp string in RFC 3339 format. This string will change with every
invocation of the function, so in order to prevent diffs on every plan & apply, it must be used with the
[`ignore_changes`](/docs/configuration/resources.html#ignore-changes) lifecycle attribute.