Example: Use the Azure libraries to provision a resource group

This example demonstrates how to use the Azure SDK management libraries in a Python script to provision a resource group. (The Equivalent Azure CLI command is given later in this article. If you prefer to use the Azure portal, see Create resource groups.)

All the commands in this article work the same in Linux/macOS bash and Windows command shells unless noted.

1: Set up your local development environment

If you haven't already, follow all the instructions on Configure your local Python dev environment for Azure.

Be sure to create and activate a virtual environment for this project.

2: Install the Azure library packages

Create a file named requirements.txt with the following contents:

azure-mgmt-resource>=1.15.0
azure-identity>=1.5.0

Be sure to use these versions of the libraries. Using older versions will result in errors such as "'AzureCliCredential' object object has no attribute 'signed_session'."

In a terminal or command prompt with the virtual environment activated, install the requirements:

pip install -r requirements.txt

3: Write code to provision a resource group

Create a Python file named provision_rg.py with the following code. The comments explain the details:

# Import the needed credential and management objects from the libraries.
from azure.mgmt.resource import ResourceManagementClient
from azure.identity import AzureCliCredential
import os

# Acquire a credential object using CLI-based authentication.
credential = AzureCliCredential()

# 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://docs.microsoft.com/azure/developer/python/azure-sdk-overview#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.

# Update the resource group with 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()

This code uses CLI-based authentication (using AzureCliCredential) because it demonstrates actions that you might otherwise do with the Azure CLI directly. In both cases you're using the same identity for authentication.

To use such code in a production script (for example, to automate VM management), use DefaultAzureCredential (recommended) or a service principal based method as describe in How to authenticate Python apps with Azure services.

4: Run the script

python provision_rg.py

5: Verify the resource group

You can verify that the group exists through the Azure portal or the Azure CLI.

  • Azure portal: open the Azure portal, select Resource groups, and check that the group is listed. If you've already had the portal open, use the Refresh command to update the list.

  • Azure CLI: run the following command:

    az group show -n PythonAzureExample-rg
    

6: Clean up resources

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

Run this command if you don't need to keep the resource group provisioned in this example. Resource groups don't incur any ongoing charges in your subscription, but it's a good practice to clean up any group that you aren't actively using. The --no-wait argument allows the command to return immediately instead of waiting for the operation to finish.

You can also use the ResourceManagementClient.resource_groups.delete method to delete a resource group from code.

For reference: equivalent Azure CLI commands

The following Azure CLI commands complete the same provisioning steps as the Python script:

az group create -n PythonAzureExample-rg -l centralus

See also