Reputation: 745
We have defined variables for a given module. This is a sample
variable "kv_name_solution" { }
variable "kv_name_seqnr" { }
variable "kv_name_purpose" { }
...
variable "location_name" { default = "Europe" }
variable "location_tier" { default = "Primary" }
variable "rg_name_solution" { default = "edw"}
variable "rg_name_seqnr" { default = "001"}
In our tfvars plenty of kv are prepared. So in a template we use this code
module "kv_create" {
for_each = var.resources_kv
source = "../../_modules/security/kv_create"
kv_name_solution = each.value["kv_name_solution"]
kv_name_seqnr = each.value["kv_name_seqnr"]
kv_name_purpose = each.value["kv_name_purpose"]
...
location_name = each.value["location_name"]
location_tier = each.value["location_tier"]
rg_name_solution = each.value["rg_name_solution"]
rg_name_seqnr = each.value["rg_name_seqnr"]
}
But, I'm trying to find a way that when in the tfvars for example the rg_name_solution and rg_name_seqnr are omitted the code in the for_each still works. Have been searching with try, but couldn't get it right so far.
Because location_name = try(each.value["location_name"], null)
seems to cancel my default set on the variable.
Any suggestions ?
These are the 'complete files'
terraform.tfvars
rg_resources = {
"edw-common" = {
rg_name_solution = "edw"
rg_name_seqnr = "001"
rg_location_name = "Europe"
rg_location_tier = "Primary"
}
}
kv_resources = {
"adf-001" = {
kv_name_solution = "edw"
kv_name_seqnr = "001"
kv_name_purpose = "adfxx"
}
}
main.tf
....
module "provision_keyvaults" {
count = (var.MODULE == "KV") || ((var.MODULE == "ALL" && var.APPLY == "false")) ? 1 : 0
source = "./_templates/kv"
resources_kv = var.kv_resources
}
....
template\kv\main.tf
module "kv_create" {
for_each = var.resources_kv
source = "../../_modules/security/kv_create"
kv_location_name = try(each.value["kv_location_name"],null)
kv_location_tier = try(each.value["kv_location_tier"],null)
kv_name_solution = each.value["kv_name_solution"]
kv_name_seqnr = each.value["kv_name_seqnr"]
kv_name_purpose = each.value["kv_name_purpose"]
rg_name_solution = try(each.value["rg_name_solution"],null)
rg_name_seqnr = try(each.value["rg_name_seqnr"],null)
}
template\kv\variables.tf
variable "resources_kv" { }
_modules\kv_create\variables.tf
variable "kv_location_name" {
default = "Europe"
}
variable "kv_location_tier" {
default = "Primary"
}
variable "kv_name_solution" { }
variable "kv_name_seqnr" { }
variable "kv_name_purpose" { }
variable "rg_name_solution" {
default = "edw"
}
variable "rg_name_seqnr" {
default = "001"
}
_modules\kv_create\main.tf
module "subscription" {
source = "../../general/subscription_getdetails"
}
module "kv_location" {
source = "../../general/location_getdetails"
location_name = var.kv_location_name
location_tier = var.kv_location_tier
}
module "resourcegroup" {
source = "../../general/rg_getdetails"
rg_name_solution = var.rg_name_solution
rg_name_seqnr = var.rg_name_seqnr
}
data "azurerm_client_config" "current" {}
resource "azurerm_key_vault" "kv" {
count = var.kv_provision ? 1 : 0
name = "kv-${module.kv_location.sitecode}-${module.subscription.environment}-${var.kv_name_solution}-${var.kv_name_seqnr}-${var.kv_name_purpose}"
....
resource_group_name = module.resourcegroup.rg.name
location = module.kv_location.azure
...
}
And bottom line, the error
╷
│ Error: Invalid template interpolation value
│
│ on _modules/general/rg_getdetails/main.tf line 18, in data "azurerm_resource_group" "rg":
│ 18: name = "rg-${module.subscription.environment}-${var.rg_name_solution}-${var.rg_name_seqnr}"
│ ├────────────────
│ │ var.rg_name_solution is null
│
│ The expression result is null. Cannot include a null value in a string
│ template.
╵
Upvotes: 1
Views: 944
Reputation: 238081
I would just use lookup to provide a default value if not explicitly given. For example:
kv_name_solution = lookup(each.value, "kv_name_solution", var.kv_name_solution)
kv_name_seqnr = lookup(each.value, "kv_name_seqnr", var.kv_name_seqnr)
kv_name_purpose = lookup(each.value, "kv_name_purpose", var.kv_name_purpose)
Upvotes: 1