Search code examples
azureazure-functionscicd

How to Structure a Single Repository for Multiple Azure Function Apps


I am new to Azure Functions and have successfully deployed a single Function App containing two functions. However, my requirements have changed, and I now need to have two separate Function Apps, each containing a single function.

I am struggling to understand how to structure the project to accommodate multiple Function Apps within a single repository. I have not found much helpful information in the Azure documentation regarding this specific scenario.

Below is the project structure and configuration for single and multiple Function App. I am looking for suggestions on how best to implement it according to best practices.

I managed to successfully deploy the second scenario but the function does not work, so I do not know if I forgot to define something or the path is wrong.

1 Function App 2 functions 1 Function App 2 functions

function_app.py

import azure.functions as func
from function_app_blueprints.http_chatbot_industri import bp as http_chatbot_industri_bp
from function_app_blueprints.http_chatbot_babo import bp as http_chatbot_babo_bp

2 Azure Function Apps with single function in it

2 Azure Function Apps with single function in it

function_app_babo/function_app.py

import azure.functions as func
import logging
from src.chatbot_core.chatbot import Chatbot
from src.common.logging_utils import get_logger

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
   

@app.function_name(name="fun_bamagpt_babo")

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

    logger = get_logger(__name__)
    chatbot = Chatbot(logger)
    logging.info("Initilization completed...")

    # Get question
    req_body = {}
    question = req.params.get('question')
    if not question:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            question = req_body.get('question')
    
    logging.info(f'question: {question}')
    
    app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
    app.register_functions(http_chatbot_industri_bp)
app.register_functions(http_chatbot_babo_bp)

Solution

  • As I mention in comment --prefix is not a valid option for func azure functionapp publish command. As given in this MS document.

    To deploy using this command you need to move to the each function app directory using cd command and run publish command.

    func azure functionapp publish <function-app-name>
    

    My Directory:

    functionapp1/function_app.py:

    import azure.functions as func
    import datetime
    import json
    import logging
    
    app = func.FunctionApp()
    
    @app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS)
    def http_trigger(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 in functionapp1.")
        else:
            return func.HttpResponse(
                 "This HTTP triggered function executed successfully in functionapp1. Pass a name in the query string or in the request body for a personalized response.",
                 status_code=200
            )
    

    OUTPUT: