Python for Azure: Mastering Azure Subscription Filtering with Python

Pavleen Singh Bali
Python for Azure
Published in
3 min readOct 12, 2023

Introduction: Managing Azure subscriptions effectively is crucial for organizations to maintain control over their cloud resources. As businesses scale and utilize a multitude of subscriptions, manual handling becomes impractical and time-consuming. In this modern era of automation, Python emerges as a powerful tool to streamline this process. By leveraging Python’s capabilities alongside the tabulate library, we can create a seamless solution to filter and display Azure subscriptions based on specific keywords.

This article introduces you to the world of automating subscription filtering using Python and Tabulate, empowering you to efficiently navigate through your Azure resources and focus on optimizing your cloud infrastructure for enhanced productivity. Explore this step-by-step guide to effortlessly gain insights into your subscriptions, saving time and effort while ensuring an organized and well-managed Azure environment.

Firstly recalling the fundamental/classical AZ CLI method for subscription filtering

az login
az account list --query "[?contains(name, '<keyword>')]" -o table  

Using command with a specific keyword

az account list --query "[?contains(name, 'data')]" -o table
This is the output of the above AZ CLI command

Azure CLI method excels at quick and efficient command-line operations, offering an intuitive and user-friendly interface.

Now in the next section of the article, we will explore the python based script and run the workflow for comparison. Also, key differenciation between both the methods incl. their respective advantages will be discussed.

Hands-On Implementation via Azure Portal & Python SDK for Azure

Prerequisites

  • Python 3.6 or later is required to use this package
  • You must have an Azure subscription to run the python code below.

Setup

pip install -r requirements.txt

Workflow

1. In this workflow demo, I am using the python script to list the subscriptions filtered with a specific keyword

2. The script below “list_filtered_subscriptions.py”, demonstrates the usage of Python SDK for Azure for implementing the above said workflow as mentioned in step 1.

3. Before running the script, in the terminal of the IDE do the following steps:

  • Log in to your Azure account
az login --tenant <tenant_id>

[Info]: Now, the “_get_credential” method using “DefaultAzureCredential” library can do the authentication properly for the current workflow.

  • After selecting the correct ‘Python Interpreter’ & correct ‘Configuration’ for the scope of your project like “Working Directory” etc. , run the scriptlist_filtered_subscriptions.py
  • Below is the Python run-console with the workflow logs, please observed the highlighted text below (As the info is sensitive so its hidden).
Python console with work-flow logs

Key Points from the Workflow:

  • Python based script gives more flexibility as we can leverage the power and elsaticity of python librabries and functions.
  • We can perform analytics on the output result e.g., refine it more or eliminate non-important information and create a formatted table of the result.
  • The output result can be further used for other Azure realted operations e.g., be it infra deployments or configuration changes etc.

--

--

Pavleen Singh Bali
Python for Azure

| Consultant @ Microsoft | Inspired Human | Chasing Dreams | Belief in "Cosmic <--> Self reflection" as a bidirectional Transaction |