The "WebHostingPlanNotFound" error occurs when Terraform attempts to create an Azure App Service or Function App but cannot find or properly reference the associated App Service Plan (ServerFarm). This error typically stems from resource dependency issues, mismatched resource groups, or naming conflicts.
The WebHostingPlanNotFound error is returned by Azure when a resource (like an App Service or Function App) references an App Service Plan that Azure cannot locate. This can happen for several reasons: 1. The App Service Plan (also called ServerFarm or Web Hosting Plan) does not exist in the specified resource group or subscription. 2. Azure Resource Manager is trying to create dependent resources (the App Service) before the App Service Plan is fully created, due to missing explicit dependencies. 3. The reference to the plan ID is incorrect or malformed. 4. The plan exists in a different resource group or region than the app attempting to use it.
First, ensure your Terraform configuration explicitly creates the App Service Plan resource. Check for a resource block like:
resource "azurerm_service_plan" "example" {
name = "example-plan"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
os_type = "Linux"
sku_name = "B1"
}If you are using the deprecated azurerm_app_service_plan, update to azurerm_service_plan for AzureRM provider 3.0+.
Ensure your App Service or Function App resource correctly references the plan ID. For modern AzureRM provider:
resource "azurerm_linux_web_app" "example" {
name = "example-app"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_service_plan.example.location
service_plan_id = azurerm_service_plan.example.id
}Note the attribute name is service_plan_id (not app_service_plan_id). Check your provider documentation for the correct attribute.
If you are referencing an existing App Service Plan (not creating it in the same configuration), use the depends_on meta-argument:
data "azurerm_service_plan" "existing" {
name = "my-existing-plan"
resource_group_name = "my-resource-group"
}
resource "azurerm_linux_web_app" "example" {
name = "example-app"
resource_group_name = azurerm_resource_group.example.name
service_plan_id = data.azurerm_service_plan.existing.id
depends_on = [data.azurerm_service_plan.existing]
}The App Service Plan and the app resource must be in the same resource group. Verify:
# Both resources use the same resource group
resource "azurerm_service_plan" "example" {
resource_group_name = azurerm_resource_group.example.name
# ...
}
resource "azurerm_linux_web_app" "example" {
resource_group_name = azurerm_resource_group.example.name
service_plan_id = azurerm_service_plan.example.id
# ...
}Do not create the plan in one resource group and the app in another.
If migrating from older configurations, ensure your AzureRM provider version is consistent. Update your provider block:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}Run terraform init to update providers and lock files.
If the App Service Plan already exists in Azure and you need to manage it with Terraform, import it:
terraform import azurerm_service_plan.example /subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Web/serverfarms/{plan-name}Then add the corresponding resource block to your configuration. This prevents Terraform from trying to create a duplicate plan.
Dependency Resolution: Terraform creates resources in parallel by default. If you have multiple apps using the same plan, all will try to reference it simultaneously. Ensure the plan is created first with explicit ordering.
Deprecated Resource Names: The azurerm_app_service_plan resource was deprecated in favor of azurerm_service_plan. If upgrading an old configuration, use terraform state mv to migrate the resource in state.
Linux vs Windows Plans: Ensure the plan's OS type (os_type = "Linux" or os_type = "Windows") matches the app type (azurerm_linux_web_app vs azurerm_windows_web_app).
Regional Quotas: Some regions have quota limits on the number of App Service Plans per subscription. If you get this error after creating many plans, try a different region.
Shared Plans: If a plan exists in one resource group and you want to use it from another (cross-resource-group deployment), you must reference it via data source with the full resource ID path.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform