Azure Virtual Machine Management Sample using Azure SDK for Go

Utolsó frissítés: 2017.01.05.
Szerkesztés a GitHubon

This sample demonstrates how to manage your Azure virtual machine (VM) using Go, and specifically how to:

  • Create a virtual machine
  • Tag a virtual machine
  • Attach and detach data disks
  • Update the OS disk size
  • Start, restart and stop a virtual machine
  • List virtual machines
  • Delete a virtual machine

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

On this page

Run this sample

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

  2. Clone the repository.

    git clone
  3. Install the dependencies using glide.

    cd virtual-machines-go-manage
    glide install
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Set the following environment variables using the information from the service principle that you created.

    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}

    [AZURE.NOTE] On Windows, use set instead of export.

  6. Run the sample.

    go run example.go

What does example.go do?

First, it creates all resources needed before creating a VM (resource group, storage account, virtual network, subnet)

Create a Linux VM

publicIPaddress, nicParameters, err := createPIPandNIC(linuxVMname, subnetInfo)
vm := setVMparameters(linuxVMname, "Canonical", "UbuntuServer", "16.04.0-LTS", *nicParameters.ID)
vmClient.CreateOrUpdate(groupName, linuxVMname, vm, nil)

Get the VM properties

vm, err := vmClient.Get(groupName, vmName, compute.InstanceView)

Tag the VM

vm.Tags = &(map[string]*string{
    "who rocks": to.StringPtr("golang"),
    "where":     to.StringPtr("on azure"),
_, err := vmClient.CreateOrUpdate(groupName, vmName, *vm, nil)

Attach data disks to the VM

    vm.StorageProfile.DataDisks = &[]compute.DataDisk{
            Lun:  to.Int32Ptr(0),
            Name: to.StringPtr("dataDisk"),
            Vhd: &compute.VirtualHardDisk{
                URI: to.StringPtr(fmt.Sprintf(vhdURItemplate, accountName, fmt.Sprintf("dataDisks-%v", vmName))),
            CreateOption: compute.Empty,
            DiskSizeGB:   to.Int32Ptr(1),
    _, err := vmClient.CreateOrUpdate(groupName, vmName, *vm, nil)

Detach data disks

vm.StorageProfile.DataDisks = &[]compute.DataDisk{}
_, err := vmClient.CreateOrUpdate(groupName, vmName, *vm, nil)

Updates the VM's OS disk size

if vm.StorageProfile.OsDisk.DiskSizeGB == nil {
    vm.StorageProfile.OsDisk.DiskSizeGB = to.Int32Ptr(0)
_, err := vmClient.Deallocate(groupName, vmName, nil)
if *vm.StorageProfile.OsDisk.DiskSizeGB <= 0 {
    *vm.StorageProfile.OsDisk.DiskSizeGB = 256
*vm.StorageProfile.OsDisk.DiskSizeGB += 10
_, err = vmClient.CreateOrUpdate(groupName, vmName, *vm, nil)

Starts, restarts and stops the VM

_, err = vmClient.Start(groupName, vmName, nil)
_, err = vmClient.Restart(groupName, vmName, nil)
_, err = vmClient.PowerOff(groupName, vmName, nil)

Creates a Windows VM

publicIPaddress, nicParameters, err := createPIPandNIC(windowsVMname, subnetInfo)
vm := setVMparameters(windowsVMname, "MicrosoftWindowsServerEssentials", "WindowsServerEssentials", "WindowsServerEssentials", *nicParameters.ID)
if _, err := vmClient.CreateOrUpdate(groupName, windowsVMname, vm, nil); err != nil {
    return err

Lists all the VMs in your subscription.

vmList, err := vmClient.ListAll()

Delete the VMs and other resources

_, err = vmClient.Delete(groupName, linuxVMname, nil)
_, err = vmClient.Delete(groupName, windowsVMname, nil)

_, err = groupClient.Delete(groupName, nil)

More information

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.