A resource is an item that can be managed and is accessible through Azure. Examples of resources include virtual networks, virtual computers, storage accounts, web applications, and databases.
A resource group is a container that contains connected resources. All the solution’s resources may be included in the resource group, or you may choose to include only the resources you want to manage collectively. For ease of deployment, updating, and deletion as a group, add resources with the same lifecycle to the same resource group.
Prerequisites
Step 1 – Open Cloud Shell
After you are done adding a few resource groups, open Azure Cloud Shell. Microsoft manages Cloud Shell, therefore it includes support for well-known command-line tools and languages. When using the Azure CLI or Azure PowerShell cmdlets to access your resources, Cloud Shell additionally securely authenticates itself automatically.
You can select between Bash or PowerShell.
If you choose to make use of your own terminal, run the following commands:
Resource Management Client provides operations for working with resources and resource groups.
pip install azure-mgmt-resource
Support for Azure Active Directory (Azure AD) token authentication is provided across the Azure SDK by the Azure Identity library. In order to build Azure SDK clients that enable Azure AD token authentication, it provides a collection of TokenCredential implementations.
pip install azure-identity
Step 2 – Write your Python code
Now, use the global search on the Azure portal and type Subscriptions. Open a new Python file and copy your Subscription ID into the following code below. This will list the names of all resource groups attached to your subscription. You can alternatively just print each object rg to get other details such as location etc.
If you’re using your own terminal, run the following command for authentication:
az login
Alternatively, you can also upload your Python file onto the Azure Cloud Shell by clicking on clicking on the Upload/Download files button.
After uploading the file, in order to execute the file, run:
python <filename>.py
Output with the list of resource groups:
Additionally, if you want to list all the resources available, add these lines of code to your program.
Output with the list of resources:
Conclusion
We learned to list resource groups and resources by making use of Azure SDK. Thanks for taking the time and reading my blog!