|
| 1 | +--- |
| 2 | +title: Manage resource groups - Python |
| 3 | +description: Use Python to manage your resource groups through Azure Resource Manager. Shows how to create, list, and delete resource groups. |
| 4 | +author: tfitzmac |
| 5 | +ms.topic: conceptual |
| 6 | +ms.date: 02/27/2023 |
| 7 | +ms.author: tomfitz |
| 8 | +--- |
| 9 | +# Manage Azure resource groups by using Python |
| 10 | + |
| 11 | +Learn how to use Python with [Azure Resource Manager](overview.md) to manage your Azure resource groups. |
| 12 | + |
| 13 | +[!INCLUDE [AI attribution](../../../includes/ai-generated-attribution.md)] |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +* Python 3.7 or later installed. To install the latest, see [Python.org](https://www.python.org/downloads/) |
| 18 | + |
| 19 | +* The following Azure library packages for Python installed in your virtual environment. To install any of the packages, use `pip install {package-name}` |
| 20 | + * azure-identity |
| 21 | + * azure-mgmt-resource |
| 22 | + * azure-mgmt-storage |
| 23 | + |
| 24 | +* The examples in this article use CLI-based authentication (`AzureCliCredential`). Depending on your environment, you may need to run `az login` first to authenticate. |
| 25 | + |
| 26 | +* An environment variable with your Azure subscription ID. To get your Azure subscription ID, use: |
| 27 | + |
| 28 | + ```azurecli-interactive |
| 29 | + az account show --name 'your subscription name' --query id -o tsv |
| 30 | + ``` |
| 31 | + |
| 32 | + To set the value, use the option for your environment. |
| 33 | + |
| 34 | + #### [Windows](#tab/windows) |
| 35 | + |
| 36 | + ```console |
| 37 | + setx AZURE_SUBSCRIPTION_ID your-subscription-id |
| 38 | + ``` |
| 39 | + |
| 40 | + > [!NOTE] |
| 41 | + > If you only need to access the environment variable in the current running console, you can set the environment variable with `set` instead of `setx`. |
| 42 | +
|
| 43 | + After you add the environment variables, you may need to restart any running programs that will need to read the environment variable, including the console window. For example, if you're using Visual Studio as your editor, restart Visual Studio before running the example. |
| 44 | + |
| 45 | + #### [Linux](#tab/linux) |
| 46 | + |
| 47 | + ```bash |
| 48 | + export AZURE_SUBSCRIPTION_ID=your-subscription-id |
| 49 | + ``` |
| 50 | + |
| 51 | + After you add the environment variables, run `source ~/.bashrc` from your console window to make the changes effective. |
| 52 | + |
| 53 | + #### [macOS](#tab/macos) |
| 54 | + |
| 55 | + ##### Bash |
| 56 | + |
| 57 | + Edit your .bash_profile, and add the environment variables: |
| 58 | + |
| 59 | + ```bash |
| 60 | + export AZURE_SUBSCRIPTION_ID=your-subscription-id |
| 61 | + ``` |
| 62 | + |
| 63 | + After you add the environment variables, run `source ~/.bash_profile` from your console window to make the changes effective. |
| 64 | + |
| 65 | +## What is a resource group? |
| 66 | + |
| 67 | +A resource group is a container that holds related resources for an Azure solution. The resource group can include all the resources for the solution, or only those resources that you want to manage as a group. You decide how you want to add resources to resource groups based on what makes the most sense for your organization. Generally, add resources that share the same lifecycle to the same resource group so you can easily deploy, update, and delete them as a group. |
| 68 | + |
| 69 | +The resource group stores metadata about the resources. When you specify a location for the resource group, you're specifying where that metadata is stored. For compliance reasons, you may need to ensure that your data is stored in a particular region. |
| 70 | + |
| 71 | +## Create resource groups |
| 72 | + |
| 73 | +To create a resource group, use [ResourceManagementClient.resource_groups.create_or_update](/python/api/azure-mgmt-resource/azure.mgmt.resource.resources.v2022_09_01.operations.resourcegroupsoperations#azure-mgmt-resource-resources-v2022-09-01-operations-resourcegroupsoperations-create-or-update). |
| 74 | + |
| 75 | +```python |
| 76 | +import os |
| 77 | +from azure.identity import AzureCliCredential |
| 78 | +from azure.mgmt.resource import ResourceManagementClient |
| 79 | + |
| 80 | +credential = AzureCliCredential() |
| 81 | +subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] |
| 82 | + |
| 83 | +resource_client = ResourceManagementClient(credential, subscription_id) |
| 84 | + |
| 85 | +rg_result = resource_client.resource_groups.create_or_update( |
| 86 | + "exampleGroup", |
| 87 | + { |
| 88 | + "location": "westus" |
| 89 | + } |
| 90 | +) |
| 91 | + |
| 92 | +print(f"Provisioned resource group with ID: {rg_result.id}") |
| 93 | +``` |
| 94 | + |
| 95 | +## List resource groups |
| 96 | + |
| 97 | +To list the resource groups in your subscription, use [ResourceManagementClient.resource_groups.list](/python/api/azure-mgmt-resource/azure.mgmt.resource.resources.v2022_09_01.operations.resourcegroupsoperations#azure-mgmt-resource-resources-v2022-09-01-operations-resourcegroupsoperations-list). |
| 98 | + |
| 99 | +```python |
| 100 | +import os |
| 101 | +from azure.identity import AzureCliCredential |
| 102 | +from azure.mgmt.resource import ResourceManagementClient |
| 103 | + |
| 104 | +credential = AzureCliCredential() |
| 105 | +subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] |
| 106 | + |
| 107 | +resource_client = ResourceManagementClient(credential, subscription_id) |
| 108 | + |
| 109 | +rg_list = resource_client.resource_groups.list() |
| 110 | + |
| 111 | +for rg in rg_list: |
| 112 | + print(rg.name) |
| 113 | +``` |
| 114 | + |
| 115 | +To get one resource group, provide the name of the resource group. |
| 116 | + |
| 117 | +```python |
| 118 | +import os |
| 119 | +from azure.identity import AzureCliCredential |
| 120 | +from azure.mgmt.resource import ResourceManagementClient |
| 121 | + |
| 122 | +credential = AzureCliCredential() |
| 123 | +subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] |
| 124 | + |
| 125 | +resource_client = ResourceManagementClient(credential, subscription_id) |
| 126 | + |
| 127 | +rg_result = resource_client.resource_groups.get("exampleGroup") |
| 128 | + |
| 129 | +print(f"Retrieved resource group {rg_result.name} in the {rg_result.location} region with resource ID {rg_result.id}") |
| 130 | +``` |
| 131 | + |
| 132 | +## Delete resource groups |
| 133 | + |
| 134 | +To delete a resource group, use [ResourceManagementClient.resource_groups.begin_delete](/python/api/azure-mgmt-resource/azure.mgmt.resource.resources.v2022_09_01.operations.resourcegroupsoperations#azure-mgmt-resource-resources-v2022-09-01-operations-resourcegroupsoperations-begin-delete). |
| 135 | + |
| 136 | +```python |
| 137 | +import os |
| 138 | +from azure.identity import AzureCliCredential |
| 139 | +from azure.mgmt.resource import ResourceManagementClient |
| 140 | + |
| 141 | +credential = AzureCliCredential() |
| 142 | +subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] |
| 143 | + |
| 144 | +resource_client = ResourceManagementClient(credential, subscription_id) |
| 145 | + |
| 146 | +rg_result = resource_client.resource_groups.begin_delete("exampleGroup") |
| 147 | +``` |
| 148 | + |
| 149 | +For more information about how Azure Resource Manager orders the deletion of resources, see [Azure Resource Manager resource group deletion](delete-resource-group.md). |
| 150 | + |
| 151 | +## Deploy resources |
| 152 | + |
| 153 | +You can deploy Azure resources by using Python classes or by deploying an Azure Resource Manager (ARM) template. |
| 154 | + |
| 155 | +The following example creates a storage account. The name you provide for the storage account must be unique across Azure. |
| 156 | + |
| 157 | +```python |
| 158 | +import os |
| 159 | +import random |
| 160 | +from azure.identity import AzureCliCredential |
| 161 | +from azure.mgmt.storage import StorageManagementClient |
| 162 | + |
| 163 | +credential = AzureCliCredential() |
| 164 | +subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] |
| 165 | + |
| 166 | +random_postfix = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz1234567890', k=13)) |
| 167 | +storage_account_name = "demostore" + random_postfix |
| 168 | + |
| 169 | +storage_client = StorageManagementClient(credential, subscription_id) |
| 170 | + |
| 171 | +storage_account_result = storage_client.storage_accounts.begin_create( |
| 172 | + "exampleGroup", |
| 173 | + storage_account_name, |
| 174 | + { |
| 175 | + "location": "westus", |
| 176 | + "sku": { |
| 177 | + "name": "Standard_LRS" |
| 178 | + } |
| 179 | + } |
| 180 | +) |
| 181 | +``` |
| 182 | + |
| 183 | +To deploy an ARM template, use [ResourceManagementClient.deployments.begin_create_or_update](/python/api/azure-mgmt-resource/azure.mgmt.resource.resources.v2022_09_01.operations.deploymentsoperations#azure-mgmt-resource-resources-v2022-09-01-operations-deploymentsoperations-begin-create-or-update). |
| 184 | + |
| 185 | +```python |
| 186 | +import os |
| 187 | +import json |
| 188 | +from azure.identity import AzureCliCredential |
| 189 | +from azure.mgmt.resource import ResourceManagementClient |
| 190 | +from azure.mgmt.resource.resources.models import DeploymentMode |
| 191 | + |
| 192 | +credential = AzureCliCredential() |
| 193 | +subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] |
| 194 | + |
| 195 | +resource_client = ResourceManagementClient(credential, subscription_id) |
| 196 | + |
| 197 | +with open("storage.json", "r") as template_file: |
| 198 | + template_body = json.load(template_file) |
| 199 | + |
| 200 | +rg_deployment_result = resource_client.deployments.begin_create_or_update( |
| 201 | + "exampleGroup", |
| 202 | + "exampleDeployment", |
| 203 | + { |
| 204 | + "properties": { |
| 205 | + "template": template_body, |
| 206 | + "parameters": { |
| 207 | + "storagePrefix": { |
| 208 | + "value": "demostore" |
| 209 | + }, |
| 210 | + }, |
| 211 | + "mode": DeploymentMode.incremental |
| 212 | + } |
| 213 | + } |
| 214 | +) |
| 215 | +``` |
| 216 | + |
| 217 | +The following example shows the ARM template you're deploying: |
| 218 | + |
| 219 | +```json |
| 220 | +{ |
| 221 | + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", |
| 222 | + "contentVersion": "1.0.0.0", |
| 223 | + "parameters": { |
| 224 | + "storagePrefix": { |
| 225 | + "type": "string", |
| 226 | + "minLength": 3, |
| 227 | + "maxLength": 11 |
| 228 | + } |
| 229 | + }, |
| 230 | + "variables": { |
| 231 | + "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]" |
| 232 | + }, |
| 233 | + "resources": [ |
| 234 | + { |
| 235 | + "type": "Microsoft.Storage/storageAccounts", |
| 236 | + "apiVersion": "2022-09-01", |
| 237 | + "name": "[variables('uniqueStorageName')]", |
| 238 | + "location": "eastus", |
| 239 | + "sku": { |
| 240 | + "name": "Standard_LRS" |
| 241 | + }, |
| 242 | + "kind": "StorageV2", |
| 243 | + "properties": { |
| 244 | + "supportsHttpsTrafficOnly": true |
| 245 | + } |
| 246 | + } |
| 247 | + ] |
| 248 | +} |
| 249 | +``` |
| 250 | + |
| 251 | +For more information about deploying an ARM template, see [Deploy resources with ARM templates and Azure CLI](../templates/deploy-cli.md). |
| 252 | + |
| 253 | +## Lock resource groups |
| 254 | + |
| 255 | +Locking prevents other users in your organization from accidentally deleting or modifying critical resources. |
| 256 | + |
| 257 | +To prevent a resource group and its resources from being deleted, use [ManagementLockClient.management_locks.create_or_update_at_resource_group_level](/python/api/azure-mgmt-resource/azure.mgmt.resource.locks.v2016_09_01.operations.managementlocksoperations#azure-mgmt-resource-locks-v2016-09-01-operations-managementlocksoperations-create-or-update-at-resource-group-level). |
| 258 | + |
| 259 | +```python |
| 260 | +import os |
| 261 | +from azure.identity import AzureCliCredential |
| 262 | +from azure.mgmt.resource import ManagementLockClient |
| 263 | + |
| 264 | +credential = AzureCliCredential() |
| 265 | +subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] |
| 266 | + |
| 267 | +lock_client = ManagementLockClient(credential, subscription_id) |
| 268 | + |
| 269 | +lock_result = lock_client.management_locks.create_or_update_at_resource_group_level( |
| 270 | + "exampleGroup", |
| 271 | + "lockGroup", |
| 272 | + { |
| 273 | + "level": "CanNotDelete" |
| 274 | + } |
| 275 | +) |
| 276 | +``` |
| 277 | + |
| 278 | +To get the locks for a resource group, use [ManagementLockClient.management_locks.list_at_resource_group_level](/python/api/azure-mgmt-resource/azure.mgmt.resource.locks.v2016_09_01.operations.managementlocksoperations#azure-mgmt-resource-locks-v2016-09-01-operations-managementlocksoperations-list-at-resource-group-level). |
| 279 | + |
| 280 | +```python |
| 281 | +import os |
| 282 | +from azure.identity import AzureCliCredential |
| 283 | +from azure.mgmt.resource import ManagementLockClient |
| 284 | + |
| 285 | +credential = AzureCliCredential() |
| 286 | +subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] |
| 287 | + |
| 288 | +lock_client = ManagementLockClient(credential, subscription_id) |
| 289 | + |
| 290 | +lock_result = lock_client.management_locks.get_at_resource_group_level("exampleGroup", "lockGroup") |
| 291 | + |
| 292 | +print(f"Lock {lock_result.name} applies {lock_result.level} lock") |
| 293 | +``` |
| 294 | + |
| 295 | +To delete a lock on a resource group, use [ManagementLockClient.management_locks.delete_at_resource_group_level](/python/api/azure-mgmt-resource/azure.mgmt.resource.locks.v2016_09_01.operations.managementlocksoperations#azure-mgmt-resource-locks-v2016-09-01-operations-managementlocksoperations-delete-at-resource-group-level). |
| 296 | + |
| 297 | +```python |
| 298 | +import os |
| 299 | +from azure.identity import AzureCliCredential |
| 300 | +from azure.mgmt.resource import ManagementLockClient |
| 301 | + |
| 302 | +credential = AzureCliCredential() |
| 303 | +subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] |
| 304 | + |
| 305 | +lock_client = ManagementLockClient(credential, subscription_id) |
| 306 | + |
| 307 | +lock_client.management_locks.delete_at_resource_group_level("exampleGroup", "lockGroup") |
| 308 | +``` |
| 309 | + |
| 310 | +For more information, see [Lock resources with Azure Resource Manager](lock-resources.md). |
| 311 | + |
| 312 | +## Tag resource groups |
| 313 | + |
| 314 | +You can apply tags to resource groups and resources to logically organize your assets. For information, see [Using tags to organize your Azure resources](tag-resources.md). |
| 315 | + |
| 316 | +## Export resource groups to templates |
| 317 | + |
| 318 | +To assist with creating ARM templates, you can export a template from existing resources. For more information, see [Use Azure portal to export a template](../templates/export-template-portal.md). |
| 319 | + |
| 320 | +## Manage access to resource groups |
| 321 | + |
| 322 | +[Azure role-based access control (Azure RBAC)](../../role-based-access-control/overview.md) is the way that you manage access to resources in Azure. For more information, see [Add or remove Azure role assignments using Azure CLI](../../role-based-access-control/role-assignments-cli.md). |
| 323 | + |
| 324 | +## Next steps |
| 325 | + |
| 326 | +- To learn Azure Resource Manager, see [Azure Resource Manager overview](overview.md). |
| 327 | +- For more information about authentication options, see [Authenticate Python apps to Azure services by using the Azure SDK for Python](/azure/developer/python/sdk/authentication-overview). |
0 commit comments