Create multiple functions withing a single function app service

Aditi Mokashi 40 Reputation points
2024-12-19T13:34:40.9233333+00:00

I want to create multiple HttpTrigger functions under a single function app service. For example, there are 2 functions, automation1 and automation2, they should be deployed under same function app service but they should be allowed to be called individually i.e. 1st function be triggered using api/automation1 and 2nd one should be triggered using api/automation2.

Please let me know if this is possible, please provide the steps to implement this. If not, please provide any alternate approaches on azure to do so. Following is my current configuration:

language - python 3.9

os - linux runtime version 4.10

plan - plan-

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,336 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,199 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sina Salam 15,891 Reputation points
    2024-12-19T15:26:14.9033333+00:00

    Hello Aditi Mokashi,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to create multiple functions withing a single Azure function app service.

    Yes. It is absolutely possible to deploy multiple HTTP-triggered functions within a single Azure Function App, and make each accessible through distinct endpoints. I will provide you an extensive tested steps and best practices, you can successfully deploy multiple HTTP-triggered functions within a single Function App while ensuring efficient management and scalability.

    NOTE:

    • If Cost Efficiency Is a Priority stick to a single function app with the outlined setup. Monitor resource utilization to avoid bottlenecks.
    • If Scalability or Advanced Features Are Needed use API Management or separate function apps for more control and scalability.

    To Implement Multiple HTTP Triggers

    1. Begin by creating a Function App in the Azure portal. Select "Create a resource" and choose "Function App." Specify your subscription, resource group, and app name. Ensure the runtime stack is Python 3.9, the operating system is Linux, and the hosting plan is set to P1v3. This configuration supports scalability and aligns with your requirements.
    2. On your local machine, install Azure Functions Core Tools and the Azure Functions extension for Visual Studio Code. These tools simplify project creation and deployment.
    3. In Visual Studio Code, initialize a new Azure Functions project. Add two HTTP-triggered functions, automation1 and automation2, and organize them into separate directories as shown:
    my-function-app/  
       ├── automation1/  
       │   ├── __init__.py  
       │   ├── function.json  
       ├── automation2/  
       │   ├── __init__.py  
       │   ├── function.json  
       ├── host.json  
       ├── local.settings.json  
    

    This will help you to create Functions with unique endpoints.

    1. Write the business logic for each function in its respective __init__.py file:
    • automation1/init.py:
         import logging  
         import azure.functions as func  
         def main(req: func.HttpRequest) -> func.HttpResponse:  
             logging.info('Processing request for automation1.')  
             return func.HttpResponse("Hello from automation1!", status_code=200)  
    
    • automation2/init.py:
         import logging  
         import azure.functions as func  
         def main(req: func.HttpRequest) -> func.HttpResponse:  
             logging.info('Processing request for automation2.')  
             return func.HttpResponse("Hello from automation2!", status_code=200)  
    
    1. Define HTTP trigger bindings in the function.json files to set up unique routes for each function:
      • automation1/function.json:
         {  
             "bindings": [  
                 {  
                     "authLevel": "function",  
                     "type": "httpTrigger",  
                     "direction": "in",  
                     "name": "req",  
                     "methods": ["get", "post"],  
                     "route": "automation1"  
                 },  
                 {  
                     "type": "http",  
                     "direction": "out",  
                     "name": "res"  
                 }  
             ]  
         }  
         ```  
       - `automation2/function.json`:  
         ```json  
         {  
             "bindings": [  
                 {  
                     "authLevel": "function",  
                     "type": "httpTrigger",  
                     "direction": "in",  
                     "name": "req",  
                     "methods": ["get", "post"],  
                     "route": "automation2"  
                 },  
                 {  
                     "type": "http",  
                     "direction": "out",  
                     "name": "res"  
                 }  
             ]  
         }  
    
    1. Then, at this stage using Visual Studio Code to deploy the project to Azure. Right-click the project and choose Deploy to Function App. This action publishes your functions to the cloud, where they can be accessed through api/automation1 and api/automation2.

    Alternative Approaches

    1. If advanced features such as centralized management, rate limiting, or caching are required, integrate Azure API Management. You can configure automation1 and automation2 as backend APIs, exposing them through consistent endpoints like /api/v1/automation1.
    2. For scenarios requiring isolated scaling or deployment, consider creating separate Function Apps for each HTTP trigger. This approach improves resource allocation and fault isolation but may increase administrative overhead.

    To ensure best practices:

    Use a Premium or Dedicated Plan to mitigate cold start delays and handle high traffic efficiently to - Optimize Resource Allocation.

    Apply appropriate authorization levels (function, admin, or anonymous) to protect your endpoints.

    Integrate Azure Application Insights to track performance metrics and debug issues for each function individually.

    Then, use function-specific error handling and retries to ensure that a failure in one function does not disrupt others.

    If you will need to read more details about the above solution, use the below references:

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. TP 102K Reputation points
    2024-12-19T14:16:53.2466667+00:00

    Hi Aditi,

    Yes, what you are describing is possible. The precise steps will vary depending on the development environment (VSCode, Visual Studio, etc.) you plan to use for writing your functions. I created quick example (it is consumption plan, but using P1v3 is similar), screenshot below:

    User's image

    In above example you would use https://contoso.azurewebsites.net/api/automation1 to call automation1 function, and https://contoso.azurewebsites.net/api/automation2 to call automation2 function.

    Below is walkthrough showing how to create function app and functions using Visual Studio Code:

    https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-vs-code?tabs=node-v4%2Cpython-v2%2Cisolated-process%2Cquick-create&pivots=programming-language-python

    In the article it describes adding a function to your function project. In your case you will need to add a second function.

    NOTE: Instead of using VS Code you code create your functions via the portal using Create button as shown in screenshot above, however, this is typically only for testing/demo purposes. In most cases you should be using a local development environment.

    Please give it a try and add a comment below if something is unclear.

    Please click Accept Answer and upvote if the above was helpful.

    Thanks.

    -TP

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.