Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure function deployment using terraform

I have the below terraform code to deploy the function app and function to the Azure portal using zip deployment.

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.50.0"
    }
  }
}

resource "azurerm_service_plan" "mssql_to_blob_service_plan" {
  name                = "mssql_to_blob_service_plan-service-plan"
  resource_group_name = azurerm_resource_group.mssql_to_snowflake_etl.name
  location            = var.default_location
  os_type             = "Linux"
  sku_name            = "Y1"
}

locals {
  tmp_azure_function_zip = "../tmp/code.zip"
  azure_function_local_basedir = "../snowflake_sync_function"
}

data "archive_file" "snowpipefunctionzip" {
  type        = "zip"
  output_path = local.tmp_azure_function_zip
  excludes    = ["__pycache__"]

  source {
    content  = file("${local.azure_function_local_basedir}/__init__.py")
    filename = "__init__.py"
  }

  source {
    content  = file("${local.azure_function_local_basedir}/function.json")
    filename = "function.json"
  }

  source {
    content  = file("${local.azure_function_local_basedir}/requirements.txt")
    filename = "requirements.txt"
  }
}


resource "azurerm_linux_function_app" "mssql_to_snowflake_app" {
  name                        = "mssql-to-snowflake-app-new"
  resource_group_name         = azurerm_resource_group.mssql_to_snowflake_etl.name
  location                    = var.default_location
  storage_account_name        = azurerm_storage_account.staging_storage_account.name
  storage_account_access_key  = azurerm_storage_account.staging_storage_account.primary_access_key
  service_plan_id             = azurerm_service_plan.mssql_to_blob_service_plan.id

 
  zip_deploy_file             = data.archive_file.snowpipefunctionzip.output_path # local.tmp_azure_function_zip
  site_config {}
  app_settings = {
    "WEBSITE_RUN_FROM_PACKAGE" = "0"
  }
}

resource "azurerm_function_app_function" "mssql_to_snowflake_app_func" {
  name            = "mssql-to-snowflake-function"
  function_app_id = azurerm_linux_function_app.mssql_to_snowflake_app.id
  language        = "Python"
  test_data = jsonencode({
    "name" = "Azure"
  })

  config_json = jsonencode({
    "bindings" = [
      {
        "authLevel" = "function"
        "direction" = "in"
        "methods" = [
          "get",
          "post",
        ]
        "name" = "req"
        "type" = "timerTrigger"
      },
      {
        "direction" = "out"
        "name"      = "$return"
        "type"      = "http"
      },
    ]
  })
}

I am using the latest terraform provider for Azurerm, I get this error:

Error: creating Function App Function: (Function Name "mssql-to-snowflake-function" / Site Name "mssql-to-snowflake-app-new" / Resource Group "mssql_to_snowflake_etl_rg") - State: "Running" / InProgressOperationID:
with azurerm_function_app_function.mssql_to_snowflake_app_func on azure_fun.tf line 63, in resource "azurerm_function_app_function" "mssql_to_snowflake_app_func": resource "azurerm_function_app_function" "mssql_to_snowflake_app_func" { creating Function App Function: (Function Name "mssql-to-snowflake-function" / Site Name "mssql-to-snowflake-app-new" / Resource Group "mssql_to_snowflake_etl_rg") - State: "Running" / InProgressOperationID:

Could someone please help in sorting the details of the below error or help with any documentation or example which explains deploying the Azure function using terraform zip deployment using the latest terraform Azure provider?

like image 565
mahesh Avatar asked Aug 13 '26 17:08

mahesh


1 Answers

You appear to be using the zip_deploy_file attribute. According to the documentation Using this value requires either WEBSITE_RUN_FROM_PACKAGE=1 or SCM_DO_BUILD_DURING_DEPLOYMENT=true to be set on the App in app_settings.

Your function app is configured to WEBSITE_RUN_FROM_PACKAGE=0.

Azure functions can either have code deployed as a zip package or have code deployed inline for each function. The error you receive is actually on the function app function which would make sense if the code package isn’t loaded then your reference to the function wouldn’t work because the package isn’t loaded.

Can you share your code package? Or at least a sample one?

like image 114
markti Avatar answered Aug 15 '26 10:08

markti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!