akostadinov
akostadinov

Reputation: 18654

Create a virtual machine from a managed image

I have created azure VMs successfully using the ruby sdk but I can't find any example for creating from a managed image. I want to do something like this but with SDK:

az image create --name fedora-75-20180724 --resource-group myteam --source https://eastusimg.blob.core.windows.net/images/fedora-28-20180724.vhd --os-type linux
# create vm without option "--use-unmanaged-disk, --os-type, --storage-account"
az vm create -g myteam -n managed-master --image fedora28-20180724 --size Standard_DS2_V2 --nics managed-master --os-disk-size-gb 40 --public-ip-address-dns-name managed-master --os-disk-name managed-master

But it is not clear to me how to do it as well can't find any example for this. Examples I looked at are here and here. Basically I have no clue how to construct the StorageProfile or maybe I need something more. So I would appreciate a code example for creating such a VM.

Thank you.

Update with a working (unless I forgot something) example based on @4c74356b41's answer:

ComputeModels::StorageProfile.new.tap do |store_profile|
  store_profile.image_reference = ComputeModels::ImageReference.new.tap do |ref|
    # obtain `image` by `compute_client.images.list_by_resource_group`
    # make sure they are in the same region though or you'll see 404
    ref.id = image.id
  end
  store_profile.os_disk = ComputeModels::OSDisk.new.tap do |os_disk|
    os_disk.name = "my-unique-disk-name"
    os_disk.disk_size_gb = 42 # optionally change size
    os_disk.caching = ComputeModels::CachingTypes::ReadWrite # this is a test machine
    os_disk.create_option = ComputeModels::DiskCreateOptionTypes::FromImage
    # setting `managed_disk` is optional
    os_disk.managed_disk = ComputeModels::ManagedDiskParameters.new.tap do |params|
      params.storage_account_type = StorageModels::SkuName::StandardLRS
    end
  end
end

Upvotes: 1

Views: 297

Answers (1)

4c74356b41
4c74356b41

Reputation: 72191

ok, i'm not a ruby person, nor I was able to locate any reasonable docs, but it should be something like this:

store_profile.os_disk = ComputeModels::OSDisk.new.tap do |os_disk|
    os_disk.name = "sample-os-disk-#{vm_name}"
    os_disk.caching = ComputeModels::CachingTypes::None
    os_disk.create_option = ComputeModels::DiskCreateOptionTypes::FromImage
end

or you might need to add the managed disk option and set the storage account type, but i think that is redundant.

Upvotes: 1

Related Questions