Home > Blockchain >  How can I check if a resource group is created in Azure using the resource_group class with Python
How can I check if a resource group is created in Azure using the resource_group class with Python

Time:07-20

So, after creating a resource group with the below code, I want to check if it has been created. I saw some examples with code that use resorcse_groups.check_exists(resource_group_name), but I didn't see it in the Microsoft documentation.

def create_resource_group(self):
    resource_group_region = {'location': region_name}
    resource_group_name = self.resource_client.resource_groups.create_or_update(
        self.config.resource_group, resource_group_region
    )

CodePudding user response:

I didn't really find something related to the "check_exists" method but here is one of the workarounds where we list the resource groups in the subscription and use the if condition.

from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient

GROUP_NAME = "<YOUR_RESOURCE_GROUP_NAME>"
subscription_id = "<YOUR_SUBSCRIPTION_ID>"

credentials = DefaultAzureCredential()
client = ResourceManagementClient(credentials, subscription_id)

for item in client.resource_groups.list():
    if(item.name==GROUP_NAME):
        print("The resource group exists")
    

RESULTS:

enter image description here

  • Related