<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/azurevm-managing-virtual-machines-in-azure/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.revolutionanalytics.com/2018/12/azurevm.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>This is the next article in my series on <a rel="nofollow" href="https://blog.revolutionanalytics.com/2018/11/azurer-intro.html" target="_blank">AzureR</a>, a family of packages for working with Azure in R. I’ll give a short introduction on how to use <a rel="nofollow" href="https://github.com/cloudyr/AzureVM" target="_blank">AzureVM</a> to manage Azure virtual machines, and in particular <a rel="nofollow" href="https://docs.microsoft.com/en-us/azure/machine-learning/data-science-virtual-machine/" target="_blank">Data Science Virtual Machines</a> (DSVMs).</p>
<div>
<h2>Creating a VM</h2>
<p>Creating a VM is as simple as using the <code>create_vm</code> method, which is available as part of the <code>az_subscription</code> and <code>az_resource_group</code> classes.</p>
<div>
<pre><code><span>library</span>(AzureRMR)
<span>library</span>(AzureVM)

## using the subscription method
sub &lt;- az_rm$
    <span>new</span>(<span>tenant=</span><span>&quot;{tenant_id}&quot;</span>, <span>app=</span><span>&quot;{app_id}&quot;</span>, <span>password=</span><span>&quot;{password}&quot;</span>)$
    <span>get_subscription</span>(<span>&quot;{subscription_id}&quot;</span>)

myNewVM &lt;- sub$<span>create_vm</span>(<span>&quot;myNewVM&quot;</span>,
    <span>location=</span><span>&quot;australiaeast&quot;</span>,
    <span>username=</span><span>&quot;datascience&quot;</span>,
    <span>passkey=</span><span>&quot;fAs30q-2a5vF!Z&quot;</span>)  <span># be sure to choose a strong password!</span>


## using the resource group method
rg &lt;- sub$<span>create_resource_group</span>(<span>&quot;myresourcegroup&quot;</span>,
    <span>location=</span><span>&quot;australiaeast&quot;</span>)

myOtherVM &lt;- rg$<span>create_vm</span>(<span>&quot;myOtherVM&quot;</span>,
    <span>username=</span><span>&quot;datascience&quot;</span>,
    <span>passkey=</span><span>&quot;l3Kgrf21%?0DFm&quot;</span>)</code></pre>
</div>
<p>Without any other options, this will create a Windows Server 2016 <a rel="nofollow" href="https://docs.microsoft.com/azure/machine-learning/data-science-virtual-machine/overview?WT.mc_id=Revolutions-blog-davidsmi#whats-included-in-the-data-science-vm" target="_blank">Data Science Virtual Machine</a>, which is pre-installed with several tools useful for analytics: Python, R (and RStudio), Tensorflow, XGBoost, SQL Server, and so on. The size will be a Standard DS3 V2 VM, which has 4 cores, 14GB of memory, 1TB primary disk, and up to 16x28GB data disks.</p>
</div>
<p>A feature of creating the VM under a subscription, as opposed to a resource group, is that it creates a new resource group specifically to hold the VM. This simplifies the task of managing and (eventually) deleting the VM considerably. See “Deleting a VM” below.</p>
<p>You can change the specifications for the VM by providing any of the following arguments:</p>
<ul>
<li><code>os</code>: either “Windows” or “Ubuntu” (for Ubuntu LTS 16.04).</li>
<li><code>size</code>: the VM size. Use the <code>az_subscription$list_vm_sizes()</code> method to see what sizes are available in your region. Note that in Azure, a VM’s size is really a broad-ranging label that encapsulates the number of cores, memory, and disk available.</li>
<li><code>passkey</code>: if creating an Ubuntu VM, you can supply a public key as the <code>passkey</code> argument.</li>
<li><code>userauth_type</code>: set this to “key” if you supply a public key.</li>
</ul>
<p>Setting these will determine whether you get a Windows or Ubuntu DSVM, the login details, and how powerful the VM is in terms of cores and memory/disk capacity. For example, this will create a Linux NC-series DSVM using public key authentication:</p>
<div>
<pre><code>myLinuxVM &lt;- sub$<span>create_vm</span>(<span>&quot;myLinuxVM&quot;</span>,
    <span>location=</span><span>&quot;australiaeast&quot;</span>,
    <span>size=</span><span>&quot;Standard_NC6s_v3&quot;</span>,
    <span>os=</span><span>&quot;Ubuntu&quot;</span>,
    <span>username=</span><span>&quot;datascience&quot;</span>,
    <span>passkey=</span><span>readLines</span>(<span>&quot;~/id_rsa.pub&quot;</span>),
    <span>userauth_type=</span><span>&quot;key&quot;</span>)</code></pre>
