How to specify .net core version with Terraform azurerm_app_service

Viewed 6598

Out of the box, I think azurerm_app_service provider does allow us to specify the .Net framework version, by utilising the dotnet_framework_version field.

dotnet_framework_version - (Optional) The version of the .net framework's CLR used in this App Service. Possible values are v2.0 (which will use the latest version of the .net framework for the .net CLR v2 - currently .net 3.5) and v4.0 (which corresponds to the latest version of the .net CLR v4 - which at the time of writing is .net 4.7.1). For more information on which .net CLR version to use based on the .net framework you're targeting - please see this table. Defaults to v4.0.

https://www.terraform.io/docs/providers/azurerm/r/app_service.html#dotnet_framework_version

The document says that the possible values are v2.0 or v4.0.

But what if I am targeting .NET Core, say v2.2 instead? What am I supposed to do here?

Azure portal allows selecting .NET Core from the drop down menu. (see screenshot below)

enter image description here

I am not sure whether or not there's a way to do this with Terraform azurerm_app_service as well.

3 Answers

You need to specify the following 2 settings in site_config block of the app_service block. for e.g.

resource "azurerm_app_service" "app_service" {
  name                = local.graphql_server_long_name
  location            = var.azure_region
  resource_group_name = azurerm_resource_group.rg.name
  app_service_plan_id = azurerm_app_service_plan.graphql_server.id
  site_config {
    linux_fx_version = "DOTNETCORE|5.0"
    dotnet_framework_version = "v5.0"
  }

those are linux_fx_version and dotnet_framework_version.

You need to be running at least 2.38.0 version of azurerm provider. You can check this by running the following command in the directory which contains your terraform files: terraform version

This will display the terraform cli and all used providers.

There are 2 available options to host .NET core application in App Service:

  1. Windows App Service plan.
  2. Linux App Service plan (default option, when you created App Service on Azure portal).

If you are using Windows App service plan, you need to specify dotnet_framework_version and as you mentioned, there is only 2 available options - v2.0 and v4.0.

Instead, you can use the Linux App Service plan and specify the .net version inlinux_fx_version field (dotnet_framework_version should be empty).
Some sort of this:

resource "azurerm_app_service" "stackoverlow_service" {
  name = "stackoverlow-test-net-version"
  location = "centralus"
  resource_group_name = "{resource_group_name}"
  app_service_plan_id = "{app_service_plan_id}"

  site_config {
    linux_fx_version = "DOTNETCORE|2.2"
    min_tls_version = "1.2"
    always_on = true
    scm_type = "None"
    managed_pipeline_mode = "Integrated"
    websockets_enabled = false
    use_32_bit_worker_process = true
  }
}

App Service is a pretty complex product with many configuration options and Azure/terraform documentation does not cover well all aspects.
To generate desired configuration, you can create App Service in Azure and import resource to terraform:

  1. create minimal correct App Service resource in terraform.
  2. create App Service with desired config in Azure portal.
  3. import resource using terraform import command :terraform import azurerm_app_service.stackoverlow_service /subscriptions/.....
  4. run terraform plan to see the exact difference or check the terraform state file.
Related