Multi line terneray expression in terraform

If you’re using Terraform writing multi line ternary expressions can come in handy. They can also be nested which can be useful – but be careful that it doesn’t become unreadable!

locals {
  iam_access_creds = (
    var.create ?
    var.create_both_creds ?
      [module.iam.access_key_id, module.iam.access_key_secret] :
      [module.iam.access_key_id] :
    []
  )
}

As seen in the above code snippet wrapping the entire expression in parenthesees allows the nested ternary, otherwise terraform would throw an error. We’ll talk about what terraform is and why and how to use it in a later post!

Read more about it here and HT to this Github issue where we first saw the answer!