</div>
<p>The <code>list_vm_sizes()</code> method will show you what the <a rel="nofollow" href="https://docs.microsoft.com/en-us/azure/virtual-machines/windows/sizes" target="_blank">available VM sizes</a> are for your region.</p>
<div>
<pre><code><span># examine VM sizes available in australiaeast region</span>
sub$<span>list_vm_sizes</span>(<span>&quot;australiaeast&quot;</span>)</code></pre>
</div>
<div>
<h2>Retrieving an existing VM</h2>
<p>If you have an existing VM, you can retrieve it with the <code>get_vm()</code> method. As with <code>create_vm()</code>, this is available as part of the <code>az_subscription</code> and <code>az_resource_group</code> classes. The only argument you need to supply is the name of the VM.</p>
<div>
<pre><code>## using the subscription method
sub &lt;- az_rm$
    <span>new</span>(<span>tenant=</span><span>&quot;{tenant_id}&quot;</span>, <span>app=</span><span>&quot;{app_id}&quot;</span>, <span>password=</span><span>&quot;{password}&quot;</span>)$
    <span>get_subscription</span>(<span>&quot;{subscription_id}&quot;</span>)

<span># retrieve the VM we created above</span>
myNewVM &lt;- sub$<span>get_vm</span>(<span>&quot;myNewVM&quot;</span>)


## and with the resource group method
rg &lt;- sub$<span>get_resource_group</span>(<span>&quot;myresourcegroup&quot;</span>)

myOtherVM &lt;- rg$<span>get_vm</span>(<span>&quot;myOtherVM&quot;</span>)</code></pre>
</div>
</div>
<div>
<h2>Working with a VM</h2>
<p>There are various things you can do with a VM object within R.</p>
<p>To stop (shutdown) a VM, call its <code>stop()</code> method. The <code>deallocate</code> argument sets whether to deallocate its resources as well, with the default being TRUE; you may want to set this to FALSE if you know you will be restarting the VM soon. To restart it, call either the <code>start()</code> or <code>restart()</code> method. The main difference between the two is that <code>restart()</code> will shutdown the VM first if it is currently running.</p>
<p>To sync the object with the resource in Azure, call the <code>sync_vm_status()</code> method. The most common situation where you might want to do this is if you create a VM with the argument <code>wait=FALSE</code>. In this case, rather than waiting for provisioning to complete, the <code>create_vm</code> method will return an incomplete VM object; you then call its <code>sync_vm_status()</code> method to update it with how the provisioning is going in Azure.</p>
<p>To dynamically resize a VM, call the <code>resize()</code> method with the new size. This has an optional <code>deallocate</code> argument controlling whether to stop and deallocate the VM first (which is sometimes necessary for successful resizing).</p>
<p>To run a script in the VM (without manually logging in), call the <code>run_script()</code> method. This will be a PowerShell script if it is a Windows VM, or a bash script if it is Linux. The script is just a character vector.</p>
<div>
<pre><code><span># simple bash script for executing on a Linux VM</span>
script &lt;-
<span>'#!/bin/bash</span>

<span>var=Hello world!</span>

<span># redirect output to a file so we know whether it ran successfully</span>
<span>echo &quot;$var&quot; > /tmp/helloworld.txt</span>
<span>'</span>

