Hi All,
I have a problem that I’m hoping you can help with.
I use Terraform to create Azure Gallery Applications. I am trying to expand my existing code to create apps in multiple regions if required, but can’t get the code to work the way I would like! (Alternative coding options are gratefully received if you can think if an easier way to do this!)
First of all I have a yaml file that I read into Terraform with various app properties similar to below:
applications:
application1:
name: "App1"
version: "1.0.0"
install: "install.bat"
uninstall: "uninstall.bat"
source: "blob url"
region: ["uksouth","ukwest"]
application2:
name: "App2"
version: "1.0.1"
install: "install.bat"
uninstall: "uninstall.bat"
source: "blob url"
region: ["uksouth"]
and so on.
Within Terraform I have a local variable to read in the file:
locals {
apps = yamldecode(file("${path.root}/config/applications_config.yaml"))
}
Later in my code, I have a for_each loop that loops through the apps to create them:
resource "azurerm_gallery_application" "this" {
for_each = local.apps.applications
name = each.value.name
gallery_id = azurerm_shared_image_gallery.this.id
location = "uksouth"
supported_os_type = "Windows"
}
resource "azurerm_gallery_application_version" "this" {
for_each = local.apps.applications
name = each.value.version
location = "uksouth"
...and so on (This is a long list so cut this down to keep the post short)
}
As you can see the region is hard coded, and I am now trying to make the region dynamic / automated in the code. I have added ‘region’ to the yaml as shown above, and I am now trying to create a new local variable that adds the application multiple times depending on the value of region; that way I can just for_each through the creation of all apps in all needed regions.
As an example, the local.apps.applications variable looks like this when application1 is read in from the yaml file:
{
"application1" = {
"install" = "install.bat"
"name" = "App1"
"region" = [
"uksouth",
"ukwest",
]
"source" = "blob url"
"uninstall" = "uninstall.bat"
"version" = "1.0.0"
}
}
I would like a new variable, that creates a duplicate; one for each region so it would look something like this:
{
"application1" = {
"install" = "install.bat"
"name" = "App1"
"region" = "uksouth"
"source" = "blob url"
"uninstall" = "uninstall.bat"
"version" = "1.0.0"
}
"application2" = {
"install" = "install.bat"
"name" = "App1"
"region" = "ukwest"
"source" = "blob url"
"uninstall" = "uninstall.bat"
"version" = "1.0.0"
}
}
I know a for expression is the way to go with this, but I cant figure out the expression itself.
The closest I have managed to get (and this does not work anyway) is below. I’m assuming I would need a nested for again in the empty map which brings back the multiple instances I am looking for:
tmp = {for application, appproperties in local.apps.applications : application => can(appproperties.region) ? length(appproperties.region) > 1 ? {} : appproperties : merge(appproperties, {"region" = "uksouth"})}
Thanks in Advance. Also any useful links on advanced help for for expressions would be appreciated if there is resources out there! All I can find is basic examples from Terraform help on how the function works.