快速入門:使用 ARM 範本建立 Azure 金鑰保險庫

Azure Key Vault 是一項雲端服務,提供一個安全的秘密儲存,例如金鑰、密碼和憑證。 本快速入門重點介紹部署 Azure Resource Manager 範本(ARM 範本)以建立金鑰保險庫的過程。

Azure Resource Manager 模板 是一個 JavaScript 物件符號(JSON)檔案,用來定義你專案的基礎架構與設定。 範本使用宣告式語法。 您可以描述預期的部署,而不需要撰寫程式設計命令順序來建立部署。

如果你的環境符合前提條件,且熟悉使用 ARM 範本,請選擇 部署到 Azure 按鈕。 範本會在 Azure 入口網站中開啟。

將 Resource Manager 範本部署至 Azure 的按鈕。

先決條件

以下是完成本文的步驟:

  • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

檢閱範本

本快速入門中使用的範本是來自 Azure 快速入門範本

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.42.1.51946",
      "templateHash": "5424899472990749957"
    }
  },
  "parameters": {
    "keyVaultName": {
      "type": "string",
      "metadata": {
        "description": "Specifies the name of the key vault."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Specifies the Azure location where the key vault should be created."
      }
    },
    "enabledForDeployment": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specifies whether Azure Virtual Machines are permitted to retrieve certificates stored as secrets from the key vault."
      }
    },
    "enabledForDiskEncryption": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specifies whether Azure Disk Encryption is permitted to retrieve secrets from the vault and unwrap keys."
      }
    },
    "enabledForTemplateDeployment": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specifies whether Azure Resource Manager is permitted to retrieve secrets from the key vault."
      }
    },
    "tenantId": {
      "type": "string",
      "defaultValue": "[subscription().tenantId]",
      "metadata": {
        "description": "Specifies the Azure Active Directory tenant ID that should be used for authenticating requests to the key vault. Get it by using Get-AzSubscription cmdlet."
      }
    },
    "skuName": {
      "type": "string",
      "defaultValue": "standard",
      "allowedValues": [
        "standard",
        "premium"
      ],
      "metadata": {
        "description": "Specifies whether the key vault is a standard vault or a premium vault."
      }
    },
    "secretName": {
      "type": "string",
      "metadata": {
        "description": "Specifies the name of the secret that you want to create."
      }
    },
    "secretValue": {
      "type": "securestring",
      "metadata": {
        "description": "Specifies the value of the secret that you want to create."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.KeyVault/vaults",
      "apiVersion": "2023-07-01",
      "name": "[parameters('keyVaultName')]",
      "location": "[parameters('location')]",
      "properties": {
        "enabledForDeployment": "[parameters('enabledForDeployment')]",
        "enabledForDiskEncryption": "[parameters('enabledForDiskEncryption')]",
        "enabledForTemplateDeployment": "[parameters('enabledForTemplateDeployment')]",
        "enableRbacAuthorization": true,
        "tenantId": "[parameters('tenantId')]",
        "enableSoftDelete": true,
        "softDeleteRetentionInDays": 90,
        "enablePurgeProtection": true,
        "sku": {
          "name": "[parameters('skuName')]",
          "family": "A"
        },
        "networkAcls": {
          "defaultAction": "Allow",
          "bypass": "AzureServices"
        }
      }
    },
    {
      "type": "Microsoft.KeyVault/vaults/secrets",
      "apiVersion": "2023-07-01",
      "name": "[format('{0}/{1}', parameters('keyVaultName'), parameters('secretName'))]",
      "properties": {
        "value": "[parameters('secretValue')]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))]"
      ]
    }
  ],
  "outputs": {
    "location": {
      "type": "string",
      "value": "[parameters('location')]"
    },
    "name": {
      "type": "string",
      "value": "[parameters('keyVaultName')]"
    },
    "resourceGroupName": {
      "type": "string",
      "value": "[resourceGroup().name]"
    },
    "resourceId": {
      "type": "string",
      "value": "[resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))]"
    }
  }
}

此範本中已定義一項 Azure 資源:

  • Microsoft.KeyVault/vaults:建立 Azure 金鑰保存庫。 該範本啟用Azure RBAC 授權(enableRbacAuthorization: true)、軟刪除及清除保護。

如需更多 Azure 金鑰保存庫範本範例,請參閱 Azure 快速入門範本

部署範本

  1. 選擇以下圖片登入 Azure 並開啟範本。 範本會建立一個金鑰保險庫。

    將 Resource Manager 範本部署至 Azure 的按鈕。

  2. 選取或輸入下列值。 除非特別指定,否則就用預設值。

    • 訂用帳戶:選取 Azure 訂用帳戶。
    • 資源群組:選取 [新建],輸入資源群組的唯一名稱,然後選取 [確認]
    • 區域:選擇地點。 例如,美國中部
    • 保險庫名稱:輸入金鑰保險庫名稱,且必須在命名空間內 vault.azure.net 全域唯一。
    • SKU 名稱:選擇 標準高級。 預設是 標準
  3. 選取 [檢閱 + 建立],然後選取 [建立]。 當金鑰保險庫成功部署後,你會收到通知。

你也可以使用 Azure PowerShell、Azure CLI 或 REST API 來部署範本。 若要了解其他部署方法,請參閱部署範本

指派 金鑰保存庫 RBAC 角色

此範本所建立的金鑰庫使用Azure RBAC授權。 要在資料平面中建立或讀取金鑰、秘密或憑證,你需要為自己分配適當的角色。 例如,管理祕密時,請為自己指定金鑰保存庫祕密官角色:

echo "Enter your key vault name:" &&
read keyVaultName &&
az role assignment create --role "Key Vault Secrets Officer" \
    --assignee-object-id $(az ad signed-in-user show --query id -o tsv) \
    --scope $(az keyvault show --name $keyVaultName --query id -o tsv)

Note

關於其他內建角色,請參見Azure 金鑰保存庫資料平面操作的內建角色。 角色指派可能需要一兩分鐘才會生效。

檢閱已部署的資源

你可以使用 Azure 入口網站檢查金鑰庫,或使用以下 Azure CLI 或 Azure PowerShell 腳本:

echo "Enter your key vault name:" &&
read keyVaultName &&
az keyvault show --name $keyVaultName

清理資源

其他 金鑰保存庫 快速入門和教學課程會以本快速入門為基礎。 如果您打算繼續進行後續的快速入門和教學課程,您可以讓這些資源留在原處。 不再需要時,刪除資源群組,該群組會刪除金鑰庫及相關資源。

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
az group delete --name $resourceGroupName

Note

刪除資源群組也會刪除金鑰庫,但金庫會進入軟刪除狀態,並在保留期間(預設為 90 天)內仍可恢復。 在此期間,該保存庫名稱仍會在全域保留,而且因為已啟用清除保護,所以無法提前清除該保存庫。 對於標準金鑰保存庫,虛刪除的保存庫不會產生費用。 更多資訊請參見 金鑰保存庫 軟刪除概覽

更多金鑰保存庫 Azure Resource Manager 範本

其他快速入門會逐步說明如何將祕密、金鑰或憑證新增至金鑰保存庫:

秘密 Keys 憑證

您可以在此找到更多金鑰保存庫的範本:金鑰保存庫資源管理員參考

下一步