Search code examples
azureterraformazure-functions

Add Existing Virtual Network to Function App Creation -Terraform Azure


#access existing application insight
data "azurerm_application_insights" "dev"{
  name                = var.tf_var_app_insights_name
  resource_group_name = var.tf_var_rg_name
}

#################step-1 function app creation
data "azurerm_service_plan" "dev" {
  name                = var.tf_var_app_service_plan
  resource_group_name = var.tf_var_rg_name
}

data "azurerm_storage_account" "dev" {
  name                     = var.tfvarstname
  resource_group_name      = var.tf_var_rg_name
}

#create function app
resource azurerm_windows_function_app "dev" {
  name                       = var.tf_var_rg_functionapp 
  resource_group_name        = var.tf_var_rg_name
  storage_account_name       = var.tfvarstname
  storage_account_access_key = data.azurerm_storage_account.dev.primary_access_key
  location                   = var.tf_var_rg_location
  service_plan_id            = data.azurerm_service_plan.dev.id 
  functions_extension_version   =var.tf_var_function_extention_version
  auth_settings  {
     enabled = true 
     active_directory  {
         client_id = var.tf_var_client_id
         client_secret = var.tf_var_client_secret
         #client_secret_setting_name  = "MICROSOFT_PROVIDER_AUTHENTICATION_SECRET"
     }
     default_provider = var.tf_var_provider
     issuer = var.tf_var_issuer
  }
  site_config{
    application_stack {
      dotnet_version = var.tf_var_dot_net_version
    }
    application_insights_key =data.azurerm_application_insights.dev.instrumentation_key
  } 
  tags ={
    environment = var.tf_var_tags_environment
    application_name =var.tf_var_tags_application_name
    function_name =var.tf_var_tags_function_name
  }
  
}

data "azurerm_virtual_network" "dev" {
  name                =  var.tf_var_virtual_network_name
  resource_group_name = var.tf_var_rg_name
}
output "vnet_id" {
 value =  data.azurerm_virtual_network.dev.id
}

data "azurerm_subnet" "dev" {
  name                 = var.tf_var_subnet_name
  resource_group_name  = var.tf_var_rg_name
  virtual_network_name = data.azurerm_virtual_network.dev.name
}
output "subnet_id" {
 value= data.azurerm_subnet.dev.id
}
resource "azurerm_app_service_virtual_network_swift_connection" "dev" {
  app_service_id = data.azurerm_service_plan.dev.id
  subnet_id      = data.azurerm_subnet.dev.id
  depends_on = [data.azurerm_subnet.dev, azurerm_windows_function_app.dev]
  lifecycle {
    ignore_changes = [
      subnet_id,
    ]
  }
}

The error message is below error.I am trying to create a function app using existing resources. All the other parts are working. I just need a little support on virtual network integration. If anyone can support that will be great.

I tried to add

depends_on = [data.azurerm_subnet.dev, azurerm_windows_function_app.dev]
  lifecycle {
    ignore_changes = [
      subnet_id,
    ]

but it did not work anywhere. I need a little support on what am I missing ?


Solution

  • By giving Credit to Vinay B, the answer is to add function app id instead of app service plan id

    resource "azurerm_app_service_virtual_network_swift_connection" "dev" {
       # service_plan_id = data.azurerm_service_plan.dev.id
       app_service_id = azurerm_windows_function_app.dev.id
      subnet_id      = data.azurerm_subnet.dev.id
      depends_on = [data.azurerm_subnet.dev, azurerm_windows_function_app.dev]
      lifecycle {
        ignore_changes = [
          subnet_id,
        ]
      }
    }