For_each with join. Is it possible?

I am trying out a for_each loop to build 21 web apps in Azure. For other resources that do not use a for_each loop I use the following to make the name

Azure App Service Plan

resource “azurerm_app_service_plan” “asp” {
name=join("",[“asp-”,var.platform,"-",var.environment,"-"])

I want to do something similar when looping through a list of webapps. In my variables file I have a list of 21 webapps. In my webapps.tf file I am doing the following:

resource “azurerm_app_service” “web” {
for_each = var.webapps
name = each.value

I was hoping to something like name= each.value join("",[“web-”,var.platform,"-",var.environment,"-"])

But that does not work.

Any ideas on how I might pull this off?

Hi @washburnjoe,

I’m not exactly sure what you’re looking for here, but I think string templates may help you.

if you are trying to format both the each.value and the result of the join operation into a single string, you could for example use something like:

name = "${each.value}-${join("-", [var.platform, var.environment])}"

This example is a bit contrived, since you could add the each.value to the start of the join list argument, but it offers some other formatting options.

1 Like