vm$<span>run_script</span>(script)</code></pre>
</div>
<p>If you login to the VM after running this script, you should find the file <code>helloworld.txt</code> in the <code>/tmp</code> directory.</p>
</div>
<div>
<h2>Deleting a VM</h2>
<p>Simply deleting a virtual machine object in R, eg with <code>rm()</code>, will not do anything to the VM itself in Azure. To delete the VM and its resources, call the object’s <code>delete()</code> method:</p>
<div>
<pre><code>vm$<span>delete</span>()</code></pre>
</div>
<p>AzureVM will prompt you for confirmation that you really want to delete the VM. By default, this will also free up all the individual Azure resources used by the VM, such as its storage, network interface, security group, and so on.</p>
<p>If you created the VM using the <code>create_vm()</code> method of the <code>az_subscription</code> class, the deletion process is very simple: it simply removes the resource group containing the VM. This is possible because the resource group was created as part of deploying the VM. Be aware that any other resources you may have created in this resource group will also be deleted.</p>
<p>Alternatively, you can use the <code>delete_vm()</code> method of the <code>az_subscription</code> and <code>az_resource_group</code> classes. These will retrieve the VM of the given name and then call its <code>delete()</code> method.</p>
</div>
<div>
<h2>GPU enabled VMs</h2>
<div>
<p>If you are running deep learning workloads, you’ll want to ensure that your VM is GPU-enabled. In Azure, the various <a rel="nofollow" href="https://docs.microsoft.com/en-us/azure/virtual-machines/windows/sizes-gpu" target="_blank">NC- and ND-series VMs</a> are designed for these workloads. You can create a GPU-enabled VM by setting the <code>size</code> argument appropriately, for example</p>
<div>
<pre><code>myGpuVM &lt;- sub$<span>create_vm</span>(<span>size=</span><span>&quot;Standard_NC12s_v3&quot;</span>, ...)</code></pre>
</div>
<p>However, the following caveats apply to GPU-enabled VMs: – Not all regions have GPUs available. To check on availability, use the <code>az_subscription$list_vm_sizes()</code> method and provide your region. – Currently, the supply of GPUs is limited. You must <a rel="nofollow" href="https://docs.microsoft.com/en-us/azure/azure-supportability/resource-manager-core-quotas-request" target="_blank">apply for a quota increase</a> if you want to deploy a GPU-enabled VM.</p>
</div>
<div>
<h2>VM clusters</h2>
<p>You can work with VM clusters (a collection of VMs sharing the same configuration) by using the <code>get_vm_cluster</code>, <code>create_vm_cluster</code> and <code>delete_vm_cluster</code> methods. These are almost identical to <code>get_vm</code>, <code>create_vm</code> and <code>delete_vm</code> with the addition of a <code>clust_size</code> argument that sets the size of the cluster.</p>
<div>
<pre><code>sub &lt;- az_rm$
    <span>new</span>(<span>tenant=</span><span>&quot;{tenant_id}&quot;</span>, <span>app=</span><span>&quot;{app_id}&quot;</span>, <span>password=</span><span>&quot;{password}&quot;</span>)$
    <span>get_subscription</span>(<span>&quot;{subscription_id}&quot;</span>)

<span># create a cluster of 5 Ubuntu VMs</span>
vmCluster &lt;- sub$<span>create_vm_cluster</span>(<span>&quot;vmCluster&quot;</span>,
    <span>location=</span><span>&quot;australiaeast&quot;</span>,
    <span>os=</span><span>&quot;Ubuntu&quot;</span>,
    <span>username=</span><span>&quot;datascience&quot;</span>,
    <span>passkey=</span><span>readLines</span>(<span>&quot;~/id_rsa.pub&quot;</span>),
    <span>userauth_type=</span><span>&quot;key&quot;</span>,
    <span>clust_size=</span><span>5</span>)</code></pre>
</div>
<p>Most things that you can do with a single VM, you can also do with a VM cluster. For example, running a script with the <code>run_script()</code> method will run the script on all the VMs in the cluster. Starting, stopping and restarting a cluster similarly carries out the given action on all the VMs.</p>
</div>
</div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.revolutionanalytics.com/2018/12/azurevm.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/0rdBtNfGZTI" height="1"></a></div>