Fun with Azure Functions and DriveAlreadyExists. I’ve recently been working with Azure Functions to automate tasks, serverless compute is great for this, just write the code and let Microsoft worry about the infrastructure that it’s running on! No need to worry about patching, scalability or high availably, that’s all taken care of for you.
In this particular instance I was using PowerShell as the language of choice and using it to connect to AzureAD. Azure Functions don’t have the AzureAD PowerShell module available natively. So you need to import them into your function.
While you can’t install modules in Azure Functions as you would normally in PowerShell, importing modules in a function is quite simple, follow the steps below:
- Create a Modules folder under D:\home\site\wwwroot\<YourFunctionName> – use Kudu for this
- Copy the modules in question into the new folder – again use Kudu to drag and drop the folder you want to copy up. It should look like this once copied:
- When the Azure Function next runs it will implicitly load the modules and they will be available to the runtime.
Note: Importing modules will increase the execution time of your Azure Function as they will need to be loaded each time the function runs.
While testing the function I discovered a small but annoying issue, the implicit import was causing some errors to be reported. While this didn’t actually affect the script, it did cause the function to report an error. I kept seeing an error reporting DriveAlreadyExists as shown below:
This wasn’t good, this would report as a failure of the function even though the function itself was executing as expected.
This sucked from a monitoring perspective, it would mean the likes of App Insights would report the function as failing even though it wasn’t and it would make picking up an actual failure impossible as there was too much noise.
I was fairly sure that if i could get the modules to import with the -noclobber switch then I’d get past the issue, but it took a fair bit of digging to find a fix as I couldn’t find any documentation that dealt with the issue.
The fix is to stop the implicit module import, and then manually import the modules with the -noclobber switch. To do this you need to do the following:
- Rename the Modules folder to something other than Modules
- Explicitly import the modules in your script
Renaming the folder is easy, again using Kudu, simply use the ren command at the prompt as shown below:
Then explicitly import the module in your script, using the full file path to where it is stored as shown below:
Save and Run your function and you should now see success!
Hopefully if you run into this issue this post will help you fix it with less experimentation than it took me!