Home > OS >  Azure functions with powershell runtime fails to load modules
Azure functions with powershell runtime fails to load modules

Time:07-29

Have a simple Azure powershell Function. This is triggered by EventGrid, connected to a storage container. Basic function works without any gripe. The moment I add module dependency everything runtime barfs. As you can see, I included Storage, Account & compute dependency. I tried both complete version and wildcard version. Both fail. I'd appreciate if someone can tell me what is amiss

Requirements.psd1

# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
    # For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'. 
    # To use the Az module in your function app, please uncomment the line below.
    Az = '8.*'
    Accounts = '2.*'
    Compute = '4.*'
    Storage = '4.*'
}

In order for these modules to be included I added import statements in profile.ps1

# import statements
Import-Module Az.Accounts
Import-Module Az.Compute
Import-Module Az.Storage


# Authenticate with Azure PowerShell using MSI.
# Remove this if you are not planning on using MSI or Azure PowerShell.
if ($env:MSI_SECRET) {
    Disable-AzContextAutosave -Scope Process | Out-Null
    Connect-AzAccount -Identity
}

run.ps1 file is simple. It parses the input and destructure's all input for processing and attempts to ivoke a remote VM to execute a script. this is where I need compute module

param($eventGridEvent, $TriggerMetadata)

# Make sure to pass hashtables to Out-String so they're logged correctly
# $eventGridEvent | Out-String | Write-Host
# $response = $eventGridEvent | ConvertTo-JSON -Depth 5 | Out-String -Width 200
$topic = $eventGridEvent.topic
$api = $eventGridEvent.data.api
$file = $eventGridEvent.data.url
$fileType = $eventGridEvent.data.contentType
# check for container name and file type and invoke appropriate
# operation in VM
Write-Information "A file change operation happened in $topic"
Write-Host "A file change operation happened in $topic"
Write-Host "$api was invoked on $file of type $fileType"
Write-Host "START execution of script in remote host"
Invoke-AzVMRunCommand -ResourceGroupName 'autosys100-rg' -Name 'autosyslinuxvm' -CommandId 'RunShellScript' -ScriptPath 'install_nginx.sh'
Write-Host "COMPLETED execution of script on remote host"

There's nothign fancy and ludicrously basic :(

Here's the error

