Share via

Best Practices for Azure function for Automation of NHI Account creation in production

Kumar, Dheeraj 60 Reputation points
2026-04-24T10:47:24.55+00:00

I would like to know the best practices of the Azure functions and how to assign required privilege to Managed identity in Graph to create certain operations like app creation, sp creation and secrets etc.

Azure Functions
Azure Functions

An Azure service that provides an event-driven serverless compute platform.


1 answer

Sort by: Most helpful
  1. Pravallika KV 14,640 Reputation points Microsoft External Staff Moderator
    2026-04-27T09:28:19.5433333+00:00

    Hi @Kumar, Dheeraj ,

    Thanks for reaching out to Microsoft Q&A.

    As discussed earlier, Azure Functions can run in several hosting plans, and each has different characteristics in terms of cost, scaling, performance, and memory.

    Consumption Plan Serverless, auto-scaling, pay-per-execution - Auto-scale from zero - No idle cost - Ideal for event-driven workloads - Cold start latency (especially for .NET isolated and Premium features) - Max 1.5 GB memory per instance 1.5 GB Pay per execution and memory-time
    Consumption Plan Serverless, auto-scaling, pay-per-execution - Auto-scale from zero - No idle cost - Ideal for event-driven workloads - Cold start latency (especially for .NET isolated and Premium features) - Max 1.5 GB memory per instance 1.5 GB Pay per execution and memory-time
    Premium Plan Serverless with pre-warmed instances - No cold start - VNET, larger memory (up to 14 GB) - Unlimited execution duration - More expensive than Consumption 3–14 GB Pay for pre-warmed instances + execution
    Dedicated (App Service) Plan Runs on App Service Plan VMs - Predictable performance - Full control over VM size - Can host web apps in same plan - Always on, pay for VMs even if idle - Less efficient for spiky workloads Depends on VM SKU (up to 32 GB+) Pay per VM
    Elastic Premium (Flex Consumption) Premium-like features with more dynamic scaling - Can scale to zero - Higher memory per instance - No cold start - Supports VNET - More complex pricing Up to 14 GB Pay per instance runtime and scaling

    Consumption: Best for small, infrequent workloads. Cost-effective if you have low to medium usage.

    Premium: Best for critical workloads needing low latency, VNET, longer execution, and more memory.

    Flex Consumption: A hybrid; scales to zero like Consumption but supports higher memory and pre-warmed instances.

    Dedicated / App Service: Best if you already have App Service Plan VMs and want predictable performance or hosting multiple apps together.

    Cost and Memory Considerations:

    • Consumption Plan
      • Memory: 1.5 GB per function instance
      • Cost: ~$0.20 per million executions + execution time
      • Cold start: Yes, can affect latency
    • Premium / Flex Consumption
      • Memory: 3–14 GB per instance
      • Cost: Higher, pay for pre-warmed instances + execution
      • Cold start: No
    • Dedicated / App Service
      • Memory: VM-dependent (2–32 GB or more)
      • Cost: Pay per VM/hour, whether functions run or not

    How to assign required privilege to Managed identity in Graph to create certain operations like app creation, sp creation and secrets etc

    This can only be achieved using PowerShell script, but not possible through Portal UI.

    Use Azure AD app registration + delegated permissions

    1. (If you want more control than built-in roles)
    2. Create an AAD app registration representing your Function
    3. Under API Permissions → Add Microsoft Graph → Application permissions:
      • Application.ReadWrite.All
      • Directory.ReadWrite.All (if you manage SPs)
      • (Optionally) AppRoleAssignment.ReadWrite.All
    4. Grant admin consent
    5. Assign that app registration’s service principal roles via Azure CLI/PowerShell to your Function’s managed identity
      • e.g. New-AzureADAppRoleAssignment or az ad app permission grant

    C. Code snippet (PowerShell + Az module)

       
       # 1. Get the Function’s managed identity principalId
       
       $func = Get-AzWebApp -Name myFunctionApp -ResourceGroup myRG
       
       $mi  = (Get-AzUserAssignedIdentity -ResourceGroup myRG -Name myFunctionApp-identity)
       
       # 2. Assign Directory role
       
       New-AzRoleAssignment -ObjectId $mi.PrincipalId `
       
                        -RoleDefinitionName "Application Administrator" `
       
                        -Scope "/"
       
    

    Or use Microsoft Graph SDK calls to add appRoleAssignments.

    Hope this helps!


    If the resolution was helpful, kindly take a moment to click on User's imageand click on Yes for was this answer helpful. And, if you have any further query do let us know

    1 person found this answer helpful.

Your answer

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