Logo

dev-resources.site

for different kinds of informations.

Azure Function App (Flex Consumption) PowerShell Modules solution

Published at
12/7/2024
Categories
azure
powershell
azurefunctions
gitlab
Author
rokicool
Author
8 person written this
rokicool
open
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

powershell Article's
30 articles in total
Favicon
Windows ไธŠ VSCode ็š„ C/C++ ๅปถไผธๆจก็ต„่™•็†็ทจ็ขผ็š„ๅ•้กŒ
Favicon
PowerShell ็š„ๆ–‡ๅญ—็ทจ็ขผ
Favicon
็ฌ”่ฎฐ3
Favicon
TryHackMe | Windows PowerShell | RSCyberTech
Favicon
Configuring Hyper-V Global Default Directories
Favicon
How I Set Up an Awesome PowerShell Environment for Script Development
Favicon
Azure Function App (Flex Consumption) PowerShell Modules solution
Favicon
File Comparison Made Easy: Detecting New and Changed Files with PowerShell
Favicon
10 Best Practices of PowerShell Code Signing for Signing Your Script
Favicon
PowerShell Script Collection: Automation and Solutions for Everyday Tasks
Favicon
Video: List All Available Windows Features on Windows 11 using CMD & PowerShell
Favicon
Set up Azure Network Security Perimeter with PowerShell
Favicon
Automating Azure Project Setup with PowerShell and GitHub Actions
Favicon
Azure Function App (Flex Consumption) in private VNET via IaC
Favicon
Using PowerShell to Create Service Principals for RBAC
Favicon
Two ways to use Pester to Mock objects with strongly typed parameters
Favicon
PowerShell Automation: Execute Batch GitHub CLI Commands
Favicon
โ“ Do you allow wrong input to enter your function?
Favicon
Introducing PowerShell Utility Scripts
Favicon
Cisco DHCP Pool conversion to Windows Server DHCP
Favicon
PowerShell | Script output garbled Chinese characters
Favicon
Rename Multiple Files in Sequence with Just One Click Using PowerShell in Windows! ๐Ÿš€
Favicon
Redirect Out-File to TestDrive: in your PowerShell Pester test scripts with this one weird trick
Favicon
Using PowerShell for day to day stuff
Favicon
Automating SharePoint Embedded: Using PowerShell to Call Graph API Endpoints
Favicon
Install Ubuntu on WSL 2
Favicon
How to Install WSL from PowerShell on Windows 10 and 11
Favicon
How to Run PowerShell Script in Jenkins Pipeline
Favicon
Identify Which Files are Large.
Favicon
Quick guide to setting up an shortcut/alias in windows

Featured ones: