範例:使用 Azure 連結庫建立資源群組

此範例示範如何使用 Python 腳本中的 Azure SDK 管理連結庫來建立資源群組。 (The 本文稍後會提供對等的 Azure CLI 命令。如果您想要使用 Azure 入口網站,請參閱建立資源群組

本文中的所有命令在Linux/macOS bash和 Windows 命令殼層中都相同,除非另有說明。

1:設定本機開發環境

如果您尚未設定環境,您可以在其中執行此程序代碼。 以下列出一些選項:

2:安裝 Azure 連結庫套件

使用下列內容建立名為 requirements.txt 的檔案:

azure-mgmt-resource
azure-identity

在啟動虛擬環境的終端機或命令提示字元中,安裝需求:

pip install -r requirements.txt

3:撰寫程式代碼以建立資源群組

使用下列程式代碼建立名為 provision_rg.py 的 Python 檔案。 註解會說明詳細資料:

# Import the needed credential and management objects from the libraries.
import os

from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient

# Acquire a credential object using DevaultAzureCredential.
credential = DefaultAzureCredential()

# Retrieve subscription ID from environment variable.
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

# Obtain the management object for resources.
resource_client = ResourceManagementClient(credential, subscription_id)

# Provision the resource group.
rg_result = resource_client.resource_groups.create_or_update(
    "PythonAzureExample-rg", {"location": "centralus"}
)

# Within the ResourceManagementClient is an object named resource_groups,
# which is of class ResourceGroupsOperations, which contains methods like
# create_or_update.
#
# The second parameter to create_or_update here is technically a ResourceGroup
# object. You can create the object directly using ResourceGroup(location=
# LOCATION) or you can express the object as inline JSON as shown here. For
# details, see Inline JSON pattern for object arguments at
# https://learn.microsoft.com/azure/developer/python/sdk
# /azure-sdk-library-usage-patterns#inline-json-pattern-for-object-arguments

print(
    f"Provisioned resource group {rg_result.name} in the {rg_result.location} region"
)

# The return value is another ResourceGroup object with all the details of the
# new group. In this case the call is synchronous: the resource group has been
# provisioned by the time the call returns.

# To update the resource group, repeat the call with different properties, such
# as tags:
rg_result = resource_client.resource_groups.create_or_update(
    "PythonAzureExample-rg",
    {
        "location": "centralus",
        "tags": {"environment": "test", "department": "tech"},
    },
)

print(f"Updated resource group {rg_result.name} with tags")

# Optional lines to delete the resource group. begin_delete is asynchronous.
# poller = resource_client.resource_groups.begin_delete(rg_result.name)
# result = poller.result()

程式代碼中的驗證

本文稍後會使用 Azure CLI 登入 Azure,以執行範例程序代碼。 如果您的帳戶具有在 Azure 訂用帳戶中建立及列出資源群組的許可權,程式代碼將會順利執行。

若要在生產腳本中使用這類程序代碼,您可以將環境變數設定為使用服務主體型方法進行驗證。 若要深入瞭解,請參閱 如何使用 Azure 服務驗證 Python 應用程式。 您必須藉由在 Azure 中指派適當的 角色,確保服務主體有足夠的許可權在訂用帳戶中建立和列出資源群組;例如, 訂用帳戶上的參與者 角色。

4:執行腳本

  1. 如果您尚未登入 Azure,請使用 Azure CLI 登入 Azure:

    az login
    
  2. AZURE_SUBSCRIPTION_ID 環境變數設定為訂用帳戶標識碼。 (您可以執行 az account show 命令,並從輸出中的 屬性取得訂用帳戶識別碼 id

    set AZURE_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
    
  3. 執行指令碼:

    python provision_rg.py
    

5:驗證資源群組

您可以透過 Azure 入口網站 或 Azure CLI 來確認群組是否存在。

6:清除資源

如果您不需要保留在此範例中建立的資源群組,請執行 az group delete 命令。 資源群組不會在您的訂用帳戶中產生任何持續費用,但資源群組中的資源可能會繼續產生費用。 最好清除您未主動使用的任何群組。 自 --no-wait 變數可讓命令立即傳回,而不是等待作業完成。

az group delete -n PythonAzureExample-rg  --no-wait

您也可以使用 ResourceManagementClient.resource_groups.begin_delete 方法,從程式代碼中刪除資源群組。 本文腳本底部的批注程式代碼示範用法。

如需參考:對等的 Azure CLI 命令

下列 Azure CLI az group create 命令會建立具有標籤的資源群組,就像 Python 腳本一樣:

az group create -n PythonAzureExample-rg -l centralus --tags "department=tech" "environment=test"

另請參閱