Share via


Python and Azure VMs

I have been experimenting lately with the Azure SDK for Python, in particular with the service management API. I found some points in the documentation that were unclear to me, so I am posting what I discovered in the process.

If you want to create a virtual machine, you need to:

  1. Create a cloud service of which the VM will be an instance
  2. Select a vhd image for the machine, either from Microsoft catalog or one you created.
  3. Indicate which blob in which storage account will store the vhd file of the VM
  4. Create a configuration set containing machine metadata
  5. Finally, deploy the VM

Note that you'll need a management certificate to interface with the Azure Service Management Service. The documentation suggests using openssl to create one, which is fine on Linux and Mac, but not on Windows. On Windows, use makecert instead:

makecert -sky exchange -r -n "CN=<CertificateName>" -pe -a sha1 -len 2048 -ss My "<CertificateName>.cer"

This will put the certificate into the current user's personal store and generate a .cer file for you to upload to Azure.

Also note that there is a bug in the Python SDK which affects the creation of endpoints. See https://github.com/WindowsAzure/azure-sdk-for-python/pull/83 for a description.

You will need to edit the init.py file as described on github.

You can do the rest in python:

from azure import *
from azure.servicemanagement import *

subscription_id = '<enter your subscription id>'
# The certificate comes from the local store, not from a .pem file on Windows

certificate_path ="CURRENT_USER\\my\\<enter your certificate name>"

# instantiate a service management service
sms = ServiceManagementService(subscription_id, certificate_path)

#provide a service name and location

name = '<name of service you want>'
location = 'West US'

# You can either set the location or an affinity_group
sms.create_hosted_service(service_name=name, label=name,location=location)

# Name of an os image as returned by list_os_images (catalog and yours)
image_name = '<your image name>.vhd'

# Destination url://storage account/container/blob where the VM disk will be created
media_link = 'https://<account name>.blob.core.windows.net/vhds/'+name+'.vhd'

# The documentation shows a Linux VM. Windows is more complicated.
# WindowsConfigurationSet contains metadata for a Windows VM
windows_config = WindowsConfigurationSet(<machine name>, '<admin password>')
# by default the api will look for domain credentials. if you want no domain:

windows_config.domain_join = None

# Here's the hard disk for the os

os_hd = OSVirtualHardDisk(image_name, media_link)

# Unless you specify endpoints, you won't be able to connect to the VM.

# The documentation is unclear on the matter.

endpoint_config = ConfigurationSet()
endpoint_config.configuration_set_type = 'NetworkConfiguration'

endpoint1 = ConfigurationSetInputEndpoint(name = 'rdp', protocol = 'tcp', port = '33890', local_port = '3389', load_balanced_endpoint_set_name = None, enable_direct_server_return = False)
endpoint2 = ConfigurationSetInputEndpoint(name = 'web', protocol = 'tcp', port = '8080', local_port = '80', load_balanced_endpoint_set_name = None, enable_direct_server_return = False)

#endpoints must be specified as elements in a list

endpoint_config.input_endpoints.input_endpoints.append(endpoint1)
endpoint_config.input_endpoints.input_endpoints.append(endpoint2)

#Finally you can deploy the VM

sms.create_virtual_machine_deployment(service_name=name,
deployment_name=name,
deployment_slot='production',
label=name,
role_name=name,
system_config=windows_config,
network_config = endpoint_config,
os_virtual_hard_disk=os_hd,
role_size='Small')

Powered by Qumana

Comments

  • Anonymous
    August 06, 2013
    Hi there, I've been following your example (and the similar one provided by Azure), and I'm running into some errors. The code executes without error, but all I end up with is a Cloud Service and no virtual machine. When I click on the Cloud Service, all I see is "You have nothing deployed to the production environment." Am I missing something?

  • Anonymous
    August 17, 2015
    It is giving me 'A parameter was incorrect' error