List Azure Functions based on Configuration Values

A company’s Azure environment can easily grow to a level that managing all the objects may become harder. The solution of this problem requires methods to query resources, such as Azure Functions, based on their properties. One of the fetures used for this is the Azure Resource Graph .

However, Function Apps configuration is beyond what Azure Resource Graph can access. In order to query Function Apps based on their configuration, we need to use Powershell.

Retrieving all Function Apps using Runtime older than 4

In my example, I was looking for a way to query function apps with runtime older than 4. But the possibilities to quer function apps based on configurations are very broad.

For example, you may be deploying function apps with the same or similar configuration and you could list all function apps with a specific configuration value. All functions using one database, or linked to one key vault, or other possible differences.

My example of a Powershell statement to list function apps based on the runtime version is the following:

runtime=@{l="FunctionRuntime";
	e={(Get-AzFunctionAppSetting -Name $_.Name 
		-ResourceGroupName $_.ResourceGroupName)["FUNCTIONS_EXTENSION_VERSION"]}}

(Get-AzFunctionApp | 
	Where-Object { $(if ((Get-AzFunctionAppSetting -Name $_.Name 
		-ResourceGroupName $_.ResourceGroupName) -eq $null) {""} 
			else {(Get-AzFunctionAppSetting -Name $_.Name 
				-ResourceGroupName $_.ResourceGroupName)["FUNCTIONS_EXTENSION_VERSION"]}) 
			-ne "~4" } ) 
| Select-Object Name,ResourceGroupname,$runtime 
|Format-Table -AutoSize

As you may notice, this is a combination of many different statements to generate the desired result. Powershell allows multiple possible combinations of the statements. If you have suggestions to improve this one, write a comment on this blog!

This is an example of the result of this execution:

Let’s analyse the powershell statements and how the result is achieved.

Powershell statements

Get-AzFunctionAppSetting: This cmdlet retrieves the app settings of one azure function. It works only individually for one function. This requires us to retrieve a list of Azure functions and process one by one using this cmdlet. Reference: Get-AzFunctionAppSettings

Get-AzFunctionApp: Retrieves a list of Function Apps. Without parameters in the way we are using, it will retrieve all function apps in the subscription. Reference: Get-AzFunctionApp

Where-Object: This statement is used to filter a list of objects. Reference: Where-Object

Select-Object: This statement is used to make a query over a list of objects, selecting the columns to display. Reference: Select-Object

Format-Table: One of the format statements available, this one formats the result in a table format. The important part is the -AutoSize parameter, used to adjust the width of the columns. Reference: Format-Table

Powershell Syntax features

runtime variable: This variable contains an object to be used as a custom column for the Select-Object statement. The object has two properties, one for the title of the column and another for the expression which will build the custom column.

$_ : This symbol in the expressions represent an object from a collection on the previous expression. This is used in combination with the pipes in many points of the statement.

[] (indexing): The result of the Get-AzFunctionAppSetting is an array. We using the indexing to retrieve one specific configuration from the array. The get_item method could have the same effect as the indexing syntax.

“|” (pipe) : The pipe symbol in Powershell means the result of one statement will be used as the input for the next one. First we retrieve the list of the functions; We use Where-Object to filter the list of functions; We use Select-Object to choose the properties to list; Finally, we format the list. Always using pipe to pass the result of one statement as input to the next one.

$(if) : If the Get-AzFunctionAppSetting cmdlet results in null, indexing would generate an error. This could happen if the list of functions includes one for which you don’t have permissions, for example. The $(if) allows to use an IF statement as a value in the middle of a bigger expression. In this way, if Get-AzFunctionAppSetting returns null we avoid indexing, otherwise we index the result to get the function runtime version.

Executing

The easiest way to execute powershell in Azure is using the Azure Cloud Shell. However, some companies don’t allow you to use the cloud shell. In this case, you will need to make the execution from your location machine, using Azure ISE.

In order to use Powershell from your local machine with Azure, you need to execute the cmdlet Connect-AzAccount. You may need to install the Azure AZ Powershell module before making the connection.

Conclusion

The possible uses of the tricks explained here goes way beyond only listing functions with older runtime. You can list functions based any configuration value, and you can expand this to other objects, querying objects based on values not acessible in the Azure Resource Graph.