I need the AWS module, which is available from the PS-Gallery but when I try to run the install step inside an Azure Function, it doesn't work. The -verbose argument flag isn't writing anything to the console.
What is the right way to get and use additional PowerShell modules within an Azure function?
Function code
[Console]::WriteLine("PowerShell Timer trigger function executed at:$(get-date)");
if (-not (Get-Module -Name "AWSPowerShell")) {
[Console]::WriteLine("AWSPowerShell not installed");
Install-Module -Name AWSPowerShell -Scope CurrentUser -ErrorAction Continue -Verbose
}
if (-not (Get-Module -Name "AWSPowerShell")){
[Console]::WriteLine("AWSPowerShell install step failed");
}
Function log
2016-06-09T11:24:31.108 Function started (Id=e09be687-2e13-4754-942e-eab75c8516e5)
2016-06-09T11:24:32.788 Powershell Timer trigger function executed at:06/09/2016 11:24:32
AWSPowerShell not installed
AWSPowerShell install step failed
2016-06-09T11:24:32.788 Function completed (Success, Id=e09be687-2e13-4754-942e-eab75c8516e5)
Update
Per @travis' answer I added the suggested code, including the nuget package manager line. This still didn't work. I added another debug line and found that there were no module providers, even after trying to add nuget!
Revised code
[Console]::WriteLine("Powershell Timer trigger function executed at:$(get-date)");
Get-PackageProvider -Name nuget -ForceBootstrap
$pkg=Get-PackageProvider -ListAvailable
if ($pkg.Count -lt 1){ [Console]::WriteLine("No providers")}
if (-not (Get-Module -listavailable -Name "AWSPowerShell")) {
[Console]::WriteLine("AWSPowerShell not installed");
Install-Module -Name AWSPowerShell -Scope CurrentUser -ErrorAction Continue -Verbose -force
}
if (-not (Get-Module -listavailable -Name "AWSPowerShell")){
[Console]::WriteLine("AWSPowerShell install step failed");
}
Import-Module AWSPowerShell
Revised log
2016-06-09T17:54:03.859 Powershell Timer trigger function executed at:06/09/2016 17:54:02
No providers
AWSPowerShell not installed
AWSPowerShell install step failed
2016-06-09T17:54:03.859 Function completed (Success, Id=80efb9fc-5e91-45f9-ab58-3b71fcd764af)
What about updating the azurerm powershell modules, there are a lot of modules to get to the latest version 4.1.0 which if we upload will be a issue to put in a flat directory.
C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager
contains 46 folders.
You need to make sure you look for all modules, not just the loaded modules by adding
-listavailable
to theget-module
call.You may need to bootstrap nuget for install-module to work in non-interactive environments. The command is:
Get-PackageProvider -Name nuget -ForceBootstrap
If the repository you are installing from is not trusted, you may need to force the
install-module
command.Example
FYI: [Console]::WriteLine is not considered a good practice in PowerShell for automated scripts. Try to stick to
Write-Verbose
you can force it like thisWrite-Verbose -message 'my message' -verbose
I suspect the reason is that you're not specifying a location for the modules to be loaded from. The current
$env:PSModulePath
is listed asI can't entirely figure out where it is referencing the first from, so I couldn't put them there. So I put this together instead
This loads the module (located at d:\home\modules) and works as expected
The reason that
Get-PackageProvider -Name nuget -ForceBootstrap
doesn't work, is that thePackageManagement
module isn't installed. However nuget is already installed. You would need to interact with kudu directly to install packages that way.I'm glad to see I'm not the only one being driven slightly nuts by functions though ;)
Azure Functions support for PowerShell scripting is currently in an experimental stage. The following scenarios are supported:
modules
that is located in the same directory where the PowerShell script resides. In the Kudu console for a sample Function, the directory structure would look like the following,We will not support customers installing their own modules using the
Install-Module
cmdlet, however, customers may upload their modules into themodules
folder.All modules in the
modules
folder will be loaded automatically, so customers will not have to explicitly use theImport-Module
cmdlet.We will support script, binary and manifest modules. These modules will reside in a flat structure within the
modules
folder. An example layout is as follows:In the context of what you are trying to achieve, here are some suggested steps to make sure that the
AWSPowerShell
module is loaded.Install
AWSPowerShell
locally on your development machine. You will need to upload all the contents under\AWSPowerShell\3.3.5.0
Using the Kudu interface, upload the installed dependencies of
AWSPowerShell
to amodules
folder residing in your Function's directory. To do so, open the the Portal UI for your Function App and click on the Function App Settings button.Next, click on the Go to Kudu button to launch the Kudu console. You should see a snapshot similar to the following,
In the cmd console prompt, navigate to your Function's folder, create a
modules
directory and upload all the contents from\AWSPowerShell\3.3.5.0
to themodules
directory.You should end up with a modules folder that has a list of files similar to the snapshot below:
Run your Function. For instance, given the following script for my Function,
if (-not (Get-Module -Name "AWSPowerShell")) { Write-Output "AWSPowerShell not installed"; } else { Write-Output "AWSPowerShell installed"; }
when executed, the log output is as follows,
Note: The Function takes a while to complete due to the fact that all the modules are loaded at runtime.
It is important to bear in mind that unlike most IaaS setup, an Azure Function is executing in a multi-tenant environment. As such, there remains the following known caveats:
Our infrastructure guards against any Function executing low-level APIs that we deem as security risks (e.g. interactive modes, host credentials access, registry edits, and etc.). If the PowerShell script or module you are using calls any of those blocked APIs, you will not be able to execute those workloads in Functions. Nonetheless, our goal is to support as many scenarios as possible, so we will prioritize unblocking scenarios based on the demand volume from our customers.
We currently have PowerShell version 4.0 and Azure PowerShell 1.4 installed in our infrastructure. We will upgrade these versions soon. As we add more support for PowerShell in Azure Functions, module suites may be upgraded or added over time. There is a remote possibility that these pre-installed modules may conflict with your existing modules.