Powershell Azure Functions: Severless for Ops People

Perhaps you’ve heard of Azure Functions, or maybe more generally serverless computing or serverless apps. If you haven’t heard of it, here is a blurb from MS:

Serverless computing is the abstraction of servers, infrastructure, and operating systems. When you build serverless apps you don’t need to provision and manage any servers, so you can take your mind off infrastructure concerns. Serverless computing is driven by the reaction to events and triggers happening in near-real-time—in the cloud. As a fully managed service, server management and capacity planning are invisible to the developer and billing is based just on resources consumed or the actual time your code is running.

Specifically this post will be talking about Azure Functions. Azure Functions support a variety languages like C#, Python and JavaScript. Wait, if those sound like developer languages to you, you would be right. However, they also support, experimentally, Powershell.

Disclaimer: Azure Functions are relatively new and Powershell support is considered experimental. Though the team has said they are working to bring it more full fledged as a supported language. Consider this my small effort to increase Powershell support. And finally like anything else in the cloud this information could to be outdated by tomorrow.

Create the Azure Resource

There are a bunch of different types of Functions, you can read more about them here https://docs.microsoft.com/en-us/azure/azure-functions/. This post will cover creating a simple HTTP Trigger that uses Powershell.

To create our Azure Function we’ll need to go to Create a Resource and select Azure Function. One thing to note here, is that the name you give your function app needs to be unique. As you can see, the Azure Function name will be added to .azurewebsites.net so that it can be publicly accessible.

azure function powershell

Put it in a resource group, existing or new. And then select an OS. For me, since we’re talking about serverless here, I dont really care what it runs out. However, they do give you the option of Windows, Linux or Docker, I stuck with Windows. (I have not tried Linux or Docker, I don’t know if Powershell works on them or not)

azure function

The Consumption Plan is what I selected, this gives you a ridiculous amount of executions and GB/s before it starts costing money. I also used my existing storage account. Click on create and azure resource management will take a few minutes to setup your Azure Function App.

Once its created, open up the Azure Functions section, select your Function App and you’ll see the overview section. In particular there are some Function App Settings and Application Settings that each have a variety of settings to modify.

azure function powershell

I’ll make a few changes to these later.

Creating Functions

, expand your app and select functions, then new function.

azure function

 

Now, enable experimental language support.

create azure function experimental

And boom. We have all kinds of languages like Batch, Python, TypeScript, but for this post I want Powershell

azure function powershell

If, for some reason, the Experimental Language Support switch is not there, go into Function App settings and select the ~1 Runtime version. It doesn’t show up under the beta.

azure function app settings

Next, select Powershell HTTP Trigger.

azure function powershell

You have 3 options for authorization level, Anonymous(obvious), Function, which uses a function level API key and Admin, which is the master API key. This is important to note as you can have multiple functions under one Function App. Meaning you could have some functions that use their own API key and others that use the Admin key. Click create.

Selecting the newly created function, it loads a predefined Powershell script for you. Note: presently Azure Functions are running Powershell version 4. Also you can add modules you may need as well. Also note the default timeout for a function is 5 minutes, this can be changed to 10 minutes by modifying the host file. So if a script takes longer than 5 minutes to run there is no guarantee it will finish.

azure function

azure function powershell

Under the expanded function you have Integrate, Manage, and Monitor.

azure function

Select integrate, this shows us the predefined variables $req and $res for trigger and output. You can also set the HTTP method, currently its set to accept all methods.

azure function powershell

 

Create Powershell API

Now lets create our code. Something to note,  to store variables within the app, go to application settings and then scroll down to find application settings. Here I have added a variable named TestVar with a value of “this is a test variable.” To access this variable we simply use $env:TestVar. Any variable created this way is accessible across all functions inside the Function App.

azure function powershell

The Function code

# POST method: $req
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$uri = $requestBody.uri
$site = $requestBody.site

$test = $env:TestVar

$json = @"
{
"result1": "$site",
"result2": "$uri",
"result3": "$test"
}
"@


Out-File -Encoding Ascii -FilePath $res -inputObject $json

Our code takes our payload and puts it in the $req variable. Then converts it to a normal powershell object, and puts it in $requestBody.
Because I know the JSON payload I’m sending I know that the body contains two keys URI and Site.
It then grabs our TestVar variable and builds a new JSON object with all 3, Site, URI, and Test.

The last line builds the response with our JSON input.

Post to the API

To post to the API, I built a small JSON payload with the below.


$azurefunction = "your function uri here"

#build JSON payload

$body = @"
{
"site": "my website",
"uri": "www.systemcenterautomation.com"
}
"@


Invoke-RestMethod -Method Post -Uri $azurefunction -Body $new

azure function powershell

Not much of an API, but this was just a demo.

 

Conclusion

Given that you can do a world of stuff in Powershell, there’s no reason why Ops people can’t use Azure Functions too. Off the top of my head you could use them to

  • Kick off Azure Automation Runbooks
  • Post data to Log Analytics
  • Monitor websites
  • Start or interact with other Azure Functions or even Azure Resources

 

If you liked this post, my next post shows how to use a Timer Trigger Azure Function to post logs into Azure Log Analytics. https://www.systemcenterautomation.com/2018/07/azure-functions-log-analytics/

Hi, I’m Billy York.  I’m a Cloud and Datacenter Engineer, specializing in monitoring and automation. Here you’ll find posts about System Center Operations Manager, Powershell, Hyper-V, Azure Automation, Azure Log Analytics and other Microsoft related technologies.

2 thoughts on “Powershell Azure Functions: Severless for Ops People”

Comments are closed.