<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/azurermr-an-r-interface-to-azure-resource-manager/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.revolutionanalytics.com/2018/11/azurermr-azure-resource-manager.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>In a previous article I <a rel="nofollow" href="https://blog.revolutionanalytics.com/2018/11/azurer-intro.html" target="_blank">announced AzureR</a>, a new family of packages for working with Azure from R. This article goes into more detail on how you can use AzureRMR, the base package of the AzureR family, to manage resources with <a rel="nofollow" href="https://docs.microsoft.com/azure/azure-resource-manager/resource-group-overview?WT.mc_id=Revolutions-blog-davidsmi" target="_blank">Azure Resource Manager</a>.</p>
<div>
<h2>Before you begin</h2>
<p>The first thing you have to do is create a <a rel="nofollow" href="https://docs.microsoft.com/azure/active-directory/develop/app-objects-and-service-principals?WT.mc_id=Revolutions-blog-davidsmi#service-principal-object" target="_blank">service principal</a>. This is a security identity used by apps, services and automation tools for programmatic access to Azure resources. You can think of it as a “user identity” (login and password or certificate) with a role and permissions to access your resources. Creating a service principal is a one-time task, after which you can reuse it for all your AzureRMR sessions.</p>
<p>There are various ways to create a service principal. The <em>easiest</em> is to have your network administrator create it for you (which is probably also the best option in a corporate environment), but if you’re doing it yourself, you can do it via the Azure cloud shell.</p>
</div>
<ul>
<li>In the Azure Portal (<a rel="nofollow" href="https://portal.azure.com/" target="_blank">https://portal.azure.com/</a>), click on the Cloud Shell icon:</li>
</ul>
<div><a rel="nofollow" href="https://revolution-computing.typepad.com/.a/6a010534b1db25970b022ad3a13986200d-pi" target="_blank"><img border="0" alt="Cloudportal2" title="Cloudportal2" src="https://revolution-computing.typepad.com/.a/6a010534b1db25970b022ad3a13986200d-800wi"></a></div>
<ul>
<li>If you haven’t used the shell before, there will be a dialog box to choose whether to use bash or PowerShell. Choose bash.</li>
<li>In the shell, type
<pre>az ad sp create-for-rbac --name {app-name} --subscription &quot;{your-subscription-name}&quot; --years {N}
</pre>
<p>substituting the desired name of your service principal (try to make it memorable to you, and unlikely to clash with other names), your subscription name, and the number of years you want the password to be valid.
<li>Wait until the app creation is complete. You should see a screen like this.</li>

<div><a rel="nofollow" href="https://revolution-computing.typepad.com/.a/6a010534b1db25970b022ad37b12b7200c-pi" target="_blank"><img border="0" alt="Cloudshell_sm" title="Cloudshell_sm" src="https://revolution-computing.typepad.com/.a/6a010534b1db25970b022ad37b12b7200c-800wi"></a></div>
<ul>
<li>Record your tenant ID, app ID, and password.</li>
</ul>
<p>If you want to allow access at something other than subscription level, you can use the <code>--scopes</code> argument in place of <code>--subscription</code>. For example, to restrict AzureRMR to only the “AnalyticsRG” resource group:</p>
<pre>az ad sp create-for-rbac --scopes /subscriptions/{your-subscription-ID}/resourceGroups/AnalyticsRG
</pre>
<p>This will grant &quot;Contributor&quot; access to the specified subscription/resources, which should be sufficient for anything you’d want to do in R.</p>
<div>
<h2>Using AzureRMR</h2>
<p>AzureRMR is a general-purpose interface to Azure Resource Manager (ARM). You can use it to work with nearly any Azure object that ARM can handle: subscriptions, resource groups, resources, templates and so on. The things you can do with AzureRMR include:</p>
<ul>
<li>Create a new resource</li>
<li>Carry out arbitrary operations on a resource</li>
<li>Delete a resource</li>
<li>Deploy a template</li>
<li>Delete a template, and, optionally, any resources that it created</li>
<li>Create and delete resource groups (if you gave your service principal subscription-level access)</li>
</ul>
<p>Here is some example code to show AzureRMR in action. Let’s retrieve a resource (in this case, a storage account):</p>
<div>
<pre><code><span>library</span>(AzureRMR)

az &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>)

