1

I am deploying a very basic Azure Functions App to demonstrate a few key features.

I have two functions, one demonstrating an HTTP Trigger and the other demonstrating a Timer Trigger. Both run perfectly on local instance.

import azure.functions as func
import os
import datetime
import logging

app = func.FunctionApp()

@app.function_name(name="HttpTrigger1")
@app.route(route="keyvaulttest")
def test_function(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    test_phrase = os.getenv("TestEnvFromKeyVault")

    logging.info(f'TestEnvFromKeyVault: {test_phrase}')
    logging.info('Python HTTP trigger function ran at %s', utc_timestamp)
 
    return func.HttpResponse(
        test_phrase,
        status_code=200
    )

@app.function_name(name="TestTimer")
@app.schedule(schedule="0 */5 * * * *", arg_name="test_timer", use_monitor=False) 
def test_function(test_timer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    test = os.getenv("TestEnvFromKeyVault")

    if test_timer.past_due:
        logging.info('The timer is past due!')

    logging.info(f'TestEnvFromKeyVault: {test}')
    logging.info('Python timer trigger function ran at %s', utc_timestamp)

When I attempt to deploy using the VSCode Azure Function extension command "Azure Functions: Deploy to FunctionApp" it says it deployed successfully. My HTTP Trigger function is deployed and works, but my Timer Trigger function is not deployed.

12:13:48 PM testapp: Deployment successful. deployer = ms-azuretools-vscode deploymentPath = Functions App ZipDeploy. Extract zip. Remote build.

enter image description here

Rob S.
  • 1,044
  • 7
  • 25
  • Note that the arguments for the various triggers don't play well when using underscores. Although not Pythonic, I'd advise the use of camelCase or PascalCase as these fixed the issue for me. – TimeTerror Mar 09 '23 at 14:59

2 Answers2

1

I did what the above solution did (including the configurations) and still found myself in the same issue described by OP. After getting on a call w Microsoft support and everything, ultimately what did it was deleting the line :

@app.function_name(name="HttpTrigger1")

...apparently in v2 the function name is read from def test_function like any other function would and the above line is causing interference reading the function when uploaded.

thatguy1155
  • 102
  • 1
  • 8
0

I have reproduced in my environment by taking both the HTTP and Timer Trigger Functions in the same Function Class file - Python Version 3.9.13

Local Execution:

enter image description here

Configuration:

  1. Created Azure Function App (Python) and Key Vault.
  2. Enabled System Assigned Managed Identity in Azure Function App.
  3. In Key Vault, Added the Secret with the value and also in Access Configuration > Add Policy > Key and Secret Management, Principal - Function App System Assigned Managed Identity Object Id.
  4. Add the Key Vault Name App Setting and Python v2 model app setting called "AzureWebJobsFeatureFlags": "EnableWorkerIndexing" to the Function App Configuration.

Azure Cloud Portal - Function App Execution:

  1. Published to Azure Portal Function App using below Command:
func azure functionapp publish <your_FunctionApp_Name_inTheAzurePortal>
  1. After Publishing to the Azure Portal Function App, I have added 2 app settings in the Configuration Menu of Azure Function App:

enter image description here

Note: You can add them either before or after publishing to the Azure Portal.

And I can see both the Functions:

enter image description here

Http Trigger Function Run:

enter image description here

Timer Trigger Function Run:

enter image description here

Code Snippets:

function_app.py:

import  azure.functions  as  func
import  logging
import  datetime
import  os
from  azure.keyvault.secrets  import  SecretClient
from  azure.identity  import  DefaultAzureCredential

app = func.FunctionApp()
@app.function_name(name="HttpTrigger1")     #Http_Trigger
@app.route(route="hello")

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

utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()

keyVaultName = os.environ["KEY_VAULT_NAME"]
logging.info(f'TestEnvFromKeyVault: {keyVaultName}')
logging.info('Python Http trigger function ran at %s', utc_timestamp)
return  func.HttpResponse("Hello Krishna, This HTTP triggered function executed successfully.",
status_code=200
)

# Timer_Trigger_in_same_function

@app.function_name(name="mytimer")
@app.schedule(schedule="0 */1 * * * *", arg_name="mytimer", run_on_startup=True,
use_monitor=False)
def  test_function(mytimer: func.TimerRequest) -> None:

utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()

if  mytimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function ran at %s', utc_timestamp)

host.json:

{
    "version": "2.0",
    "logging": {
    "applicationInsights": {
    "samplingSettings": {
    "isEnabled": true,
    "excludedTypes": "Request"
        }
    }
},
"extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.15.0, 4.0.0)"
    }
}

requirements.txt file:

azure-functions
azure.keyvault.secrets
azure.identity
azure-keyvault

local.settings.json:

{

"IsEncrypted": false,
"Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=krishfunappstorage;AccountKey=<Storage-Account-Access_Key>;EndpointSuffix=core.windows.net",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "KEY_VAULT_NAME":"krishkv4funapp"
}
}

Above parts of code sample taken from the below references:

  1. Python Azure Functions Code from this MS Doc.
  2. Function App Publish Command from this MS Doc Reference
  • maybe it is my python version 3.10 that I am using... I do exactly what you do and when I deploy it using the cli command it acts like it deploys and then there is nothing in my function app – Rob S. Feb 08 '23 at 00:53
  • i just tried again without using the v2 project and it worked the first time. I would like to use V2 if that is possible – Rob S. Feb 08 '23 at 00:54
  • 1
    @RobS. I have used v2 model of Python in the above practical and will update you in the answer with Python 3.10.x version of Azure Functions in above scenario. –  Feb 08 '23 at 03:53