DEV Community

Cover image for Azure Function App (Flex Consumption) PowerShell Modules solution
Roman Kiprin
Roman Kiprin

Posted on

Azure Function App (Flex Consumption) PowerShell Modules solution

Prewords

In this article, I explain a technique that allows embedding PowerShell modules in Azure Function based on the Flex Consumption plan.

The fully working example is available in my GitLab project azure-function-app which is devoted to the Flex Consumption plan Azure Functions.

Back to the article

I have to admit that PowerShell is not suitable for high-load applications.

This scripting language is excellent for automating tasks. It offers numerous options for data management, API access, output, logging, error handling, and so much more.

Evidently, the bare language does not support millions of possible operations. You need to use PowerShell modules.

When you organize your PowerShell scripts within Azure Function Apps, things with PowerShell modules become complicated.

Basically, you can use

Install-Module Important-Module
Import-Module Important-Module
Enter fullscreen mode Exit fullscreen mode

and it will work... but. You probably don't want to do it. Every Azure Function execution will trigger that operation. This implies that each execution will involve the download and unpacking of the modules.

PowerShell Azure Function Apps and modules

And that is why the Azure Function PowerShell template has file requirements.psd1 which looks like this:

# 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' = '13.*'
}
Enter fullscreen mode Exit fullscreen mode

Which is super useful—you only need to request the modules. The hidden engine will install and cache them.

However, that is not exactly correct when you use the Flex Consumption Plan.

What is with Flex Consumption?

Flex Consumption Plan uses a different approach. Your function does not reserve any computing power; you only pay for executions and resources. This is an excellent feature! You don't pay for reservations.

However, at the same time, your function loses the ability to cache modules.

Should we return to dynamic installation of them? No! Of course not!

And what is the issue with PowerShell modules?

Microsoft suggests that your PowerShell Azure Function should include the necessary modules in the Azure Function distribution package.

That means both your function and the modules will be installed within the same filesystem in the backend Storage account.

It is like'manual caching'. Everything is reachable by the Azure Function App.

There is another problem, though. You have to include the modules in your function repository. And that is a manual process.

Possible solution is...

That is why I wrote all of it.

Why don't we use the pipeline that builds the Azure Function distribution package to automatically include all the necessary PowerShell modules?

Here in my repository: rokicool/azure-function-app@gitlab, you can find an example.

There is a requiredModules.ps1 file that defines the requirements:


# requiredModules.ps1
# List the modules that will be embedded into Azure Function App deployment
$requiredModules = @{
   "Az.Accounts" = "3.0.5"
}

Enter fullscreen mode Exit fullscreen mode

And there is a installModules.ps1 PowerShell script that is expected to be used in the GitLab pipeline:

build:
  stage: build
  image: 
    name: mcr.microsoft.com/azure-powershell:mariner-2
  script:
    - tdnf install zip -y
    - pwsh ./azure-function/Modules/installModules.ps1 ## <-- HERE
    # Build the zip file with the code of the Azure Function
    - mkdir dist
    - cd ./azure-function
    - zip -r ../dist/azure-function.zip ./*
  artifacts:
    paths:
      - dist/azure-function.zip
    name: ${ARM_AZFUNC_NAME}_artifact
Enter fullscreen mode Exit fullscreen mode

Therefore, the distribution package includes all the requested modules. You just need to provide names and the versions.

Let's step back and think!

Instead of requesting and installing the PowerShell modules dynamically, this technique puts the modules in the package alone with your Azure Function scripts!

It is even better from the reliability perspective. Your modules will be with your Azure function as long as your function exists.

Win-Win.


BTW. Since you spent your time on this article, you might be interested in the previous one that explains about Azure Function App (Flex Consumption) in private VNET via IaC

Top comments (0)