Course outlines for learning terraform.
expected time | requirements |
---|---|
30 minutes | A computer with Terraform installed, terraform knowledge. |
Goal: Learn how to conditionally create resources.
Sometimes you may want to create a resource on certain conditions. In many other programming languages this will be done using an if
or when
statement. In Terraform the syntax is a little weird, so some explanation may help.
A conditional is expressed using:
condition ? true_val : false_val
For example:
var.feature ? 1 : 0
That would return 1
when var.feature
is true
, otherwise 0
is returned.
To optionally create a resource, this conditional can be used on the count
parameter. That’s quite unintuitive:
resource "x" "y" {
count = var.feature ? 1 : 0
name = "z"
}
You can use this count
trick on resources
and data
objects.
You can test quite some things/conditions, here are some examples:
# Compare a string:
var.variable == "something" ? 1 : 0
# Negative compare:
var.variable != "something" ? 1 : 0
# You can also return some value on a condition. (That would not be in the `count` parameter.)
var.variable == "europe" ? "amsterdam": "other"
# Or compare a value:
var.variable > 10 ? "large" : "small"
# Or use a function:
regex('pattern', var.variable) ? "found" : "not found"
my_var
.example
.example
resource conditionally, when my_var
is an odd number.Here are some hints:
variable
documentation.resource "example" "default" {
name = "whatever"
}
Can you explain these conditionals:
count = var.vpc_id == "" ? 1 : 0
count = var.vpc_id == "" ? min(length(data.aws_availability_zones.default.names), var.amount) : 0
count = var.vpc_id != "" && var.bastion_host ? 1 : 0
variable "my_var" {
type = number
}
resource "example" "default" {
count = var.my_var % 2 != 0 ? 1 : 0
# Or the reverse:
# count = var.my_var % 2 == 1 ? 1 : 0
name = "whatever"
}