Windows WebApp
Last updated: November 28, 2024WebApp is a container for your web application. Windows WebApp requires a Windows AppServicePlan. 
The code below creates a Windows WebApp configured for Dotnet version 8.0
The code below creates a Windows WebApp configured for Dotnet version 8.0
resource "azurerm_windows_web_app" "webapp" {
  name                = "${var.environment_prefix}-${var.solution_prefix}${var.customer_prefix}-webapp"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  service_plan_id = azurerm_service_plan.appserviceplan.id
  public_network_access_enabled = true
  tags = var.tags
  site_config {  
    application_stack {
      dotnet_version = "v8.0"
    }
  }
}Setting the connection string
Add the following snippet to your webapp resource definition to add the connection string:
connection_string {
  name  = "DemoDotnetContext"
  type  = "SQLAzure"
  value = "Server=tcp:${azurerm_mssql_server.server.fully_qualified_domain_name};Authentication=Active Directory Default; Database=${azurerm_mssql_database.db.name};User Id=${azurerm_user_assigned_identity.webapp_identity.client_id};"
}replace the values as per your need
Set the WebApp identity
If you want to assign a user managed identity to your web app, you can do this by adding:
Set the WebApp identity
If you want to assign a user managed identity to your web app, you can do this by adding:
dentity {
  type = "UserAssigned"
  identity_ids = [azurerm_user_assigned_identity.webapp_identity.id]
}The complete example would be:
resource "azurerm_user_assigned_identity" "webapp_identity" {
  name                = "DemoWebAppIdentity"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  tags = var.tags
}
resource "azurerm_windows_web_app" "webapp" {
  name                = "${var.environment_prefix}-${var.solution_prefix}${var.customer_prefix}-webapp"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  service_plan_id = azurerm_service_plan.appserviceplan.id
  public_network_access_enabled = true
  tags = var.tags
  site_config {  
    application_stack {
      dotnet_version = "v8.0"
    }
  }
  identity {
    type = "UserAssigned"
    identity_ids = [azurerm_user_assigned_identity.webapp_identity.id]
  }
  connection_string {
    name  = "DemoDotnetContext"
    type  = "SQLAzure"
    value = "Server=tcp:${azurerm_mssql_server.server.fully_qualified_domain_name};Authentication=Active Directory Default; Database=${azurerm_mssql_database.db.name};User Id=${azurerm_user_assigned_identity.webapp_identity.client_id};"
  }
}
Further reading
