Azure Virtual Machines Management Samples - Python

Last updated: 30/06/2016
Edit on GitHub

These samples demonstrate how to perform common management tasks with Microsoft Azure Virtual Machines using Python. The code examples provided show how to do the following:

  • Create a virtual machine
  • Start a virtual machine
  • Stop a virtual machine
  • Restart a virtual machine
  • Update a virtual machine
    • Expand a drive
    • Tag a virtual machine
    • Attach data disks
    • Detach data disks
  • List virtual machines
  • Delete a virtual machine.

Running this sample

  1. If you don't already have it, install Python.

    This sample (and the SDK) is compatible with Python 2.7, 3.3, 3.4, and 3.5.

  2. We recommend to use a virtual environment to run this example, but it's not mandatory. Install and initialize the virtual environment with:

    pip install virtualenv
    virtualenv mytestenv
    cd mytestenv
    source bin/activate
  3. Clone the repository.

    git clone
  4. Install the dependencies using pip.

    cd virtual-machines-python-manage
    pip install -r requirements.txt
  5. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  6. Fill in and export these environment variables into your current shell.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
  7. Run the sample.


More information

Here are some helpful links:

If you don't have a Microsoft Azure subscription you can get a FREE trial account here.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact with any additional questions or comments.