0

I'm looking for guidance on creating a project structure for multiple functions in one Azure Function App. This is the post I read before Create multiple functions in one Azure Function App

There are two things I can't seem to find a good answer:

  1. Use Visual Studio Code to deploy multiple functions.
  2. From the link above, it seems that I have to create a new function first, but the func azure functionapp publish <App Name> is used to publish app to an existing function.

I tried to deploy using VSCode, and it ended up overwriting to another function.

17
  • func azure functionapp publish <App Name> command will publish the local function to an existing function app. Commented Jan 11 at 7:09
  • @IkhtesamAfrin, it must be done via command line, right? Does VSCode support?
    – indp
    Commented Jan 11 at 7:14
  • You can deploy both ways Commented Jan 11 at 7:15
  • Update - it overwrote another function after I ran the func azure functionapp...
    – indp
    Commented Jan 11 at 7:15
  • Did you mean if you are deploying to an existing function app having one function in it, it is getting overwritten? Commented Jan 11 at 7:17

2 Answers 2

0

In order to deploy multiple functions at once you need to execute func init --worker-runtime python --model V2 command once which will create all the default files needed for a function. Then you can run func new command many times if you would like to add different trigger functions in it.

If you will run func new thrice for creating one HTTP triggered function and 2 timer trigger function then the folder structure and function_app.py would look like below-

Folder Structure-

enter image description here

function_app.py-

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

app = func.FunctionApp()

@app.timer_trigger(schedule="0 */5 * * * *", arg_name="myTimer", run_on_startup=True,
              use_monitor=False) 
def TimerTriggerFunction(myTimer: func.TimerRequest) -> None:
    
    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')

@app.route(route="HttpTriggeredFunction", auth_level=func.AuthLevel.ANONYMOUS)
def HttpTriggeredFunction(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
        )

@app.timer_trigger(schedule="0 0 0/1 * * *", arg_name="myTimer", run_on_startup=True,
              use_monitor=False) 
def TimerTriggeredFunction2(myTimer: func.TimerRequest) -> None:
    
    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')

You can either deploy using func azure functionapp publish <function_APP_Name> command or vs code.

enter image description here

enter image description here

enter image description here

Portal-

enter image description here

4
  • Thanks for the details. I'm not sure if it can be better with different Python files instead of one. Practically, no one would put all the code of 3 functions in a single file like that. I'd expect the function_app.py contains all the triggers, but the logic of each function is stored separately.
    – indp
    Commented Jan 11 at 16:24
  • @indp Yes, you can try to store the logic of each function separately Commented Jan 12 at 10:48
  • How is it possible that you said all code must be in the same function_app.py? Do you have an example?
    – indp
    Commented Jan 12 at 15:51
  • I am not sure if it is feasible, that's why I suggested you to try Commented Jan 12 at 17:19
0

This works well, additionally You could store additional downstream logic in monorepo folder structure.

Then load each via __init__.py

What is __init__.py for?

Not the answer you're looking for? Browse other questions tagged or ask your own question.