如何在同一 ARM 模板中授予 VM 对 Key Vault 的访问权限

How to grant VM access to Key Vault in the same ARM template

我正在尝试创建一个能够配置 VM AND Key Vault 的 ARM 模板。 VM 标识设置为类型 "SystemAssigned"。请参阅下面的代码片段:

        ...
        "name": "[variables('VM1')]",
        "type": "Microsoft.Compute/virtualMachines",
        "identity": { 
            "type": "SystemAssigned"
        },
        "apiVersion": "2019-07-01",
        ...

我希望能够授予 VM 访问 Vault 中机密的权限。为此需要为 accessPolicies 检索 tenantIDObjectID。对于我使用的租户 ID:

"tenantId": "[subscription().tenantId]",

是否有类似的方法来引用在同一模板中创建的 VM 的 ObjectID?

谢谢!

您需要使用引用来获取 VM 的托管服务身份的对象 ID(主体 ID)以分配对 KeyVault 的访问权限。 See the documentation here

我看到 KeyVault 需要租户 ID 和对象 ID。

模板中应该已有的租户 "tenantId": "[subscription().tenantId]"

并且 ObjectID 是 PrincipalId,如链接的文档和下面的示例所示。

{
    "apiVersion": "2017-09-01",
    "type": "Microsoft.Authorization/roleAssignments",
    "name": "[parameters('rbacGuid')]",
    "properties": {
        "roleDefinitionId": "[variables(parameters('builtInRoleType'))]",
        "principalId": "[reference(variables('vmResourceId'), '2017-12-01', 'Full').identity.principalId]",
        "scope": "[resourceGroup().id]"
    },
     "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/', parameters('vmName'))]"
    ]
}