Step 2 - Setting up Function Keys - Local Development
  • 07 Nov 2019
  • 2 Minutes to read
  • Contributors
  • Comment
  • Dark
    Light
  • PDF

Step 2 - Setting up Function Keys - Local Development

  • Comment
  • Dark
    Light
  • PDF

Article Summary

After Terraform has setup the majority of my infrastructure I next need to configure the function keys.

Question - Why didnt you setup the key in Terraform?
There is a great discussion topic about what you should and shouldnt do in Terraform. Out of the box Terraform is not able to setup function keys. There are workaround approaches you could use such as executing a Powershell script or REST operations from Terraform

In my case I wrote a Powershell script which utilises the Azure CLI to setup the keys. I felt it was not worth the additional complexity of running this from Terraform. I would just execute the script locally as a one off when I need to.

To execute the Powershell script I used the following command:

powershell -command .\FunctionKeys.ps1

The Powershell script to setup keys is below:


function SetupFunctionKey {
    param( [string] $subscriptionId, [string] $resourceGroup, [string] $webAppName, [string] $keyName, [string] $keyValue )
   
	Write-Host "Executing function to setup key"

	$resourceId = "/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$webAppName"
	Write-Host "Resource Id = " $resourceId
   
	#List Keys
	#=========
	Write-Host 'Checking updated keys'
	$output = az rest --method post --uri "$resourceId/host/default/listKeys?api-version=2018-11-01"

	Write-Host $output
	$LastExitCode
	if (!$?) {
		Write-Error "Error listing keys 1"
		return
	}	

	#Delete Existing key
	#===================
	Write-Host 'Deleting existing key'
	#$output = az rest --method delete --uri "$resourceId/host/default/functionkeys/$($keyName)?api-version=2018-11-01"

	Write-Host $output
	$LastExitCode
	if (!$?) {
		Write-Error "Error deleting key"
		return
	}

	#Add new Key
	#===========
	$payload = (@{ properties=@{ name=$keyName; value=$keyValue } } | ConvertTo-Json -Compress).Replace('"', '\"')

	Write-Host "Payload = " $payload
	Write-Host 'About to put new key in Azure'
	$output = az rest --method put --uri "$resourceId/host/default/functionkeys/$($keyName)?api-version=2018-11-01" --body "$payload"

	Write-Host $output
	$LastExitCode
	if (!$?) {
		Write-Error "Error adding key"
		return
	}

	#List Keys
	#=========
	Write-Host 'Checking updated keys'
	$output = az rest --method post --uri "$resourceId/host/default/listKeys?api-version=2018-11-01"

	Write-Host $output
	$LastExitCode
	if (!$?) {
		Write-Error "Error listing keys 2"
		return
	}
}

#Common Variables
#=================
$mySubscriptionId = "[TBC]"
$myResourceGroup = "[TBC]"
$myWebAppName = "[TBC]"

#Setup 1st Key
#==============
$functionKey = "[TBC]"
$keyName = "[TBC]"
SetupFunctionKey -keyName $keyName -keyValue $functionKey -subscriptionId $mySubscriptionId -resourceGroup $myResourceGroup -webAppName $myWebAppName

#Setup 2nd Key
#==============
$functionKey = "[TBC]"
$keyName = "[TBC]"
SetupFunctionKey -keyName $keyName -keyValue $functionKey -subscriptionId $mySubscriptionId -resourceGroup $myResourceGroup -webAppName $myWebAppName

At this point I would now have two function keys setup as host keys on my Function App. One for my APIM to use in its policy to act as a proxy to the functions and one for my Logic App to use to call the functions.


Was this article helpful?