For-each application service plan

Hello, I am trying to create 2 resources by using “for-each” Terraform 0.12 version to create more than one application service plan. So, have declared with AppServicePlanName as variable type = list(string). :

variable "AppsServicePlanName" {
  type = list(string)
  description = "app service plan name"
  default = [ "testappsvcp1", "testappsvcp2" ]
} :

resource  "azurerm_resource_group" "appssvc" {
  name     = var.AppsResourceGroupName
  location = var.AppsResourceGroupLocation

resource "azurerm_app_service_plan" "asp" {
  for_each            = toset(var.AppServicePlanName)
  name                = each.value
  resource_group_name = "${}"
  location            = var.AppsServicePlanLocation
  asp_name            = each.value
  asp_kind            = var.AppsServicePlanAspKind
  asp_tier            = var.AppsServicePlanAspTier
  asp_size            = var.AppsServicePlanAspSize
  asp_capacity        = var.AppsServicePlanCapacity
  is_xenon            = var.AppsServicePlanisXenon

tags = {
 env = "Test"
  layer = "app"

Get this error:

in resource "azurerm_app_service_plan" "asp":

 for_each            = toset("[ "testappsvcp1", "testappsvcp2" ]")
A comma is required to separate each function argument from the next.

Can I not declare it within an array like how I have declared in the variable?

Also, if I need to create apps services based on these 2 app service plans. So, if I use for_each for app service plan, how will I susbstitute the asp_id for them?

Can I use the each.key/value from app service plan into app service? I am confused there. Could somebody please help me understand this?

Thanks in Advance.

Hi @psu,

The snippet of source code in the error message you shared doesn’t seem to match the corresponding line in the full example you shared:

for_each            = toset("[ "testappsvcp1", "testappsvcp2" ]")
  for_each            = toset(var.AppServicePlanName)

Focusing on the expression in the error message, it looks like you wrote the object expression in quotes, and so Terraform thinks you are intending it to be a single string. But that expression contains quotes itself, so there is a syntax error.

The correct syntax is to remove the quotes, like this:

  for_each = toset(["testappsvcp1", "testappsvcp2"])

If you were intending to use var.AppsServicePlanName instead, you can simplify a bit by declaring it as being a set rather than a list, and then you won’t need the toset function anymore:

variable "AppsServicePlanName" {
  type        = set(string)
  description = "app service plan name"
  default     = [ "testappsvcp1", "testappsvcp2" ]
  for_each = var.AppServicePlanName

Thank you for the reply @apparentlymart. The reason it was a different error that I posted as I was trying to tokenize the variables. As my intention is to move this to a module by itself so that I could use this code for future IAC coding.

I will try the set string, so if I were to use it as variables, I cannot set a default as I will be setting variables at runtime, so is it OK to not declare a default, so would it be then?