dockerterraformterraform-provider-azureazure-appserviceazurerm-app-service

Using Terraform, how to create an Azure App Service for Windows and Docker


My team is attempting to create an Azure App Service that uses Windows Docker Containers, using Terraform.

I have tested the docker-basic example, from the Terraform GitHub project, to successfully create a Linux Docker App Service.

Next, I used the Terraform documentation for the azurerm_app_service and azurerm_app_service_plan, to change the main.tf file to one that will create a Windows Docker App Service, with a Windows IIS Docker image. Here is the updated main.tf file:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "main" {
  name     = "${var.prefix}-resources"
  location = "${var.location}"
}

resource "azurerm_app_service_plan" "main" {
  name                = "${var.prefix}-asp"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"
  kind                = "Windows"
  reserved            = false

  sku {
    tier = "Standard"
    size = "S1"
  }
}

resource "azurerm_app_service" "main" {
  name                = "${var.prefix}-appservice"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"
  app_service_plan_id = "${azurerm_app_service_plan.main.id}"

  site_config {
    app_command_line = ""
    windows_fx_version = "DOCKER|windows/servercore/iis:windowsservercore-ltsc2019"
  }

  app_settings = {
    "WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false"
    "DOCKER_REGISTRY_SERVER_URL"          = "https://mcr.microsoft.com"
  }
}

There are 4 changes. In azurerm_app_service_plan, the kind field is now set to "Windows," and the reserved field is now set to false. In the azurerm_app_service, the Docker image is specified in the windows_fx_version field (instead of linux_fx_version), and the DOCKER_REGISTRY_SERVER_URL points to the microsoft public docker registry mcr.microsoft.com (instead of docker.io)

The Terraform apply is successful, however, the App Service that gets created is not a Docker App Service. It, instead, gets created in the .NET stack configuration. We compared what was created with a Windows Docker App Service that we created in the Azure Portal, and it is clearly not right.

If more changes are needed, or if any of my changes have been in error, my team and I cannot detect what they might be, from the official documentation. Is there something we missed, in the Documentation? Is there a step missing from the Documentation? Is this an actual Terraform bug?


Solution

  • I tried the code given by you in my lab.

    The Major difference while creating in Portal and Terraform was that in portal it takes bydeafult as “Isxenon:true” and kind as “app,container,windows” even for a windows docker container . But while creating a windows docker container using terraform it takes “isxenon :false” and kind as “app” that’s why it gives a stack setting bydefault as a code based app.

    enter image description here

    Second thing is that Xenon is not supported in standard Sku, it should be premiumv3 Sku.

    So while deploying a windows docker container make the following changes to the code :

    In the App service plan: Kind = “xenon”, Is_xenon = true, Sku tier = premiumv3 Size = p1v3

    Then the main.tf file will be like this

    provider "azurerm" {
      features {}
    }
    
    data "azurerm_resource_group" "main" {
      name     = "example-resources"
    }
    
    resource "azurerm_app_service_plan" "main" {
      name                = "testansuman-asp"
      location            = data.azurerm_resource_group.main.location
      resource_group_name = data.azurerm_resource_group.main.name
      kind                = "xenon"
      reserved            = false       
      is_xenon = true
      sku {
        tier = "PremiumV3"
        size = "P1v3"
      }
    }
    
    resource "azurerm_app_service" "main" {
      name                = "dockerwindows-appservice"
      location            = data.azurerm_resource_group.main.location
      resource_group_name = data.azurerm_resource_group.main.name
      app_service_plan_id = azurerm_app_service_plan.main.id
    
      site_config {
        windows_fx_version = "DOCKER|mcr.microsoft.com/azure-app-service/windows/parkingpage:latest"
        use_32_bit_worker_process = true
      }
    
      app_settings = {
        "WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false"
        "DOCKER_REGISTRY_SERVER_URL"          = "https://mcr.microsoft.com"
      }
    }
    

    I have tested it as well and I don’t see any stack setting in configurations page now.

    enter image description here

    enter image description here

    Note: When you are creating a code based app then you can use kind as “APP/Windows” but creating a docker container app you have to select the kind “xenon” and your windows docker image that you want to use .