我正在尝试自动部署我们的环境 vir ARM 模板。我可以部署事件网格和函数应用,但现在我需要在部署函数应用后将函数应用订阅到事件网格。有没有办法获取函数应用程序的 webhook url
- 通过 ARM
- 发布管道中的其他一些组件(Powershell)
一旦我们有了 webhook url,我们就可以通过 ARM 创建订阅 - 但是要访问正确的 url 似乎是我们落伍的地方。
请提供任何帮助
我正在尝试自动部署我们的环境 vir ARM 模板。我可以部署事件网格和函数应用,但现在我需要在部署函数应用后将函数应用订阅到事件网格。有没有办法获取函数应用程序的 webhook url
一旦我们有了 webhook url,我们就可以通过 ARM 创建订阅 - 但是要访问正确的 url 似乎是我们落伍的地方。
请提供任何帮助
在上面@Van 和@Barrie 的答案的帮助下,我设法完成了这项工作。
此脚本从 azure api 返回 masterkey 和 defaultkey,这使您能够从发布管道中的 functionApp/webApp 创建 eventgrid 订阅。
Van 的脚本(7 月 30 日)适用于 FA 版本 1,但不适用于 FunctionApps V2(api 中有所更改)。在 V2 中使用此脚本时,错误是:
运行时密钥存储在 blob 存储中。此 API 不支持此配置。请将环境变量 AzureWebJobsSecretStorageType 值更改为“文件”。
我修改了这个脚本,现在它适用于 V2:
#DEBUG: when debugging (running in powershell on local pc) you need to comment out the next line by starting the line with #
param($resourceGroupName, $webAppname)
function Get-PublishingProfileCredentials($resourceGroupName, $webAppName){
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$webAppName/publishingcredentials"
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
return $publishingCredentials
}
function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName){
$publishingCredentials = Get-PublishingProfileCredentials $resourceGroupName $webAppName
return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
}
function Get-MasterAPIKey($kuduApiAuthorisationToken, $webAppName ){
$bearerToken = Invoke-RestMethod -Uri https://$webAppName.scm.azurewebsites.net/api/functions/admin/token -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"}
$masterkeyResponse = Invoke-RestMethod -Method GET -Headers @{Authorization=("Bearer {0}" -f $bearerToken)} -Uri "https://$webAppName.azurewebsites.net/admin/host/systemkeys/_master"
$masterKeyValue = $masterkeyResponse.value
return $masterKeyValue
}
function Get-HostAPIKeys($kuduApiAuthorisationToken, $webAppName, $masterKey ){
$apiUrl = "https://$webAppName.azurewebsites.net/admin/host/keys?code=$masterKey"
$result = Invoke-WebRequest $apiUrl
return $result
}
#DEBUG: when debugging this in powershell on my local pc I use this to authenticate (remove # to uncomment the next line):
#Login-AzureRmAccount -SubscriptionName "Insert_Subscription_Name_Here"
#DEBUG: when debugging you need to set these parameters:
# $resourceGroupName = "Insert_ResourceGroup_Name_Here"
# $webAppname = "Insert_FunctionApp_Name_Here"
#Auth Header
$kuduToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName
#MasterKey
$masterKey = Get-MasterAPIKey $kuduToken $webAppName
Write-Host "masterKey = " $masterKey
#Default Key
$result = Get-HostAPIKeys $kuduToken $webAppName $masterkey
$keysCode = $result.Content | ConvertFrom-Json
Write-Host "default Key = " $keysCode.Keys[0].Value
#Set Return Values:
$faMasterKey = $masterkey
$faDefaultKey = $keysCode.Keys[0].Value
Write-Output ("##vso[task.setvariable variable=fa_MasterKey;]$faMasterKey")
Write-Output ("##vso[task.setvariable variable=fa_DefaultKey;]$faDefaultKey")
这个剧本和范的剧本只有很小的区别。主要区别在于此脚本将在 Azure CLI Functions V2 上运行。更多信息:https ://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-grid
我终于设法让这个工作。最后,我创建了一个提取主密钥(和默认密钥)的 powershell 任务,现在我可以创建我的 eventgrid 订阅。
谢谢
这是我使用的 powershell 脚本:
param($resourceGroupName, $webAppname)
function Get-PublishingProfileCredentials($resourceGroupName, $webAppName){
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$webAppName/publishingcredentials"
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName
$resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action
list -ApiVersion 2015-08-01 -Force
return $publishingCredentials
}
function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName){
$publishingCredentials = Get-PublishingProfileCredentials $resourceGroupName $webAppName
return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
}
function Get-MasterAPIKey($kuduApiAuthorisationToken, $webAppName ){
$apiUrl = "https://$webAppName.scm.azurewebsites.net/api/functions/admin/masterkey"
$result = Invoke-RestMethod -Uri $apiUrl -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"}
return $result`
}
function Get-HostAPIKeys($kuduApiAuthorisationToken, $webAppName, $masterKey ){
$apiUrl = "https://$webAppName.azurewebsites.net/admin/host/keys?code=$masterKey"
$result = Invoke-WebRequest $apiUrl
return $result`
}
$accessToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppname
$adminCode = Get-MasterAPIKey $accessToken $webAppname
Write-Host "masterKey = " $adminCode.Masterkey
$result = Get-HostAPIKeys $accessToken $webAppname $adminCode.Masterkey
$keysCode = $result.Content | ConvertFrom-Json
Write-Host "default Key = " $keysCode.Keys[0].Value
$faMasterKey = $adminCode.Masterkey
$faDefaultKey = $keysCode.Keys[0].Value
Write-Output ("##vso[task.setvariable variable=fa_MasterKey;]$faMasterKey")
Write-Output ("##vso[task.setvariable variable=fa_DefaultKey;]$faDefaultKey")
这将输出:
(我将尝试创建一个 VSTS 任务并将其发布到市场 - 详细信息将随之而来)
您应该能够像这样输出 webhook URL:
"outputs": {
"Url": {
"type": "string",
"value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', parameters('yourFunctionAppName'), parameters('yourFunctionName')),'2015-08-01').trigger_url]"
}
}
这是一个相关的答案。
我和你在同一条船上,最终得到了这个工作,但花了很多时间来确定正确的端点等。我试图做的是使用az eventgrid event-subscription create
. 主要问题在于--endpoint
参数,因为它上面有一个code
查询字符串参数。通过执行以下操作,我可以很容易地在 Azure 门户中找到它:
但是,我想以编程方式完成这一切,但事实证明这很困难。最后,我使用的 bash 脚本如下所示:
#!/bin/bash
appName="myfunctionappname"
resourceGroup="myresourcegroupname"
# First do a KUDU login so we can get a JWT bearer token
user=$(az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup --query "[?publishMethod=='MSDeploy'].userName" -o tsv)
pass=$(az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup --query "[?publishMethod=='MSDeploy'].userPWD" -o tsv)
bearerToken=$(curl -s -u $user:$pass https://$appName.scm.azurewebsites.net/api/functions/admin/token | tr -d '"')
# Creating event grid subscription linked against the endpoint is an admin function so requires a master key
masterKeyResponse=$(curl -s -H "Authorization: Bearer $bearerToken" "https://$appName.azurewebsites.net/admin/host/systemkeys/_master")
masterKey=$(echo $masterKeyResponse | jq '.value' | tr -d '"')
functionName="MyFunctionName"
az eventgrid event-subscription create -g $resourceGroup --name "test-event-subscription" --endpoint "https://$appName.azurewebsites.net/runtime/webhooks/EventGridExtensionConfig?functionName=$functionName&code=$masterKey"
对于 V 2.0 和 3.0 Function Apps,您必须将 AzureWebJobsSecretStorageType 设置为文件:
"properties": {
"name": "[variables('functionsName')]",
"siteConfig": {
"appSettings": [
{
"name": "FUNCTIONS_EXTENSION_VERSION",
"value": "~3"
},
{
"name": "AzureWebJobsSecretStorageType",
"value": "files"
},
然后您可以使用以下方法获取 url 或密钥和 url:
"outputs": {
"mValidateConfigurationUrl": {
"type": "string",
"value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', variables('functionsName'), 'mValidateConfiguration'),'2015-08-01').trigger_url]"
},
"mValidateConfigurationUrlObj": {
"type": "object",
"value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', variables('functionsName'), 'mValidateConfiguration'),'2015-08-01')]"
}