post image :date_long | 1 min Read

How to conditionally add key value to Terraform map

This code will conditionally add or ommit netapp-cleaner block based on prefix local variable.

locals {
  prefix = "deaut"
  raj = merge({
    netapp-admin = {
      member = "serviceAccount:raj",
      role = "roles/netapp.admin"
    },
    netapp-user = {
      member = "serviceAccount:jan",
      role = "roles/netapp.user"
    },

  },
  local.prefix == "xdeaut" ? {
  netapp-cleaner = {
        member = "serviceAccount:cleaner",
        role = "roles/blaaaa"
      }
  }: {}
  )
}
output "debug" {
   value = local.raj
   description = "debug"
}

202406191306

author image

Jan Toth

I have been in DevOps related jobs for past 6 years dealing mainly with Kubernetes in AWS and on-premise as well. I spent quite a lot …

comments powered by Disqus