services | platforms | author |
---|---|---|
azure-resource-manager |
python |
lmazuel |
This sample explains how to manage your resources and resource groups in Azure using the Azure Python SDK.
On this page
-
If you don't already have it, install Python.
-
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
-
Clone the repository.
git clone https://github.com/Azure-Samples/resource-manager-python-resources-and-groups.git
-
Install the dependencies using pip.
cd resource-manager-python-resources-and-groups pip install -r requirements.txt
-
Create an Azure service principal either through Azure CLI, PowerShell or the portal.
-
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}
-
Run the sample.
python example.py
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(
'AZURE_SUBSCRIPTION_ID',
'11111111-1111-1111-1111-111111111111') # your Azure Subscription Id
credentials = ServicePrincipalCredentials(
client_id=os.environ['AZURE_CLIENT_ID'],
secret=os.environ['AZURE_CLIENT_SECRET'],
tenant=os.environ['AZURE_TENANT_ID']
)
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 the resource groups in your subscription.
for item in client.resource_groups.list():
print_item(item)
client.resource_groups.create_or_update('azure-sample-group', resource_group_params)
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)
key_vault_params = {
'location': 'westus',
'properties': {
'sku': {'family': 'A', 'name': 'standard'},
'tenantId': os.environ['AZURE_TENANT_ID'],
'accessPolicies': [],
'enabledForDeployment': True,
'enabledForTemplateDeployment': True,
'enabledForDiskEncryption': True
}
}
client.resources.create_or_update(GROUP_NAME,
'Microsoft.KeyVault',
'',
'vaults',
'azureSampleVault',
'2015-06-01',
key_vault_params)
for item in client.resource_groups.list_resources(GROUP_NAME):
print_item(item)
client.resource_groups.export_template(GROUP_NAME, ['*'])
delete_async_operation = client.resource_groups.delete('azure-sample-group')
delete_async_operation.wait()