我有一个资源组 (A),其中包含现有的 Key Vault,其中包含多个应用程序(在各个资源组中)使用的机密。
我有一个 ARM 模板,可以在另一个资源组 (B) 中创建函数应用程序。在此功能应用程序的部署过程中(通过 ARM 模板部署,对于 B),我想通过向资源组 A 中的 Key Vault 添加新的访问策略来授予对 A 中 Key Vault 的访问权限。
我在完成此任务时遇到了很大的问题。
这是 Function App 的精简模板定义:
"resources": [
{
"type": "Microsoft.Web/sites",
"apiVersion": "2021-03-01",
"name": "[parameters('functionAppName')]",
"kind": "functionapp",
"location": "[parameters('location')]",
"identity": {
"type": "SystemAssigned"
},
... (unrelated)
},
{
"type": "Microsoft.KeyVault/vaults/accessPolicies",
"apiVersion": "2019-09-01",
"name": "[concat(parameters('keyVaultName'), '/add')]",
"location": "[resourceGroup().location]",
"properties": {
"accessPolicies": [
{
"tenantId": "[tenant().tenantId]",
"objectId": "[reference(resourceId('Microsoft.Web/sites', parameters('functionAppName')), '2019-08-01').identity.principalId]",
"permissions": {
"secrets": [
"get",
"list"
]
}
}
]
},
"dependsOn": [
"[resourceId('Microsoft.Web/sites', parameters('functionAppName'))]"
]
}
使用中的参数(
keyVaultName
、functionAppName
)均有效。
问题似乎出在部署本身的范围内,这是
Resource Group
。
我尝试在
scope
资源本身上设置 Microsoft.KeyVault/vaults/accessPolicies
,但这不起作用 - 范围之间似乎存在某种不匹配。
我的问题如下:
Resource Group
更改为 Subscription
(或任何名称))?您可以创建针对 Key Vault 的资源组的嵌套部署。嵌套部署方法允许您将访问策略包含在作用域为
Function App's
资源组的模板中,确保 Key Vault access policy
应用于 Function App 的 身份,即使它驻留在不同的资源组中。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"functionAppName": {
"type": "string",
"defaultValue": "venkatfunctionapptestg",
"metadata": {
"description": "The name of the Function App to be created."
}
},
"location": {
"type": "string",
"defaultValue": "East US",
"metadata": {
"description": "The Azure region where the Function App will be deployed."
}
},
"keyVaultName": {
"type": "string",
"defaultValue": "keyvauldemo",
"metadata": {
"description": "The name of the existing Key Vault."
}
},
"keyVaultResourceGroupName": {
"type": "string",
"defaultValue": "Storage-RG",
"metadata": {
"description": "The name of the resource group where the Key Vault is located."
}
}
},
"resources": [
{
"type": "Microsoft.Web/sites",
"apiVersion": "2021-03-01",
"name": "[parameters('functionAppName')]",
"location": "[parameters('location')]",
"identity": {
"type": "SystemAssigned"
},
"properties": {}
},
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2021-04-01",
"name": "GrantKeyVaultAccessPolicy",
"resourceGroup": "[parameters('keyVaultResourceGroupName')]",
"dependsOn": [
"[resourceId('Microsoft.Web/sites', parameters('functionAppName'))]"
],
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.KeyVault/vaults/accessPolicies",
"apiVersion": "2019-09-01",
"name": "[concat(parameters('keyVaultName'), '/add')]",
"properties": {
"accessPolicies": [
{
"tenantId": "[tenant().tenantId]",
"objectId": "[reference(resourceId('Microsoft.Web/sites', parameters('functionAppName')), '2021-03-01', 'Full').identity.principalId]",
"permissions": {
"secrets": [
"get",
"list"
]
}
}
]
}
}
]
}
}
}
]
}
az deployment group create --resource-group "Venkat-RG" --template-file "Keyvault.json"
输出:
运行代码后,具有身份的Function App已经创建完成。
Function App's
身份已添加到 Key Vault
访问策略中,并具有 get 和 list 权限。