New_to_work
New_to_work

Reputation: 263

How can I list all the resource groups in my azure account using python?

I am completely new to Azure. I cannot find a proper documentation of Python SDK for Azure anywhere. I want to access all the resources in my azure account using python. Starting with listing all the resource groups present in my account. How can I do that?

Also, please share link of proper documentation if present.

Upvotes: 2

Views: 4791

Answers (2)

BlueJapan
BlueJapan

Reputation: 1746

Read here

import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient

credential = AzureCliCredential()
subscription_id = "Your_ID"

resource_client = ResourceManagementClient(credential, subscription_id)
group_list = resource_client.resource_groups.list()

for group in group_list:
    print(group)

Upvotes: 0

Martin Brandl
Martin Brandl

Reputation: 58931

Here is a good article to get started: Manage Azure resources and resource groups with Python

This is how the python code looks like (taken from the article):

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)

for item in client.resource_groups.list():
    print_item(item)

Upvotes: 4

Related Questions