Azure 資源群組是保留 Azure 解決方案相關資源的容器。 資源群組可能包含記憶體、虛擬機、應用程式、儀錶板、服務,或您在 Azure 中處理的任何專案。
Azure Command-Line 介面 (CLI) 可讓您建立、保存及設定預設的 Azure 資源群組。 Azure CLI 也可讓您在建立資源之後清除資源。
Azure 區域識別
Azure 客戶可以選擇在數個不同的區域中部署資源。 有時候,客戶可以藉由選取提供相同服務的鄰近區域來降低成本。 如果識別附近的區域,訊息會顯示未來部署中選取的區域。
在下列範例中 az config ,命令是用來關閉區域建議訊息:
az config set core.display_region_identified=no
如需 Azure 區域的更多資訊,請參閱 [為您選擇合適的 Azure 區域]。
建立資源群組
若要建立資源群組,請使用 az group create 命令:
az group create --name MyResourceGroup --location eastus
資源群組與單一位置相關聯。 若要查看目前訂用帳戶中支援的所有位置,請執行 az account list-locations 命令:
az account list-locations
若要查看目前訂用帳戶的所有資源群組,請使用 az group list 命令:
az group list --output table
小提示
--output 參數是全域參數,適用於所有命令。 值 table 會以友善格式呈現輸出。 如需詳細資訊,請參閱 Azure CLI 命令的輸出格式。
當您建立資源時,它會位於資源群組內。 下列範例示範使用 az storage account create 命令建立的記憶體帳戶:
az storage account create --resource-group MyResourceGroup --name storage134 --location eastus --sku Standard_LRS
若要移除資源群組,請執行 az group delete 命令:
az group delete --name MyResourceGroup
當您移除資源群組時,會刪除其內含的所有資源。 這個動作無法復原。 如果您嘗試本文中的任何命令,請刪除您建立的資源群組,以清除您的帳戶。
設定預設資源群組
您可以針對從本機 Azure CLI 或 Azure Cloud Shell 執行的所有命令設定預設資源群組。 Azure CLI 會將此設定儲存在本機的 設定 檔案中。 若要查看目前的組態,請執行 az config get 命令:
az config get
結果會顯示預設資源群組和其他預設值。 如果您第一次使用 Azure CLI,結果可能是空的。
若要設定 Azure CLI 安裝的預設資源群組,請執行 az config set 命令:
az config set defaults.group=MyResourceGroup
這個指令會設定指定索引鍵的值,在此案例中為 defaults.group。 如需可用的組態選項,請參閱 Azure CLI 組態。
備註
az config set 命令不會驗證您輸入的資源群組是否存在。 命令只會儲存機碼/值組。
執行命令之後,下列兩個命令會提供您相同的結果:
az storage account create --resource-group MyResourceGroup --name storage01 --location eastus --sku Standard_LRS
az storage account create --name storage01 --location eastus --sku Standard_LRS
資源群組與訂用帳戶相關聯。 如果您的組織有多個訂用帳戶,您必須先變更為所需的訂用帳戶,才能使用該訂用帳戶中的資源群組。 如果資源群組的預設值不屬於您目前的訂用帳戶,則會產生錯誤。 如需多個訂用帳戶的詳細資訊,請參閱 使用多個 Azure 訂用帳戶。
您不需要重設預設值,即可使用其他資源群組。 請改為指定資源群組:
az group create --name OtherResourceGroup --location eastus
az storage account create --resource-group StorageGroups --name storage03 --location westus --sku Standard_LRS
預設值僅適用於您。 這不會影響其他使用者或您透過 Azure 入口網站所做的變更。
如果您使用保存的參數值,如本文所述,這些值會優先於 組態 檔中設定的預設值。
設定資源群組鎖定
身為系統管理員,您可能需要鎖定資源群組,以防止使用者刪除或修改它。 如需詳細資訊,請參閱 鎖定資源以防止非預期的變更。
在 Azure CLI 中,使用 az group lock 命令。 例如, az account lock create 命令可以防止使用者刪除資源群組:
az group lock create --name "Cannot delete resource group" --lock-type CanNotDelete
備註
您必須擁有 contributor 資源群組的許可權,才能建立或變更鎖定。
若要查看資源群組上的目前鎖定,請使用 az group lock list 命令:
az group lock list --output table
清理資源
如果您嘗試本文中的任何命令,則可以移除您使用 az group delete 命令建立的資源:
az group delete --name MyResourceGroup
az group delete --name OtherResourceGroup
az group delete --name StorageGroups
此命令會移除群組及其所有相關聯的資源。