Connected!
2022-07-25T21:41:19  Welcome, you are now connected to log-streaming service. The default timeout is 2 hours. Change the timeout with the App Setting SCM_LOGSTREAM_TIMEOUT (in seconds).
2022-07-25T21:41:28.181 [Information] Executing 'Functions.egExample' (Reason='EventGrid trigger fired at 2022-07-25T21:41:28.1674630 00:00', Id=718522d6-3705-4be8-b4dc-1e1f93383c9f)
2022-07-25T21:41:28.236 [Warning] The first managed dependency download is in progress, function execution will continue when it's done. Depending on the content of requirements.psd1, this can take a few minutes. Subsequent function executions will not block and updates will be performed in the background.
2022-07-25T21:41:28.333 [Error] Executed 'Functions.egExample' (Failed, Id=718522d6-3705-4be8-b4dc-1e1f93383c9f, Duration=115ms)Result: FailureException: Failed to install function app dependencies. Error: 'Failed to get latest version for module 'Storage' with major version '4'.  'Stack:    at Microsoft.Azure.Functions.PowerShellWorker.DependencyManagement.DependencyManager.WaitOnDependencyInstallationTask() in /mnt/vss/_work/1/s/src/DependencyManagement/DependencyManager.cs:line 246at Microsoft.Azure.Functions.PowerShellWorker.DependencyManagement.DependencyManager.WaitForDependenciesAvailability(Func`1 getLogger) in /mnt/vss/_work/1/s/src/DependencyManagement/DependencyManager.cs:line 164at Microsoft.Azure.Functions.PowerShellWorker.RequestProcessor.ProcessInvocationRequest(StreamingMessage request) in /mnt/vss/_work/1/s/src/RequestProcessor.cs:line 247
2022-07-25T21:41:38.569 [Information] Executing 'Functions.egExample' (Reason='EventGrid trigger fired at 2022-07-25T21:41:38.5683456 00:00', Id=57490df5-c718-436e-87e3-211406b00f9d)
2022-07-25T21:41:39.236 [Warning] The Function app may be missing the 'Az.Accounts' module. If 'Az.Accounts' is available on the PowerShell Gallery, add a reference to this module to requirements.psd1. Make sure this module is compatible with PowerShell 7. For more details, see https://aka.ms/functions-powershell-managed-dependency.
2022-07-25T21:41:40.002 [Error] ERROR: The specified module 'Az.Accounts' was not loaded because no valid module file was found in any module directory.Exception             :Type    : System.IO.FileNotFoundExceptionMessage : The specified module 'Az.Accounts' was not loaded because no valid module file was found in any module directory.HResult : -2147024894TargetObject          : Az.AccountsCategoryInfo          : ResourceUnavailable: (Az.Accounts:String) [Import-Module], FileNotFoundExceptionFullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommandInvocationInfo        :MyCommand        : Import-ModuleScriptLineNumber : 13OffsetInLine     : 1HistoryId        : 1ScriptName       : C:\home\site\wwwroot\profile.ps1Line             : Import-Module Az.AccountsPositionMessage  : At C:\home\site\wwwroot\profile.ps1:13 char:1  Import-Module Az.Accounts  ~~~~~~~~~~~~~~~~~~~~~~~~~PSScriptRoot     : C:\home\site\wwwrootPSCommandPath    : C:\home\site\wwwroot\profile.ps1InvocationName   : Import-ModuleCommandOrigin    : InternalScriptStackTrace      : at <ScriptBlock>, C:\home\site\wwwroot\profile.ps1: line 13PipelineIterationInfo :Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException : Result: ERROR: The specified module 'Az.Accounts' was not loaded because no valid module file was found in any module directory.Exception             :Type    : System.IO.FileNotFoundExceptionMessage : The specified module 'Az.Accounts' was not loaded because no valid module file was found in any module directory.HResult : -2147024894TargetObject          : Az.AccountsCategoryInfo          : ResourceUnavailable: (Az.Accounts:String) [Import-Module], FileNotFoundExceptionFullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommandInvocationInfo        :MyCommand        : Import-ModuleScriptLineNumber : 13OffsetInLine     : 1HistoryId        : 1ScriptName       : C:\home\site\wwwroot\profile.ps1Line             : Import-Module Az.AccountsPositionMessage  : At C:\home\site\wwwroot\profile.ps1:13 char:1  Import-Module Az.Accounts  ~~~~~~~~~~~~~~~~~~~~~~~~~PSScriptRoot     : C:\home\site\wwwrootPSCommandPath    : C:\home\site\wwwroot\profile.ps1InvocationName   : Import-ModuleCommandOrigin    : InternalScriptStackTrace      : at <ScriptBlock>, C:\home\site\wwwroot\profile.ps1: line 13PipelineIterationInfo :Exception: The specified module 'Az.Accounts' was not loaded because no valid module file was found in any module directory.Stack:

CodePudding user response:

Your requirements.psd1 has incorrect module names as you have forgotten the Az. prefix.

@{
    # For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'. 
    # To use the Az module in your function app, please uncomment the line below.
    # Az = '8.*'
    'Az.Accounts' = '2.*'
    'Az.Compute' = '4.*'
    'Az.Storage' = '4.*'
}

I would usually recommend not installing the entire Az module as it takes a long time however the compute, accounts and storage modules should be installed with the Az module so you wouldn't need to specify both.

You also don't need to specify the imports in the profile.ps1 of the function.

If the modules are available on the PSModulePath then you can just use the functions in your run.ps1

  • Related