@@ -15,8 +15,10 @@ interpolations are wrapped in `${}`, such as `${var.foo}`.
1515The interpolation syntax is powerful and allows you to reference
1616variables, attributes of resources, call functions, etc.
1717
18- You can also perform [ simple math] ( #math ) in interpolations, allowing
19- you to write expressions such as ` ${count.index + 1} ` .
18+ You can perform [ simple math] ( #math ) in interpolations, allowing
19+ you to write expressions such as ` ${count.index + 1} ` . And you can
20+ also use [ conditionals] ( #conditionals ) to determine a value based
21+ on some logic.
2022
2123You can escape interpolation with double dollar signs: ` $${foo} `
2224will be rendered as a literal ` ${foo} ` .
@@ -25,32 +27,32 @@ will be rendered as a literal `${foo}`.
2527
2628There are a variety of available variable references you can use.
2729
28- ### User string variables
30+ #### User string variables
2931
3032Use the ` var. ` prefix followed by the variable name. For example,
3133` ${var.foo} ` will interpolate the ` foo ` variable value.
3234
33- ### User map variables
35+ #### User map variables
3436
3537The syntax is ` var.MAP["KEY"] ` . For example, ` ${var.amis["us-east-1"]} `
3638would get the value of the ` us-east-1 ` key within the ` amis ` map
3739variable.
3840
39- ### User list variables
41+ #### User list variables
4042
4143The syntax is ` ["${var.LIST}"] ` . For example, ` ["${var.subnets}"] `
4244would get the value of the ` subnets ` list, as a list. You can also
4345return list elements by index: ` ${var.subnets[idx]} ` .
4446
45- ### Attributes of your own resource
47+ #### Attributes of your own resource
4648
4749The syntax is ` self.ATTRIBUTE ` . For example ` ${self.private_ip_address} `
4850will interpolate that resource's private IP address.
4951
5052-> ** Note** : The ` self.ATTRIBUTE ` syntax is only allowed and valid within
5153provisioners.
5254
53- ### Attributes of other resources
55+ #### Attributes of other resources
5456
5557The syntax is ` TYPE.NAME.ATTRIBUTE ` . For example,
5658` ${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}`.
6163This is documented in more detail in the [ resource configuration
6264page] ( /docs/configuration/resources.html ) .
6365
64- ### Outputs from a module
66+ #### Outputs from a module
6567
6668The syntax is ` MODULE.NAME.OUTPUT ` . For example ` ${module.foo.bar} ` will
6769interpolate the ` bar ` output from the ` foo `
6870[ module] ( /docs/modules/index.html ) .
6971
70- ### Count information
72+ #### Count information
7173
7274The syntax is ` count.FIELD ` . For example, ` ${count.index} ` will
7375interpolate the current index in a multi-count resource. For more
@@ -76,14 +78,53 @@ page](/docs/configuration/resources.html).
7678
7779<a id =" path-variables " ></a >
7880
79- ### Path information
81+ #### Path information
8082
8183The syntax is ` path.TYPE ` . TYPE can be ` cwd ` , ` module ` , or ` root ` .
8284` cwd ` will interpolate the current working directory. ` module ` will
8385interpolate the path to the current module. ` root ` will interpolate the
8486path of the root module. In general, you probably want the
8587` path.module ` variable.
8688
89+ <a id =" conditionals " ></a >
90+ ## Conditionals
91+
92+ Interpolations may contain conditionals to branch on the final value.
93+
94+ ```
95+ resource "aws_instance" "web" {
96+ subnet = "${var.env == "production" ? var.prod_subnet : var.dev_subnet}"
97+ }
98+ ```
99+
100+ The conditional syntax is the well-known ternary operation:
101+
102+ CONDITION ? TRUEVAL : FALSEVAL
103+
104+ The condition can be any valid interpolation syntax, such as variable
105+ access, a function call, or even another conditional. The true and false
106+ value can also be any valid interpolation syntax. The returned types by
107+ the true and false side must be the same.
108+
109+ The support operators are:
110+
111+ * Equality: ` == ` and ` != `
112+ * Numerical comparison: ` > ` , ` < ` , ` >= ` , ` <= `
113+ * Boolean logic: ` && ` , ` || ` , unary ` ! `
114+
115+ A common use case for conditionals is to enable/disable a resource by
116+ conditionally setting the count:
117+
118+ ```
119+ resource "aws_instance" "vpn" {
120+ count = "${var.something ? 1 : 0}"
121+ }
122+ ```
123+
124+ In the example above, the "vpn" resource will only be included if
125+ "var.something" evaluates to true. Otherwise, the VPN resource will
126+ not be created at all.
127+
87128<a id =" functions " ></a >
88129## Built-in Functions
89130
@@ -266,7 +307,7 @@ The supported built-in functions are:
266307 in brackets to indicate that the output is actually a list, e.g.
267308 ` a_resource_param = ["${split(",", var.CSV_STRING)}"] ` .
268309 Example: ` split(",", module.amod.server_ids) `
269-
310+
270311 * ` timestamp() ` - Returns a UTC timestamp string in RFC 3339 format. This string will change with every
271312 invocation of the function, so in order to prevent diffs on every plan & apply, it must be used with the
272313 [ ` ignore_changes ` ] ( /docs/configuration/resources.html#ignore-changes ) lifecycle attribute.
0 commit comments