While using the below code, I was able to successfully create a resource when the tfvars list has only one resource. It fails to create a plan when it has more than one resource.
During the testing, I also noticed that if both the resources has similar configuration (keys) under settings, then plan is getting generated.
In the following example, plan is failing because of “always_on” is added to the second resource. Is there any way to solve this issue?
Providers: AzureRM 3.98.0, terraform version – 1.7.5
Error: The given value is not valid for variable “appservicelist”: collection elements cannot be unified.
env.auto.tfvars
appservicelist = {
appsvc1 = {
appsvcname = "appsvctest01"
rgname = "appsvcrg01"
settings = {
site_config = {
minimum_tls_version = "1.2"
http2_enabled = true
application_stack = {
java_version = "17"
}
}
}
},
appsvc2 = {
appsvcname = "appsvctest02"
rgname = "appsvcrg02"
settings = {
site_config = {
minimum_tls_version = "1.2"
http2_enabled = true
always_on = true
application_stack = {
java_version = "17"
}
}
}
}
}
main.tf
module "appservice" {
for_each = var.appservicelist
source = "./modules/appservice"
appsvcname = each.value.appsvcname
rgname = each.value.rgname
settings = each.value.settings
}
variables.tf
variables "appservicelist" {
type = map(object({
appsvcname = string
rgname = string
settings = any
}) )
}
./modules/appservice/main.tf
resource "azurerm_linux_web_app" "appservice" {
name = var.appsvcname
rgname = var.rgname
dynamic "site_config" {
for_each = lookup(var.settings, "site_config", {}) != {} ? [1] : []
content {
always_on = lookup(var.settings.site_config, "always_on", false)
http2_enabled = lookup(var.settings.site_config, "http2_enabled", false)
minimum_tls_version = lookup(var.settings.site_config, "http2_enabled", null)
...
...
}
dynamic "application_stack" {
for_each = lookup(var.settings.site_config, "application_stack" {}) != {} ? [1] : []
content {
java_version = lookup(var.settings.site_config.application_stack, "java_version", null)
}
}
}
}
./modules/appservice/variables.tf
variables "appsvcname" {}
variables "rgname" {}
variables "settings" {
type = any
default = {}
}