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>"

Login-AzureRmAccount



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;
    'FUNCTIONS_EXTENSION_VERSION' = '~1';
    'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING' = $storageAccountConnectionString; 
    'WEBSITE_CONTENTSHARE' = $contentShare;   
    'APPINSIGHTS_INSTRUMENTATIONKEY' = $AppInsightsKey}


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

#Dev
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"

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

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

You May Also Like

About the Author: michaelstephensonuk