For an Azure Virtual Machine, we need to have several Azure resources. Before starting with anything, make sure you have an Active Directory service principal. You also need to have the application ID, the authentication key, and the tenant ID that you need in a later.
- Open myPythonProject.py file that was created, and then add this code to enable your application to run:
if __name__ == "__main__":
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute.models import DiskCreateOption
- Next in the .py file, add variables after the import statements to specify common values used in the code:
SUBSCRIPTION_ID = 'subscription-id'
GROUP_NAME = 'myResourceGroup'
LOCATION = 'westus'
VM_NAME = 'myVM'
credentials = ServicePrincipalCredentials(
client_id = 'application-id', secret = 'authentication-key',
tenant = 'tenant-id'
credentials = get_credentials()
To learn more about your query you can go through this: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/python