Using Terraform expressions
In Terraform, you use an expression to refer to a value within a configuration. Thus, a string such as "abc"
or a list such as ["orange", "apple", "strawberry"]
is considered a simple expression. However, some expressions are more complex and are helpful when writing more flexible Terraform code. Terraform provides two useful constructs to write more flexible code: dynamic blocks and conditional expressions.
Dynamic blocks
Note
The code for this section is under chap03/dynamic-block
in the GitHub repo of this book.
In the previous chapter, we introduced the ability to create multiple instances using the count
and for_each
meta-arguments. Both constructs, in essence, created multiple resources using a single block. Blocks within blocks are termed nested blocks, and some nested blocks can be repeated. For example, within the google_compute_instance
resource, you can attach multiple disks using the attached_disk...