PowerShell in Azure Functions v2 Generally Available, and now?
Tags: azure, powershell, serverless, operation
Published: 2019-11-12 07:50:34

Since last week, the support of PowerShell in Azure Function is Generally available.  What can we expect? 

First, we can use PowerShell in production but what can be done with it. 
It’s was I think Gaël Colas who was the first to forge the idea of Event-Based Serverless Automation. Regarding Azure or Windows and Linux, PowerShell is a powerful automation tool. It is heavily used to automate tasks in the Microsoft ecosystem, in configuration management, operation daily works, CI/CD Pipeline and to automate tasks in Azure. 

But with Azure Functions, there is something different. Before Azure Functions automation was often a human process. Someone has to trigger something to start a workflow. It can be a pipeline in some CI/CD tools or a DSC script to provision a server. Each time someone has to push a button at some moment. 

With Azure function, it’s not the same, you can still use the push button mode but the real use case with PowerShell Azure Functions is to react to any event without human interaction. You can capture the usage of the CPU of one VM, use the deletion of a resource, check Azure AD groups, or the presence of some data in a blob as a trigger to start one Azure Function. In fact, there so many possibilities, you can use with Azure Function that you cannot enumerate all. 

How can you do that? To trigger a function, we can use the standard Azure Function trigger bindings, HTTP, Timer, Storage Blob or Queue, it’s simple to use.  You can also use Azure Event Grid and Azure Event Hub. This means that with Azure Function we can react to almost any event in an Azure subscription. 
This means we do not have to wait for someone to acknowledge a problem, Azure Function can handle it. It’s a game-changer where Operation is no more an engineering problem, it becomes a software engineering problem. 

PowerShell is mostly used to automate operation related workflows. Manage VM, Active Directory, Authentication referential, Configuration, resources orchestration in Azure or elsewhere. In Azure Function you can use is two tools already used on-premises we can use to perform that: Module and Modules. 

Azure Functions allow you to import any private module inside an Azure Functions App. You just need to add your modules inside the “Modules” folder of the Functions App and modules will be loaded in all functions of the Functions App. You can reuse your development. More using your module enable the use of libraries shared across different tools and the use of a sort of MVC model where the module is the Model handling data and logic, the function is the Controller accepting input; the events data and type and convert them to the controller and finally the output produced by the function is the view. 

For Modules, I mean the fact that PowerShell Azure Functions can manage module dependencies automatically. In the host.json Managed dependencies are enabled by default

```json
{
  "version": "2.0",
  "managedDependency": {
    "Enabled": true
  }

```
Module from PowerShell Gallery can be added by editing the requirements.psd1 file in the Function App root. 

```json
@{
    Az = '2.*'
    dbatools = "1.*"
}
```
Simply add the module name and the version (with or without a wildcard) in this file and the Azure Functions App will manage to download and install the module at the next reboot.

With internal modules and public modules from the PowerShell Gallery, you have all the tools to create any automation scenario you will need. The only limit will be our imagination.

About the price, Azure Functions can be really cheap. There are two components of the price, memory consumption, and resource consumption. 

Memory consumption is measured by GB used per second. If the script in the function uses 730 MB for 5 seconds and it’s executed 1500 per mouth you got.

1500 * 5 = 7500 seconds
(730/1024) * 7500 = 5346,68 GB/s

Resource consumption is measured by the number of the function execution, here 1500 
With a price of €0.000014/GB-s and €0,169 per Millions of executions, it means almost nothing.

We have a free tier, 400,000 GB-s (About 6666 minutes for a 1Gb scripts) and 1 million executions per subscription. In the example I use, the price will be near 0 (but you have to count the storage account hosting the function APP)

If you take care of script and memory consumption, Automation through Azure Functions with PowerShell can be almost free of charge.