thedevopscat blog

Thoughts, observations and learnings of the devopscat...

Terraform Conditional Expressions

2022-12-23 3 min read Terraform

As you are no doubt aware, Terraform is a declarative laungage, it reads like a shopping list rather than code with a bunch of conditional statements. I like to think of it as matter of fact, there simply isn’t if, elseif (etc) type statements at play here and this is a (generally) a good thing. “Do or do not…” as somebody wise once said… (I know my audience!).

Anyway, it would be nice to turn things on and off for a given scenareo, this is expecially usefull when creating template code. For this we can use conditional expressions or conditionals to their friends.

A conditional breaks down like this:

<test for some condition> ? <true value> : <false value>

We can store this in a variable, then use this conditionally set result to either do or not do something … (but we certainly won’t simply try) … ok, I’ll stop now!

Remember the count index from the first terraform post ? Remember I mentioned in passing it could also be set to 0 and 1? Well, here is another ’logical’ use case.

variable "build_admin_vm" {
  type        = bool
  default     = true
  description = "Enables or disables the the build of the linux admin vm."
}

resource "azurerm_linux_virtual_machine" "aks1_preprod_linux_vm" {
  count                           = var.build_admin_vm ? 1 : 0
  name                            = "admin-vm"
...

Here we are using the count as a simple switch, the boolean condition of the variable is either true or false, resulting in a conditional value (count) of either 1 or 0. This code will either build 1 vm or 0 vm’s respectively, hence this time the vm name also no longer needs a calculation for uniqueness, like in the first example use case.

I use this in test environments for big projects where I want to test a sub-section of the infrastructure with minimal cost and build times, I’ll wrap this logic around things like VM’s, Azure Application Gateway and Azure Firewall.


Now for some bonus material I found when fact checking this post.

I particularly like this AWS based example, from the terraform website

variable "high_availability" {
  type        = bool
  description = "If this is a multiple instance deployment, choose `true` to deploy 3 instances"
  default     = true
}

resource "aws_instance" "ubuntu" {
  count                       = (var.high_availability == true ? 3 : 1)
  ami                         = data.aws_ami.ubuntu.id
  instance_type               = "t2.micro"
  associate_public_ip_address = (count.index == 0 ? true : false)
  subnet_id                   = aws_subnet.subnet_public.id
  tags                        = merge(local.common_tags)
}

This is notable for the following reasons:

  • Offers a variance of my on/off functionaity to basic/high availability.
  • Associates a single public IP to just the first of potentially 3 vm’s to use as a jump box.

Nice! I’ll show my version of this in a future post.