sub &lt;- az$<span>get_subscription</span>(<span>&quot;{subscription_id}&quot;</span>)
rg &lt;- sub$<span>get_resource_group</span>(<span>&quot;rgName&quot;</span>)
stor &lt;- rg$<span>get_resource</span>(<span>type=</span><span>&quot;Microsoft.Storage/storageAccounts&quot;</span>,
    <span>name=</span><span>&quot;mystorage&quot;</span>)</code></pre>
</div>
<p>Step by step:</p>
<ol>
<li>
<p>The first thing we do is login to Azure via <code>az_rm$new()</code>. This authenticates with Azure Resource Manager and stores a token which will be used for further interactions with ARM. AzureRMR will automatically handle such issues as token renewal on expiry.</p>
</li>
<li>
<p>Given the session object, retrieve an Azure subscription with the <code>get_subscription()</code> method and store its details in a subscription object. Notice that AzureRMR uses R6 classes, so the method is part of the object itself.</p>
</li>
<li>
<p>Given a subscription object, retrieve the details for a resource group with the <code>get_resource_group()</code> method.</p>
</li>
<li>
<p>Given a resource group object, retrieve the details for a resource and store them into a resource object. There are many ways of identifying a resource; here, we use the type and the name.</p>
</li>
</ol>
<p>This replicates the hierarchical structure of Azure, whereby subscriptions contain resource groups, which contain individual resources.</p>
<p>Once we have a resource, we can do <em>things</em> with it, via the <code>do_operation()</code> method. In this case, we have a storage account. One of the things we can do with a storage account is retrieve its access keys.</p>
<div>
<pre><code>stor$<span>do_operation</span>(<span>&quot;listKeys&quot;</span>, <span>http_verb=</span><span>&quot;POST&quot;</span>)</code></pre>
</div>
<p>Here’s another example. If we have a virtual machine, we can start it, execute shell commands, and then shut it down again:</p>
<div>
<pre><code>vm &lt;- rg$<span>get_resource</span>(<span>type=</span><span>&quot;Microsoft.Compute/virtualMachines&quot;</span>,
    <span>name=</span><span>&quot;myVirtualMachine&quot;</span>)

vm$<span>do_operation</span>(<span>&quot;start&quot;</span>, <span>http_verb=</span><span>&quot;POST&quot;</span>) <span># may take a while</span>
vm$<span>do_operation</span>(<span>&quot;runCommand&quot;</span>,
    <span>body=</span><span>list</span>(
        <span>commandId=</span><span>&quot;RunShellScript&quot;</span>, <span># RunPowerShellScript for Windows</span>
        <span>script=</span><span>as.list</span>(<span>&quot;ifconfig > /tmp/ifconfig.out&quot;</span>)
    ),
    <span>encode=</span><span>&quot;json&quot;</span>,
    <span>http_verb=</span><span>&quot;POST&quot;</span>)
vm$<span>do_operation</span>(<span>&quot;powerOff&quot;</span>, <span>http_verb=</span><span>&quot;POST&quot;</span>)</code></pre>
</div>
<p>For the types of operations you can carry out on a resource, consult the <a rel="nofollow" href="https://docs.microsoft.com/en-us/rest/api/?view=Azure" target="_blank">Azure REST API documentation</a>.</p>
<p>You can also interrogate the fields of a resource object; in particular the <code>properties</code> field can contain arbitrary information about an Azure resource. For example, a storage account’s properties includes the endpoint URIs, and a virtual machine’s properties includes its admin login details.</p>
<div>
<pre><code><span># file and blob storage endpoint</span>
stor$properties$primaryEndpoints$file
stor$properties$primaryEndpoints$blob

