terraform-provider-azapi: Azure Function in a Flex Consumption Plan 500 Error
I’m trying to create an azure function in a flex consumption plan. The terraform plan seems to work fine but the apply gives an error with nothing helpful even with diagnostics on.
Terraform code (let me know if you need any of the variable values):
terraform {
required_providers {
azuread = {
source = "hashicorp/azuread"
version = "~> 2.47.0"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~> 4.0, < 4.1.0" #bug with 4.1 causing the plugin to crash https://github.com/hashicorp/terraform-provider-azurerm/issues/27301
}
azapi = {
source = "Azure/azapi"
}
}
backend "azurerm" {}
}
resource "azurerm_subnet" "digest_subnet" {
name = local.digest_subnet_name
virtual_network_name = local.vnet_name
resource_group_name = local.resource_group_name
address_prefixes = [var.digest_ip_range]
service_endpoints = ["Microsoft.KeyVault", "Microsoft.Web"]
delegation {
name = "serverFarms"
service_delegation {
name = "Microsoft.Web/serverFarms"
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}
resource "azapi_resource" "digestPlan" {
type = "Microsoft.Web/serverfarms@2023-12-01"
schema_validation_enabled = false
location = data.azurerm_resource_group.main_resource_group.location
name = local.flex_service_plan_name
parent_id = data.azurerm_resource_group.main_resource_group.id
body = jsonencode({
kind = "functionapp",
sku = {
tier = "FlexConsumption",
name = "FC1"
},
properties = {
reserved = true
}
})
}
resource "azapi_resource" "digest_processor" {
type = "Microsoft.Web/sites@2023-12-01"
schema_validation_enabled = false
location = data.azurerm_resource_group.main_resource_group.location
name = local.digest_app_name
parent_id = data.azurerm_resource_group.main_resource_group.id
identity {
type = "SystemAssigned"
identity_ids = []
}
tags = merge(local.tags, {
"paradigm:resource" = local.digest_app_name,
"hidden-link: /app-insights-conn-string" = module.application_insights.connection_string
"hidden-link: /app-insights-instrumentation-key" = module.application_insights.ai_instrumentation_key
"hidden-link: /app-insights-resource-id" = module.application_insights.ai_id
})
body = jsonencode({
kind = "functionapp,linux",
identity = {
type : "SystemAssigned"
}
properties = {
serverFarmId = azapi_resource.digestPlan.id,
httpsOnly = true
functionAppConfig = {
deployment = {
storage = {
type = "blobContainer",
value = local.blobStorageAndContainer,
authentication = {
type = "SystemAssignedIdentity"
}
}
},
scaleAndConcurrency = {
maximumInstanceCount = var.digestMaximumInstanceCount,
instanceMemoryMB = var.instanceMemoryMB
},
runtime = {
name = var.functionAppRuntime,
version = var.functionAppRuntimeVersion
}
},
siteConfig = {
appSettings = setunion(local.shared_function_appsettings, local.digest_appsettings)
}
keyVaultReferenceIdentity = azurerm_user_assigned_identity.event_processor_identity.id
virtualNetworkSubnetId = azurerm_subnet.digest_subnet.id
}
})
depends_on = [azapi_resource.digestPlan, module.application_insights, azurerm_storage_account.digest_sa]
}
Error:
Error: Failed to create/update resource │ │ with azapi_resource.digest_processor, │ on digest.tf line 81, in resource “azapi_resource” “digest_processor”: │ 81: resource “azapi_resource” “digest_processor” { │ │ creating/updating Resource: (ResourceId │ “/subscriptions/<subscriptionid>/resourceGroups/<rg name>/providers/Microsoft.Web/sites/nhub-eastus-dev-digest-fx” │ / Api Version “2023-12-01”): PUT │ https://management.azure.com/subscriptions/<subscriptionid>/resourceGroups/<rg name>/providers/Microsoft.Web/sites/nhub-eastus-dev-digest-fx │ -------------------------------------------------------------------------------- │ RESPONSE 500: 500 Internal Server Error │ ERROR CODE UNAVAILABLE │ -------------------------------------------------------------------------------- │ { │ “Message”: “An error has occurred.” │ } │ -------------------------------------------------------------------------------- │ ╵
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 16 (1 by maintainers)
Microsoft.App/environmentsthis is the trick, the subnet must have this delegationOh my goodness, I’ve been messing around with the deployment a lot today and I finally figured out my issue. https://learn.microsoft.com/en-us/azure/azure-functions/flex-consumption-plan#considerations
I had to register the Microsoft.App provider on my subscription. I’m now able to create the azure function with the vnet integration. @trampolin I don’t think you should need a /23 though, it says you need a min of 40 ip addresses so I would think /25 should be fine, but I haven’t tried that to verify.
@zspasev-fr is correct, the delegation type for Flex Consumption function apps is different. This is documented here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-networking-options?tabs=azure-portal#regional-virtual-network-integration.