Null values don’t go well hand in hand with Terraform’s for_each
, but, as always, here’s some hack on how to get through.
Prerequisites
- Terraform
Solution
Personally, I’ve used null values in conditionals, being part of a dynamic blocks so to speak. Here’s some example though:
using tolist() function in a dynamic resource block
# variables.tf
variable "enable_access_config" {
description = "Access configurations, i.e. IPs via which this instance can be accessed via the Internet."
type = string
default = null
}
# module.tf
resource "google_compute_instance" "this" {
...
network_interface {
subnetwork = var.subnetwork
subnetwork_project = var.subnetwork_project
dynamic "access_config" {
for_each = var.enable_access_config == null ? [] : tolist([var.enable_access_config])
content {}
}
}
...
}
# main.tf
module "vms" {
...
enable_access_config = "true"
}
Conclusion
When nothing works, feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.