<span># OS profile for a VM: includes login details</span>
vm$properties$osProfile</code></pre>
</div>
<p>To create a new resource, we can use the resource group object’s <code>create_resource</code> method. The specific arguments to this method depend on the resource being created; again, see the Azure documentation for more details. Let’s create a new storage account:</p>
<div>
<pre><code>stor2 &lt;- rg$<span>create_resource</span>(<span>type=</span><span>&quot;Microsoft.Storage/storageAccounts&quot;</span>,
    <span>name=</span><span>&quot;mystorage2&quot;</span>,
    <span>kind=</span><span>&quot;StorageV2&quot;</span>,
    <span>sku=</span><span>list</span>(<span>name=</span><span>&quot;Standard_LRS&quot;</span>))</code></pre>
</div>
<p>And to delete a resource, we can either use the resource object’s <code>delete</code> method, or the resource group object’s <code>delete_resource</code> method. AzureRMR will prompt you for confirmation that you really want to carry out the deletion.</p>
<div>
<pre><code>stor2$<span>delete</span>()</code></pre>
</div>
<p>In a manner similar to resources, deploying a <a rel="nofollow" href="https://docs.microsoft.com/azure/azure-resource-manager/resource-group-overview?WT.mc_id=Revolutions-blog-davidsmi#template-deployment" target="_blank">template</a> is just a matter of calling the resource group object’s <code>deploy_template</code> method. This takes two arguments, <code>template</code> and <code>parameters</code>. Both arguments should be in JSON format: either the name of a JSON file, a character vector containing the JSON data, or a list containing the parsed JSON (via <code>jsonlite::toJSON</code>).</p>
<div>
<pre><code>vm_tpl &lt;- rg$<span>deploy_template</span>(<span>&quot;myNewVirtualMachine&quot;</span>,
    <span>template=</span><span>&quot;vm_template.json&quot;</span>,
    <span>parameters=</span><span>list</span>(
        <span>os=</span><span>&quot;Windows&quot;</span>,
        <span>size=</span><span>&quot;Standard_DS2_v2&quot;</span>,
        <span>username=</span><span>&quot;ruser&quot;</span>,
        <span>password=</span><span>&quot;MyPassword123!&quot;</span> <span># use a strong password!</span>
    ))</code></pre>
</div>
<p>Normally, deleting a template doesn’t touch the resources it creates: it only deletes the template itself. However, AzureRMR optionally allows you to free any resources created when you delete a template. This is useful when managing complex objects like VMs, which actually consist of multiple individual resources in Azure (storage account, disk, network interface, etc). When you are done with the VM, deleting the template lets you free all these resources with a single command.</p>
<div>
<pre><code>vm_tpl$<span>delete</span>(<span>free_resources=</span><span>TRUE</span>)</code></pre>
</div>
</div>
<div>
<h2>Extending</h2>
<p>AzureRMR is meant to be a generic mechanism for working with Resource Manager. You can extend it to provide support for service-specific features; packages that do this include <a rel="nofollow" href="https://github.com/cloudyr/AzureVM" target="_blank">AzureVM</a> for <a rel="nofollow" href="https://azure.microsoft.com/en-us/services/virtual-machines/" target="_blank">virtual machines</a>, and <a rel="nofollow" href="https://github.com/cloudyr/AzureStor" target="_blank">AzureStor</a> for <a rel="nofollow" href="https://azure.microsoft.com/en-us/services/storage/" target="_blank">storage accounts</a>. For example, instead of using a generic <code>do_operation()</code> call to retrieve the access keys for a storage account, AzureStor provides an <code>az_storage</code> class that has a <code>list_keys()</code> method. Similarly, the AzureVM package provides an <code>az_vm_template</code> class that wraps the VM deployment template, and has methods for interacting with the VM.</p>
<p>Extending AzureRMR also gives you the opportunity to provide a <em>client</em> interface to an Azure service. AzureRMR only acts as an interface to Resource Manager; it doesn’t provide facilities to, for example, work with the data that is stored in a storage account. This is provided by AzureStor, which has a client interface to file and blob storage. With this you can upload and download files, list files and containers, create and delete containers, and so on.</p>
<p>For more information, see the “Extending AzureRMR” vignette that is part of <a rel="nofollow" href="https://github.com/cloudyr/AzureRMR" target="_blank">AzureRMR</a>.</p>
</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/11/azurermr-azure-resource-manager.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/8H3MYM05YkY" height="1"></a></div></ul></div>