edgeman / resource-manager-python-resources-and-groups

An example illustrating how to use Python to manipulate Azure resources and resource groups

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

services platforms author

Manage Azure resources and resource groups with Python

This sample explains how to manage your resources and resource groups in Azure using the Azure Python SDK.

On this page

Run this sample

  1. If you don't already have it, install Python.

  2. We recommend to use a virtual environnement to run this example, but it's not mandatory. You can initialize a virtualenv this way:

    pip install virtualenv
    virtualenv mytestenv
    cd mytestenv
    source bin/activate
  3. Clone the repository.

    git clone https://github.com/Azure-Samples/resource-manager-python-resources-and-groups.git
  4. Install the dependencies using pip.

    cd resource-manager-python-resources-and-groups
    pip install -r requirements.txt
  5. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  6. Export these environment variables into your current shell.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
  7. Run the sample.

    python example.py

What is example.py doing?

The sample walks you through several resource and resource group management operations. It starts by setting up a ResourceManagementClient object using your subscription and credentials.

import os
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient

subscription_id = os.environ.get(
    '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id
credentials = ServicePrincipalCredentials(
client = ResourceManagementClient(credentials, subscription_id)

It also sets up a ResourceGroup object (resource_group_params) to be used as a parameter in some of the API calls.

resource_group_params = {'location':'westus'}

There are a couple of supporting functions (print_item and print_properties) that print a resource group and it's properties. With that set up, the sample lists all resource groups for your subscription, it performs these operations.

List resource groups

List the resource groups in your subscription.

for item in client.resource_groups.list():

Create a resource group

client.resource_groups.create_or_update('azure-sample-group', resource_group_params)

Update a resource group

The sample adds a tag to the resource group.

resource_group_params.update(tags={'hello': 'world'})
client.resource_groups.create_or_update('azure-sample-group', resource_group_params)

Create a key vault in the resource group

key_vault_params = {
    'location': 'westus',
    'properties':  {
        'sku': {'family': 'A', 'name': 'standard'},
        'tenantId': os.environ['AZURE_TENANT_ID'],
        'accessPolicies': [],
        'enabledForDeployment': True,
        'enabledForTemplateDeployment': True,
        'enabledForDiskEncryption': True

List resources within the group

for item in client.resource_groups.list_resources(GROUP_NAME):

Export the resource group template

client.resource_groups.export_template(GROUP_NAME, ['*'])

Delete a resource group

delete_async_operation = client.resource_groups.delete('azure-sample-group')


An example illustrating how to use Python to manipulate Azure resources and resource groups

License:MIT License


Language:Python 100.0%