Home > Software engineering >  Terraform Azure function app - simple example not spinning up the function properly
Terraform Azure function app - simple example not spinning up the function properly

Time:08-06

Having followed the tutorial in here: https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image?tabs=in-process,cmd,azure-cli&pivots=programming-language-java&fbclid=IwAR1ixY5uUIrx6tKAyMeob7fRD31nSThriPPl_qDJ390BlMDeoMcb0aah584

I am able to spin-up a simple Java Azure Function App, that works as in a demo and can be also reached on the public web URL.

In the second step I tried to do the same, however wanted to deploy with Terraform:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.6.0"
    }
  }
}
    // Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "fnappp" {
  name = "terraskrgroup2"
  location = "North Europe"
}

resource "azurerm_storage_account" "fnappp" {
  name                     = "terraskrstorage2"
  resource_group_name      = azurerm_resource_group.fnappp.name
  location                 = azurerm_resource_group.fnappp.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_service_plan" "fnappp" {
  name                = "serviceplanfnappp2"
  resource_group_name = azurerm_resource_group.fnappp.name
  location            = azurerm_resource_group.fnappp.location
  os_type             = "Linux"
  sku_name            = "EP1"
  worker_count = 1
}


resource "azurerm_application_insights" "fnappp" {
  name                = "tf-test-appinsights2"
  location            = azurerm_resource_group.fnappp.location
  resource_group_name = azurerm_resource_group.fnappp.name
  application_type    = "java"
}

resource "azurerm_linux_function_app" "fnappp" {
  name                = "example-linux-function-final2"
  resource_group_name = azurerm_resource_group.fnappp.name
  location            = azurerm_resource_group.fnappp.location

  storage_account_name = azurerm_storage_account.fnappp.name
  storage_account_access_key = azurerm_storage_account.fnappp.primary_access_key
  service_plan_id      = azurerm_service_plan.fnappp.id
  functions_extension_version = "~3"


  site_config {
    application_stack {
      docker {
        image_name   = "functhird"
        image_tag    = "latest"
        registry_url = "functiontestregistry.azurecr.io"
        registry_username = "REDACTED"
        registry_password = "REDACTED"
      }
    }
    application_insights_connection_string = azurerm_application_insights.fnappp.connection_string
    application_insights_key = azurerm_application_insights.fnappp.instrumentation_key

    http2_enabled = true
  }
}

I am using Azure Container Registry, with the principal with role Owner - same credentials were used to push the docker image to the registry, so this shall be working ok.

Symptoms I observe are:

  1. When I open example-linux-function-final2 in the Function App, within the "Functions" subsesction it doesn't contain any listed function, even though HttpExample should be there
  2. When I ssh into the function (this works) - I end up seeing fully empty container, thus there is no endpoint mapped and the api call /api/HttpExample?name=asdf always results to 404
  3. The exact same image works well if I deploy with Azure CLI instead of terraform, so that one should be fine (tutorial in the top part of the post)
  4. "0 function loaded" log line is present in the log file. From the log file it definitely fetches the image correctly, also runs it, but for some reason fails to find the functions /mappings in the image? Still the other "reference" function created manually is using the exactly same image and it works just fine over there

Would you have some suggestions what might be wrong with the Terraform template?

CodePudding user response:

After digging in deeper, in the following post: functions infrastructure as code

in Custom Container Image section it says:

Also, set WEBSITES_ENABLE_APP_SERVICE_STORAGE to false, since your app content is provided in the container itself

which turned out to fix my issue.

So the full Terraform code to deploy simple Java docker image is as follows:

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.16.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "fnappp" {
  name = "terraskrgroup4"
  location = "North Europe"
}

resource "azurerm_storage_account" "fnappp" {
  name                     = "terraskrstorage4"
  resource_group_name      = azurerm_resource_group.fnappp.name
  location                 = azurerm_resource_group.fnappp.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_service_plan" "fnappp" {
  name                = "serviceplanfnappp4"
  resource_group_name = azurerm_resource_group.fnappp.name
  location            = azurerm_resource_group.fnappp.location
  os_type             = "Linux"
  sku_name            = "EP1"
  worker_count = 1
}


resource "azurerm_application_insights" "fnappp" {
  name                = "tf-test-appinsights4"
  location            = azurerm_resource_group.fnappp.location
  resource_group_name = azurerm_resource_group.fnappp.name
  application_type    = "java"
}

resource "azurerm_linux_function_app" "fnappp" {
  name                = "example-linux-function-final4"
  resource_group_name = azurerm_resource_group.fnappp.name
  location            = azurerm_resource_group.fnappp.location

  storage_account_name = azurerm_storage_account.fnappp.name
  storage_account_access_key = azurerm_storage_account.fnappp.primary_access_key
  service_plan_id      = azurerm_service_plan.fnappp.id
  functions_extension_version = "~3"


  app_settings = {
    WEBSITES_ENABLE_APP_SERVICE_STORAGE = false
  }

  site_config {
    application_stack {
      docker {
        image_name   = "functhird"
        image_tag    = "latest"
        registry_url = "functiontestregistry.azurecr.io"
        registry_username = "REDACTED"
        registry_password = "REDACTED"
      }
    }
    application_insights_connection_string = azurerm_application_insights.fnappp.connection_string
    application_insights_key = azurerm_application_insights.fnappp.instrumentation_key

    http2_enabled = true
  }
}

whereas key modification I made to make it work was adding the:

  app_settings = {
    WEBSITES_ENABLE_APP_SERVICE_STORAGE = false
  }
  • Related