Instantly share code, notes, and snippets.

Embed
What would you like to do?
#
#.SYNOPSIS
# Creates Event Grid Subscription.
#.DESCRIPTION
# Creates an Event Grid Subscription (Topic type: Azure Subscription) to a Function App V2
#.OUTPUTS
# Progress messages
#
Param(
[string]$ResourceGroupName,
[string]$FunctionAppName,
[string]$FunctionName,
[string]$EventGridSubscriptionName
)
Function Get-AppServicePublishingCredentials($resourceGroupName, $functionAppName){
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$functionAppName/publishingcredentials"
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
return $publishingCredentials
}
#function to get bearer token from publishing profile
function Get-KuduAccessToken($resourceGroupName, $functionAppName){
$publishingCredentials = Get-AppServicePublishingCredentials $resourceGroupName $functionAppName
return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
}
#function to get the Master Key using End point and passing bearer tocken in Authorization Header
function Get-MasterAPIKey($accessToken, $functionAppName ){
$result = Invoke-RestMethod -Uri "https://$functionAppName.scm.azurewebsites.net/api/functions/admin/masterkey" -Headers @{"Authorization"=$accessToken;"If-Match"="*"}
return $result
}
function Get-EventGridExtensionSystemKey($accessToken, $functionAppName, $masterKey ){
$result = Invoke-RestMethod ("https://$functionAppName.azurewebsites.net/admin/host/systemkeys/eventgrid_extension?code=" + $masterKey.masterKey.ToString())
return $result.value
}
function Create-EventGridSubscription($functionAppName, $functionName, $systemKey, $eventGridSubscriptionName)
{
$functionEndpointURL = "https://" + $functionAppName + ".azurewebsites.net/runtime/webhooks/eventgrid?functionName=" + $functionName + "&code=" + $systemkey +""
$subscriptionID=(Get-AzureRmContext).Subscription.Id
New-AzureRmDeployment `
-Name egdeployment `
-Location westeurope `
-TemplateUri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-event-grid-resource-events-to-webhook/azuredeploy.json `
-eventSubName $eventGridSubscriptionName `
-endpoint $functionEndpointURL
}
# Get Kudu Access Token, so we can call Kudu API to get master key
$accessToken = Get-KuduAccessToken $ResourceGroupName $FunctionAppName
Write-Output "This is the accesstoken: $accessToken"
# Get master key to call Event Grid Extension API
$masterKey = Get-MasterAPIKey $accessToken $FunctionAppName
Write-Output "This is the masterKey: $masterkey"
# Get Event Grid SystemKey, so we can create the Event Grid Webhook
$systemKey = Get-EventGridExtensionSystemKey $accessToken $FunctionAppName $masterkey
Write-Output "This is the SystemKey: $systemKey"
Write-Output "Creating the Event Grid Subscription:"
Create-EventGridSubscription $FunctionAppName $FunctionName $systemKey $EventGridSubscriptionName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment