azurerm – a Python library for the Azure RM REST API


This post is about an unofficial Azure library for Python called azurerm – a simple set of wrappers for Azure Resource Manager REST calls. If you’re looking for an official Python library for Microsoft Azure, there’s a bundle called azure which installs all the official Azure Python packages. That would be the recommended approach if you’re developing a Python based Azure solution.

azurerm provides an easy framework for making Azure REST calls and getting back the JSON response. This can be handy when you’re doing simple testing and writing  utility scripts. The goal is simplicity over completeness of API. If all you want to do is create/list/query/delete a few Azure Resource Manager resources in a Python script, and you don’t want to go through 2-factor authentication, azurerm provides a simple way to do it. It also provides a basic authentication routine based on adal to get an authentication token.

For example, to list the VNETs in your subscription, you could write something like this:

vnets = azurerm.list_vnets(access_token, subscription_id)
for vnet in vnets['value']:
   print(vnet['name'] + ', ' + vnet['location'])

Or to get the details for all the public IP addresses in your subscription and print out the JSON:

ips = azurerm.list_public_ips(access_token, subscription_id, resource_group)
print(json.dumps(ips, sort_keys=False, indent=2, separators=(',', ': ')))

Installing azurerm

To install azurerm (note azurerm has only been tested with Python 3):

pip install azurem

Authenticating to Azure

azurerm function calls take an access token as the first argument. To get an access token you need a tenant id, and application id and an application secret. Then you can call the get_access_token function like this:

import azurerm
access_token = azurerm.get_access_token(tenant_id, app_id, app_secret)

To get these values you need to follow some one-time steps to register an application with Azure, create a Active Directory Service Principal (like a virtual user) and assign its privileges. These steps are well-documented here: Authenticating a service principal with Azure Resource Manager. I also cover the basics in this post: Azure Resource Manager REST calls from Python.


You can find examples for most azurerm functions documented in the github project: – see the readme and The readme also includes the current list of supported functions.

Here’s another example, including authentication, showing how to issue a scale action against a VM Scale Set (including authentication), in this case changing the number of VMs in a Scale Set to 5:

import azurerm
tenant_id = 'your_tenant_id'
app_id = 'your_application_id'
app_secret = 'your_app_secret'
subscription_id = 'your_sub_id'
access_token = azurerm.get_access_token(tenant_id, app_id, app_secret)
scaleoutput = azurerm.scale_vmss(access_token, subscription_id, 'myresourcegroup', 'myvmss', 'Standard_A1', 'Standard', 5)

Add pygame for a GUI console

Here’s an example of adding a GUI library (pygame) to make a simple Azure console for VM Scale Sets called vmssdashboard – see the source code here:

Image of VMSS Dashboard

Future versions

The simple REST framework of azurerm means it’s easy to add new functions to the library. I mostly add them as I need them and am open to requests. What I will try and avoid doing is making it any more complicated than it is. Official libraries exist for Azure. azurem is about making the Azure REST API more accessible.

This entry was posted in Cloud, Computers and Internet, Python and tagged , , . Bookmark the permalink.

One Response to azurerm – a Python library for the Azure RM REST API

  1. Pingback: Azure Resource Manager REST calls from Python | MSFT Stack

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s