Azure Functions:如何运行Linux消耗Zip部署?

huangapple go评论51阅读模式
英文:

Azure Functions: How to run Linux Consumption Zip Deploy?

问题

I am running Linux on WSL and want to publish the hello-world example to an Azure Function using the Linux consumption plan on Python 3.10.

My hello world example is:

import azure.functions as func
import logging

app = func.FunctionApp(auth_level=func.AuthLevel.ANONYMOUS)

@app.function_name(name="HttpTrigger1")
@app.route(route="hello")
def test_function(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
    else:
        name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
            "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
            status_code=200
        )

My local.settings.json is:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "SCM_DO_BUILD_DURING_DEPLOYMENT": true,
    "AzureWebJobsStorage": "redacted",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "WEBSITE_CONTENTSHARE": "redacted",
    "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING": "redacted"
  }
}

requirements.txt:

azure-functions

I am deploying by zipping my folder and then using the az cli:

az functionapp deployment source config-zip -g rg_here -n fun_name_here --src my_zip_file.zip

In fact, the above nor from the terminal will work, either.

func azure functionapp publish my_fun_name

By not work, I mean that I get either this response:

No HTTP triggers found.

or

Your app is currently in read-only mode because you are running from a package file. To make any changes, update the content in your zip file and WEBSITE_RUN_FROM_PACKAGE app setting.

And my functions are not visible on the portal or VS Code.

英文:

I am running Linux on WSL and want to publish the hello-world example to an Azure Function using the Linux consumption plan on Python 3.10.

This is not a solution for me: https://stackoverflow.com/questions/71070103/zip-deploy-azure-function-from-storage-account

My hello world example is:

import azure.functions as func
import logging

app = func.FunctionApp(auth_level=func.AuthLevel.ANONYMOUS)

@app.function_name(name="HttpTrigger1")
@app.route(route="hello")
def test_function(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
    else:
        name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
            "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
            status_code=200
    )

My local.settings.json is:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "SCM_DO_BUILD_DURING_DEPLOYMENT": true,
    "AzureWebJobsStorage": "redacted",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "WEBSITE_CONTENTSHARE": "redacted",
    "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING": "redacted",
    
  }
}

requirements.txt:

azure-functions

I am deploying by zipping my folder and then using the az cli:

az functionapp deployment source config-zip -g rg_here -n fun_name_here --src my_zip_file.zip

In fact the above nor from the terminal will work, either.

func azure functionapp publish my_fun_name

By not work, I mean that I get either this response:

No HTTP triggers found.

or

Your app is currently in read only mode because you are running from a package file. To make any changes update the content in your zip file and WEBSITE_RUN_FROM_PACKAGE app setting.

And my functions are not visible on the portal or VS Code.

答案1

得分: 1

I have followed the commands from the SO 71617500 and it worked for me.

根据MS文档1,为了使您的函数应用从包中运行,需要在运行在Linux上的消费计划中的函数应用的应用设置中添加WEBSITE_RUN_FROM_PACKAGE=<URL>设置。

首先,在函数应用配置中设置"SCM_DO_BUILD_DURING_DEPLOYMENT": true的值,WEBSITE_RUN_FROM_PACKAGE会在函数应用创建期间自动添加。

Azure Functions:如何运行Linux消耗Zip部署?

init.py

import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')
    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
            "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
            status_code=200
        )

local.settings.json

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "",
        "FUNCTIONS_WORKER_RUNTIME": "python"
    }
}

我只压缩了以下函数文件

Azure Functions:如何运行Linux消耗Zip部署?

使用以下命令从本地CLI部署zip文件

az functionapp deployment source config-zip -g iafrin-RG -n afreenfuncapp0123 --src "C:\Users\iafrin\Documents\functionApp\demo-file\hello-world-test-file.zip"

它成功部署了

Azure Functions:如何运行Linux消耗Zip部署?

我能够在门户中看到该函数以及警告,在vs code中也以只读模式查看文件。

Azure Functions:如何运行Linux消耗Zip部署?

Azure Functions:如何运行Linux消耗Zip部署?

您的应用当前处于只读模式,因为它正在从包文件运行。要进行任何更改,请更新zip文件中的内容和WEBSITE_RUN_FROM_PACKAGE应用设置。

上面的警告说明,我无法从门户编辑我的文件,因为它正在从包中运行,并且根据MS文档2,门户编辑仅适用于Linux上的HTTP和定时器触发器,使用高级和专用计划的函数,而不适用于消费计划。

英文:

I have followed the commands from the SO 71617500 and it worked for me.

As per MS document1, to enable your function app to run from a package, add a WEBSITE_RUN_FROM_PACKAGE=<URL> setting to your app settings for functions apps running on Linux in a Consumption plan.

Firstly I set the value as "SCM_DO_BUILD_DURING_DEPLOYMENT": true in function app configuration and WEBSITE_RUN_FROM_PACKAGE was automatically added during function app creation.

Azure Functions:如何运行Linux消耗Zip部署?

init.py

import  logging
import  azure.functions  as  func
def  main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name  =  req.params.get('name')
if  not  name:
try:
req_body  =  req.get_json()
except  ValueError:
pass
else:
name  =  req_body.get('name')
if  name:
return  func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
return  func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)

local.settings.json

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python"
}
}

I have only zipped the following function files

Azure Functions:如何运行Linux消耗Zip部署?

Used the below command to deploy the zip file from local cli

az functionapp deployment source config-zip -g iafrin-RG -n afreenfuncapp0123 --src "C:\Users\iafrin\Documents\functionApp\demo-file\hello-world-test-file.zip"

It got deployed successfully

Azure Functions:如何运行Linux消耗Zip部署?

I was able to see the function in the portal along with the warning and in vs code I was able to files in read-only mode as well.

Azure Functions:如何运行Linux消耗Zip部署?

Azure Functions:如何运行Linux消耗Zip部署?

Your app is currently in read only mode because you are running from a package file. To make any changes update the content in your zip file and WEBSITE_RUN_FROM_PACKAGE app setting.

Above warning says, I can't edit my files from the portal as It is running from a package and according to MS document2, portal editing is enabled only for HTTP and Timer triggers for Functions on Linux using Premium and Dedicated plans not for Consumption plan.

huangapple
  • 本文由 发表于 2023年5月30日 10:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定