Azure not installing python packages from requirements.txt when using Terraform with azurerm_linux_function_app

Kristoffer Strekerud 0 Reputation points
2024-09-19T13:03:32.38+00:00

I'm trying to install a python package on my running Azure Function, but it seems Azure it not installing it. I've checked Kudu, and tried having the requirements.txt both in the "wwwroot" folder and the folder that the function is in (which is deployed correctly).

I've searched and looked a lot, but nothing I've tried seems to resolve the problem.

The function (run.py) is deployed fine and works as intended, but if I try to import the libraries I have in requirements.txt I get "module not found".

No module named 'psycopg2'. Cannot find module. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound.

Can anyone see what might be going on here, or know how to correctly install python packages when running Azure functions?

The Terraform code:

resource "azurerm_linux_function_app" "function_app" {
  name                = "func-${var.company_name}-log-001-${var.environment}"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location

  storage_account_name       = azurerm_storage_account.storage_account_func.name
  storage_account_access_key = azurerm_storage_account.storage_account_func.primary_access_key
  service_plan_id            = azurerm_service_plan.appservice.id

  virtual_network_subnet_id  = azurerm_subnet.subnet_func_app.id
  
  app_settings = {
      "AzureWebJobsStorage"            = azurerm_storage_account.storage_account_func.primary_connection_string
      "FUNCTIONS_WORKER_RUNTIME"       = "python"
      "APPINSIGHTS_INSTRUMENTATIONKEY" = azurerm_application_insights.app_insights.instrumentation_key
      "KeyVaultName"                   = azurerm_key_vault.key_vault.name
      "SCM_DO_BUILD_DURING_DEPLOYMENT" = true,
      "ENABLE_ORYX_BUILD"              = true
    }
  site_config {
    application_stack {
      python_version = "3.11"
    }
  }

  identity {
    type = "SystemAssigned"
  }
}


resource "azurerm_function_app_function" "function_app_function" {
  name            = "func-${var.company_name}-func-log-001-${var.environment}"
  function_app_id = azurerm_linux_function_app.function_app.id
  language = "Python"

  file {
    name    = "run.py"
    content = file("code-scripts/run.py")
  }

  file {
    name    = "requirements.txt"
    content = file("code-scripts/requirements.txt")
  }
  
  config_json = jsonencode({
    "bindings" = [
      {
        authLevel      = "function"
        type           = "eventGridTrigger"
        direction      = "in"
        name           = "eventGridEvent"
      }
    ]
  })
}

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,029 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pinaki Ghatak 4,380 Reputation points Microsoft Employee
    2024-09-20T09:02:19.5466667+00:00

    Hello @Kristoffer Strekerud

    Based on the code you provided, it looks like you have correctly specified the requirements.txt file in the azurerm_function_app_function resource.

    However, it's possible that the packages are not being installed because the requirements.txt file is not being found or is not being processed correctly.

    One thing you can try is to add a WEBSITE_RUN_FROM_PACKAGE app setting to your azurerm_linux_function_app resource. This setting tells Azure Functions to run your function app from a package file, which includes your code and dependencies. You can create a package file by running pip install -r requirements.txt -t . in the root directory of your function app, which will install the packages specified in requirements.txt in the site-packages directory.

    Then, you can create a zip file of your function app by running zip -r functionapp.zip . in the root directory of your function app. Finally, you can upload the zip file to a storage account and set the WEBSITE_RUN_FROM_PACKAGE app setting to the URL of the zip file.

    Here's an example of how to add the WEBSITE_RUN_FROM_PACKAGE app setting to your azurerm_linux_function_app

    resource xx  app_settings = { 
    AzureWebJobsStorage = azurerm_storage_account.storage_account_func.primary_connection_string 
    FUNCTIONS_WORKER_RUNTIME = python 
    APPINSIGHTS_INSTRUMENTATIONKEY = azurerm_application_insights.app_insights.instrumentation_key 
    KeyVaultName = azurerm_key_vault.key_vault.name 
    SCM_DO_BUILD_DURING_DEPLOYMENT = true
    ENABLE_ORYX_BUILD = true 
    WEBSITE_RUN_FROM_PACKAGE = "https://.blob.core.windows.net//functionapp.zip" 
    }
    
    

    Replace the name of your storage account and container, respectively.

    I hope this helps

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.