Home > front end >  Azure python SDK - start or run VM from resource group
Azure python SDK - start or run VM from resource group

Time:08-12

I am not able to start Azure Vm using python code without using clientId and Secrete Id.

Can we start or stop Azure vm in python without using client_id and secrete id.

Here is the code for reference.

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient,ComputeManagementClientConfiguration

credentials = ServicePrincipalCredentials(
    client_id = '<client-id>',
    secret = '<key>',
    tenant = '<tenant-id>'
)

subscription_id = '<subscription-id>'

compute_config = ComputeManagementClientConfiguration(credentials, subscription_id, api_version='2015-05-01-preview')
compute_client = ComputeManagementClient(compute_config)
resource_group_name = '<resource-group>'
vm_name = '<vm-name>'
result = compute_client.virtual_machines.deallocate(resource_group_name, vm_name)

here we are using client Id and all... but I want to stop my Azure Vm without need of applications id/client id..

CodePudding user response:

you can use azure-identity package for this and DefaultAzureCredential:

from azure.identity import DefaultAzureCredential
credentials = DefaultAzureCredential()
compute_config = ComputeManagementClientConfiguration(credentials, subscription_id, api_version='2015-05-01-preview')
compute_client = ComputeManagementClient(compute_config)

https://docs.microsoft.com/en-us/python/api/overview/azure/identity-readme?view=azure-python

main advantage - you can use MSI authentication

  • Related