簡體   English   中英

FunctionApp 未導入 python 模塊

[英]FunctionApp not importing python module

我花了幾天時間嘗試使用 Terraform 部署 function 應用程序。 它利用了請求 python 模塊。

我已將問題縮小到 Azure 沒有在 requirements.txt 中導入 python 模塊。 文件。

為了測試,我創建了一個簡單的 httptrigger function 應用程序,它只返回一個 url 參數。 我的部署腳本壓縮目錄並使用 terraform 部署應用程序。 一切都很好,一切都按預期工作。

But, if change my python script ( __init__.py ) to import the python modules “urllib3” and/or “requests,” The function fails, even though the simple python script doesn't actually use any methods or classes from the modules. 部署 function 時沒有錯誤。 從 url 調用時失敗。

我驗證了這些模塊在 requirements.txt 文件中列出。

可以肯定的是,如果我注釋掉導入請求和 urllib3,一切正常。

任何意見或建議將不勝感激。

簡單的測試腳本( __init__.py ):

import logging
import azure.functions as func
# function fails with the following line.  Works if commented out.
import urllib3



def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    target = req.params.get('target')
    if target:
        return func.HttpResponse("Pretending to use " + target, status_code = 503)
    else:
        return func.HttpResponse("No Taget Specified", status_code = 400)

requirements.txt(7/15 更新)

azure-functions==1.2.1
future==0.18.2
pefile==2019.4.18
PyInstaller==3.6
pylint==2.5.3
pywin32-ctypes==0.2.0
PyYAML==5.3.1
urllib3==1.25.9
requests==2.24.1

7/16 更新:Terraform 文件:

variable "prefix" {
    type = "string"
    default = "jis"
}

variable "location" {
    type = "string"
    default = "eastus"
}

variable "environment" {
    type = "string"
    default = "dev"
}

variable "functionapp" {
    type = "string"
    default = "./testit.zip"
}

resource "random_string" "storage_name" {
    length = 24
    upper = false
    lower = true
    number = true
    special = false
}

provider "azurerm" {
  version = "~>2.1.0"
  features {}
}

# Create Storage Account

resource "azurerm_resource_group" "rg" {
    name = "${var.prefix}-${var.environment}"
    location = "${var.location}"
}

resource "azurerm_storage_account" "storage" {
    name = "${random_string.storage_name.result}"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    location = "${var.location}"
    account_tier = "Standard"
    account_replication_type = "LRS"
}

resource "azurerm_storage_container" "deployments" {
    name = "function-releases"
    storage_account_name = "${azurerm_storage_account.storage.name}"
    container_access_type = "private"
}

# upload zip file

resource "azurerm_storage_blob" "appcode" {
    name = "functionapp.zip"
    storage_account_name = "${azurerm_storage_account.storage.name}"
    storage_container_name = "${azurerm_storage_container.deployments.name}"
    type = "Block"
    source = "${var.functionapp}"
}

# Create function app

data "azurerm_storage_account_sas" "sas" {
    connection_string = "${azurerm_storage_account.storage.primary_connection_string}"
    https_only = true
    start = "2020-07-10"
    expiry = "2023-12-31"
    resource_types {
        object = true
        container = false
        service = false
    }
    services {
        blob = true
        queue = false
        table = false
        file = false
    }
    permissions {
        read = true
        write = false
        delete = false
        list = false
        add = false
        create = false
        update = false
        process = false
    }
}

resource "azurerm_app_service_plan" "asp" {
    name = "${var.prefix}-plan"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    location = "${var.location}"
    kind = "Linux"
    reserved = true
    sku {
        tier = "Dynamic"
        size = "Y1"
    }
}

resource "azurerm_function_app" "functions" {
    name = "${var.prefix}-${var.environment}"
    location = "${var.location}"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    app_service_plan_id = "${azurerm_app_service_plan.asp.id}"
    storage_connection_string = "${azurerm_storage_account.storage.primary_connection_string}"
    version = "~2"

    app_settings = {
        https_only = true
        FUNCTIONS_WORKER_RUNTIME = "python"
        WEBSITE_NODE_DEFAULT_VERSION = "~10"
        FUNCTION_APP_EDIT_MODE = "readonly"
        HASH = "${base64encode(filesha256("${var.functionapp}"))}"
        WEBSITE_RUN_FROM_PACKAGE = "https://${azurerm_storage_account.storage.name}.blob.core.windows.net/${azurerm_storage_container.deployments.name}/${azurerm_storage_blob.appcode.name}${data.azurerm_storage_account_sas.sas.sas}"
    }
}

# 

可能是版本兼容性問題。

您使用的 urllib3 和 requests 的版本來自 2018 年 6 月,其他(未檢查全部)似乎是最新版本。

為什么需要使用舊版本?

經過一番掙扎,我發現了問題所在。 感謝@HuryShen 和@AnkitKumar 為我指明了正確的方向。

我沒有在 function zip 文件中包含模塊 urllib3 和 requests。 它們需要安裝到<funcname>.python_packages/lib/site-packages directory 最簡單的方法(來自 requirements.txt 文件)是

pip install  --target="azure/.python_packages/lib/site-packages"  -r requirements.txt

下載模塊后,它們可以包含在 zip 文件中。 之后,terraform 腳本工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM