I am creating Azure App Service Plans that are defined in a .tfvars.json file using a for_each loop. Later, I want to use the Id of the App Service Plans when creating Azure App Services to assign the App Service to an appropriate App Service Plan. I have slimmed down some of the code here to protect sensitive information and not overwhelm the discussion.
Here is the relevant section of my .tfvars.json that defines the App Service Plans and App Services:
{
"configuration": {
"app_service_plans": {
"plan_list": [
{
"role": "plan1",
"size": "S3",
"tier": "Standard"
},
{
"role": "plan2",
"size": "S3",
"tier": "Standard"
}
]
},
"app_services": {
"service_list": [
{
"role": "admin",
"service_plan_role": "plan1"
},
{
"role": "externalapi",
"service_plan_role": "plan2"
}
]
}
}
}
Here is the code to create each App Service Plan:
module "app_service_plans" {
source = "<modulesourcelocation>"
for_each = {
for app_service_plan in var.configuration.app_service_plans.plan_list :
"${app_service_plan.role}" => app_service_plan
}
role = each.value.role
size = each.value.size
tier = each.value.tier
}
The app_service_plan module has a resource section to create the App Service Plan. There is nothing extraordinary about the resource definition so I am only including the definition below:
resource "azurerm_app_service_plan" "appserviceplan" {...}
Here is the outputs.tf for the app_service_plan module:
output "id" {
value = azurerm_app_service_plan.appserviceplan.id
}
Now, back in the main module, I want to create App Services and reference App Service Plans that were created in the app_service_plan module.
As with the App Service Plans, the App Services are defined in the .tfvars.json file and created using a for_each loop like so:
module "app_services" {
source = "<modulesourcelocation>"
for_each = {
for app_service in var.configuration.app_services.service_list :
"${app_service.role}" => app_service
}
app_service_plan_id = module.app_service_plans[each.value.service_plan_role].id
}
This results in the following error:
Error: Invalid index
59: app_service_plan_id = module.app_service_plans[each.value.service_plan_role].id
|----------------
| each.value.service_plan_role is "plan1"
| module.app_service_plans is object with 2 attributes
The given key does not identify an element in this collection value.
Clearly I am either not creating the correct output from the app_service_plan module or I am not using the correct syntax to reference an app_service_plan created in the module through for_each, but I am not sure what I am doing wrong.