Powershell Function App AZ Module Issue

3 minute read


In this post, I will build on my previous post where I created a Powershell Function App that will get the next CIDR Range for a VNET. These are the steps I did to build the Function App in the GUI before using terraform to deploy the Function App later through IaC.

To Resolve:

  1. So using the UI, I deployed a Powershell Function App using runtime ~4. I then went to the Code + Test blade and pasted in a basic HTTP template:

    using namespace System.Net
    param($Request, $TriggerMetadata)
    Write-Output "Starting Function App Invocation..."
    $SubscriptionID = "some-guid"
    Set-AzContext -Subscription $SubscriptionID
    $name = $Request.Query.Name
    $json = @{
          "Details"      = "Successfully logged into development subscription"
          "proposedCIDR" = ""
          "name" = $name
    } | ConvertTo-Json
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
          StatusCode     = [HttpStatusCode]::OK
          Body = $json
    • Notice that I’m calling the Set-AzContext cmdlet because my Function App will be using a Managed Identity to read VNETs.
    • At this point in the development, I did not pass anything into the Query property of the $Request object because I wanted to isolate tesing Az cmdlets first.
  2. Well before you can use any Az cmdlets, you have to enable a few things:

    • First, the host.json managedDependency needs Enabled: true to that the Function App run time will automatically download/install any modules you specify.
    • Next, you specify modules in the requirements.psd1. Usually the Az module is enough, more on that next.
    • That’s it in general, but I had a few more steps since the goal is to read a VNET I had to:
      • Enable System Managed Identity for the Function App
      • Set Reader role of the Function App Managed Identity to the VNET I was targeting.
  3. So the issue I had is that even with that simple HTTP script in step 1, I kept getting errors like:

    Result: Failure Exception: Failed to install function app dependencies. Error: 'Failed to install function app dependencies. Error: 'The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Unable to save the module 'Az'.'' Stack: at Microsoft.Azure.Functions.PowerShellWorker.DependencyManagement.DependencyManager.WaitOnDependencyInstallationTask() in D:\a\_work\1\s\src\DependencyManagement\DependencyManager.cs:line 246 at Microsoft.Azure.Functions.PowerShellWorker.DependencyManagement.DependencyManager.WaitForDependenciesAvailability(Func`1 getLogger) in D:\a\_work\1\s\src\DependencyManagement\DependencyManager.cs:line 164 at Microsoft.Azure.Functions.PowerShellWorker.RequestProcessor.ProcessInvocationRequest(StreamingMessage request) in D:\a\_work\1\s\src\RequestProcessor.cs:line 247
    • And I had set 'Az' = '8.2.0' because that was one less the latest version as of the time of this post at PowershellGallery.

    • So Googling this error did not give me any good hits. I guess I’m just unfortuante that my powershell Function App is having issues loading the Az module.

    • This is no surprise to me though, have you ever tried installing it? i.e. run Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force on a new machine? This takes like 10+ minutes and downloads like 30 submodules.

    • So I found that you have the option of uploading a Custom Module using the Kudo Advanced tools and uploading, but even that timed out when I was trying to upload my az.zip of all the az modules I had installed on my own machine that I had zipped.

    • This got me thinking, why do I even need that whole thing? So then I commented it out and looked up some recent versions of Az.Accounts and Az.Network instead and put those in the requirements.psd1. Then everything started working.

  4. Moral of the story, only import submodules you need your Function App to do. Odd because many sites on the internet freely say to import Az parent instead. The easiest way to determine which modules you need is to lookup cmdlets used in your code and see what module they are part of.