Setting up Function Apps via Powershell

This post is really a note to self and if its useful to anyone else then great!  If your wanting to script setting up many Azure Function Apps then you may find this useful.

I had a task to create a bunch of function apps in different resource groups and locations to support a project.  I have created the below powershell script to help me do this.  The script assumed your resource groups are already setup and then does the following:

  1. Connects to your subscription
  2. Gets the resource group
  3. Creates an AppInsight instance
  4. Creates a storage account
  5. Gets the key from the storage account
  6. Creates a function app
  7. Updates the function app so that it is linked to the app insights and storage account

At the bottom of the script you can see how i reused the powershell function to setup the Azure resources for each of our environments.


Set-ExecutionPolicy -ExecutionPolicy Unrestricted

$prodSubId = "<Add id>"
$testSubId = "<Add id>"


function AddFunctionApp([string] $subscriptionId, [string] $rgname, [string] $storageAccountName, [string] $functionAppName, [string] $location)
    Write-Host 'Subscription = ' $subscriptionId

    #Set the Azure Subscription you are working with
    Set-AzureRmContext -SubscriptionId $subscriptionId

    #Get the resource group
    $resourceGroup = Get-AzureRmResourceGroup | Where-Object { $_.ResourceGroupName -eq $rgname }
    #Create App Insights
    $appInsightsResource = New-AzureRmResource -ResourceName $functionAppName -ResourceGroupName $rgname -Tag @{ applicationType = "web"; applicationName = $functionAppName} `
        -ResourceType "Microsoft.Insights/components" -Location $location -PropertyObject @{"Application_Type"="web"} -Force

    #Create Azure Storage
    New-AzureRmStorageAccount -ResourceGroupName $rgname -Name $storageAccountName -Location $location -SkuName Standard_LRS

    #Create Function App
    New-AzureRmResource -ResourceType 'Microsoft.Web/Sites' -ResourceName $functionAppName -Kind 'functionapp' -Location $location -ResourceGroupName $rgname -Properties @{} -Force 

    #Extract Storage Key from Storage Account
    $keys = Get-AzureRmStorageAccountKey -ResourceGroupName $rgname -Name $storageAccountName
    $accountKey = $keys | Where-Object {$_.KeyName -eq "Key1" } | Select Value

    #Update Function App with settings from Storage and App Insights
    $AppInsightsKey = $appInsightsResource.Properties.InstrumentationKey
    $storageAccountConnectionString = 'DefaultEndpointsProtocol=https;AccountName=' + $storageAccountName + ';AccountKey=' + $accountKey.Value
    $contentShare = $functionAppName.ToLower()

    $AppSettings = @{}
    $AppSettings = @{'AzureWebJobsDashboard' = $storageAccountConnectionString;
    'AzureWebJobsStorage' = $storageAccountConnectionString;
    'WEBSITE_CONTENTSHARE' = $contentShare;   

    Set-AzureRmWebApp -Name $functionAppName -ResourceGroupName $rgname -AppSettings $AppSettings

AddFunctionApp -subscriptionId $testSubId -rgName "MyFunction-Dev" -storageAccountName "myfunctionstorepdev" -functionAppName "MyFunction-Dev" -location "North Europe"

#System Test
AddFunctionApp -subscriptionId $testSubId -rgName "MyFunction-SysTest" -storageAccountName "myfunctionstoresys" -functionAppName "MyFunction-SysTest" -location "North Europe"

AddFunctionApp -subscriptionId $testSubId -rgName "MyFunction-UAT" -storageAccountName "myfunctionstorepuat" -functionAppName "MyFunction-UAT" -location "North Europe"

AddFunctionApp -subscriptionId $testSubId -rgName "MyFunction-Prod" -storageAccountName "myfunctionstoreprod" -functionAppName "MyFunction-Prod" -location "North Europe"



Thanks for checking out my article.  If you liked it or found it useful that's really great.  Please feel free to say hi if you bump into me at any networking events and any likes, shares or feedback on the article are really appreciated.  If you really loved it you are also welcome to buy me a coffee using the button below.

All the best



Buy Me A Coffee

You May Also Like

About the Author: michaelstephensonuk