skip to Main Content
#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 ?

2

Answers


  1. Chosen as BEST ANSWER

    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,
        ]
      }
    }
    

  2. I tried to Add an Existing Virtual Network to Function App Creation using Terraform and I was able to provision the requirement successfully.

    You are attempting to create an Azure Function App with a Virtual Network (VNet) integration using existing resources, according to the configuration you shared. The error message "ID was missing the ‘sites’ element" indicates that the ID of the App Service (or Function App) is not referenced correctly.

    You need to specify an app_service_id for the Terraform resource azurerm_app_service_virtual_network_swift_connection. This ID should refer to either an Azure Function App or an App Service. However, you have given the ID of a service plan in your code, which is incorrect:

    resource "azurerm_app_service_virtual_network_swift_connection" "dev" {
      app_service_id = data.azurerm_service_plan.dev.id
    ...
    }
    

    You need to use the ID of the azurerm_windows_function_app resource that you created earlier, not the service plan. The service plan ID is missing the ‘sites’ element, which is required for an App Service or Function App ID.

    **My terraform configuration: **

    provider "azurerm" {
      features {}
    }
    
    # Create a new Resource Group
    resource "azurerm_resource_group" "example" {
      name     = "vksb-rg"
      location = "east us"
    }
    
    # Create a new Storage Account
    resource "azurerm_storage_account" "example" {
      name                     = "vksbstacc"
      resource_group_name      = azurerm_resource_group.example.name
      location                 = azurerm_resource_group.example.location
      account_tier             = "Standard"
      account_replication_type = "LRS"
    }
    
    # Create a new Service Plan
    resource "azurerm_service_plan" "example" {
      name                = "vksb-serviceplan"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
      os_type = "Windows"
    
      sku_name = "S1"  # This combines the tier and size into one attribute
    }
    
    # Create a new Application Insights
    resource "azurerm_application_insights" "example" {
      name                = "vksb-appinsights"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
      application_type    = "web"
    }
    
    # Create a new Function App
    resource "azurerm_windows_function_app" "example" {
      name                = "vksb-functionapp"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
      service_plan_id = azurerm_service_plan.example.id
      storage_account_name       = azurerm_storage_account.example.name
      storage_account_access_key = azurerm_storage_account.example.primary_access_key
     
    
      site_config {
        
      }
    
      app_settings = {
        "APPLICATIONINSIGHTS_CONNECTION_STRING" = "InstrumentationKey=${azurerm_application_insights.example.instrumentation_key}"
      }
    }
    
    # Create a new Virtual Network
    resource "azurerm_virtual_network" "example" {
      name                = "vksb-vnet"
      address_space       = ["10.0.0.0/16"]
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
    }
    
    # Create a new Subnet with a delegation for Microsoft.Web/serverFarms
    resource "azurerm_subnet" "example" {
      name                 = "demovk-subnet"
      resource_group_name  = azurerm_resource_group.example.name
      virtual_network_name = azurerm_virtual_network.example.name
      address_prefixes     = ["10.0.2.0/24"]
      service_endpoints    = ["Microsoft.Storage", "Microsoft.Sql"]
    
      delegation {
        name = "functionappdelegation"
        service_delegation {
          name    = "Microsoft.Web/serverFarms"
          actions = ["Microsoft.Network/virtualNetworks/subnets/join/action"]
        }
      }
    }
    
    
    # Integrate the Function App with the Virtual Network using the appropriate resource type
    resource "azurerm_app_service_virtual_network_swift_connection" "example" {
      app_service_id = azurerm_windows_function_app.example.id
      subnet_id      = azurerm_subnet.example.id
    }
    

    Output:

    enter image description here

    &

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search