Six ways to manage your Azure resources
One of the great things about Azure is that there is an incredible amount of choice about how you go about managing your resources. Whether you want to create a new web app, configure a network security group, or stop a virtual machine, there’s plenty of choices for how to accomplish that.
Here’s a quick summary of what your options are, and why you might choose them:
1. Use the Portal
The first option is actually two, because there’s a “new” portal (portal.azure.com) and the “classic portal” (manage.windowsazure.com). Thankfully, there’s very little reason to visit the classic portal anymore – almost everything can be done in the new portal.
The Azure portal is a great choice when you’re just wanting to explore. You can look around the resources you have deployed, navigate into their various property pages, and quite often you’ll discover settings to configure features you didn’t even know were there. Here’s just a few of the things you can set up for a web-app for example.
The portal’s also great for things like browsing the list of available images for Virtual machines.
But! While the portal is great for experimenting and exploring, it’s not the best choice for deploying of application, or any task you need to implement repeatedly. For those sort of things, it’s much better to use a technique that can be automated.
And that’s where the other options come in.
2. Use Azure PowerShell
Azure PowerShell is a comprehensive collection of PowerShell cmdlets that let you do pretty much anything with Azure. The commands uses the conventional PowerShell prefixes of New-
, Get-
, Set-
and Remove-
, for create, read, update and delete operations.
For example, here’s a sample Azure PowerShell script that creates a web app and deploys code from GitHub
$gitrepo="https://github.com/Azure-Samples/app-service-web-dotnet-get-started.git"
$webappname="mywebapp$(Get-Random)"
$location="West Europe"
# Create a resource group.
New-AzureRmResourceGroup -Name myResourceGroup -Location $location
# Create an App Service plan in Free tier.
New-AzureRmAppServicePlan -Name $webappname -Location $location -ResourceGroupName myResourceGroup -Tier Free
# Create a web app.
New-AzureRmWebApp -Name $webappname -Location $location -AppServicePlan $webappname -ResourceGroupName myResourceGroup
# Configure GitHub deployment from your GitHub repo and deploy once.
$PropertiesObject = @{
repoUrl = "$gitrepo";
branch = "master";
isManualIntegration = "true";
}
Set-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName myResourceGroup -ResourceType Microsoft.Web/sites/sourcecontrols -ResourceName $webappname/web -ApiVersion 2015-08-01 -Force
All the major Azure services have PowerShell cmdlets available, so pretty much anything you can do in the portal can be automated with it. For more examples of Azure PowerShell in action, check out Elton Stoneman’s Pluralsight course Managing IaaS with PowerShell.
Of course, not all developers know PowerShell and it does have its quirks, but if you already know some PowerShell or are willing to learn, this is a great way to automate your resource management. And its not necessarily a Windows only thing now – Azure PowerShell is available on Mac and Linux (currently in beta).
3. Use the Azure CLI
Alternatively, if you’re a Max or Linux user, or just prefer a bash shell to PowerShell, the Azure CLI 2.0 might be for you. It’s a fully cross-platform command-line experience, and despite being newer, already has capabilities on a par with Azure PowerShell.
The commands typically take the form az command subcommand options
which makes the API very easy to explore. Just type az
and get a list of the main commands. Type az webapp
and see what you can do with webapps. There’s even an interactive mode which gives you a sort of intellisense experience at the command prompt!
By default the commands will emit JSON, although it supports a few output formats. To give a feel for how it compares to PowerShell, here’s an Azure CLI bash script to perform the same task of creating a web app and deploying code from GitHub:
#!/bin/bash
# Replace the following URL with a public GitHub repo URL
gitrepo=https://github.com/Azure-Samples/php-docs-hello-world
webappname=mywebapp$RANDOM
# Create a resource group.
az group create --location westeurope --name myResourceGroup
# Create an App Service plan in `FREE` tier.
az appservice plan create --name $webappname --resource-group myResourceGroup --sku FREE
# Create a web app.
az webapp create --name $webappname --resource-group myResourceGroup --plan $webappname
# Deploy code from a public GitHub repository.
az webapp deployment source config --name $webappname --resource-group myResourceGroup \
--repo-url $gitrepo --branch master --manual-integration
Obviously the capabilities and use cases for the Azure CLI and Azure PowerShell are very similar so its a hard call which one to use. Azure CLI would probably be a better choice if you prefer bash to PowerShell, if you’re working cross-platform, and also has the benefit of being open source, so it is picking up plenty of community contributed features.
4. Use the Azure Management Libraries for .NET
The Azure management libraries for .NET allow you to write code in C# (or your favourite .NET language) to manage your Azure resources. This is a great choice if you’re more comfortable with C# than scripting language and there is a lot of business logic surrounding the task you want to automate.
The libraries are available on NuGet and support a fluent interface allowing you to write code looking like this:
var sql = azure.SqlServers.Define(sqlServerName)
.WithRegion(Region.USEast)
.WithNewResourceGroup(rgName)
.WithAdministratorLogin(administratorLogin)
.WithAdministratorPassword(administratorPassword)
.Create();
As you can see, it’s very easy to work with. The down-sides are that it doesn’t have the same comprehensive coverage of Azure features and services that the CLI and PowerShell have, although hopefully that will change in the future (they’re open source too).
You will also need to go through the somewhat convoluted process of creating a service principal (instructions here) to create credentials your code can log in with.
But if you wanted to create say an Azure Function that ran on a timer to start or stop virtual machines, this would be a great choice.
5. … or for your favourite language
If you’re a Java programmer, there’s an Azure Java API. There’s also a node.js Azure SDK, a set of Azure Python libraries, and an Azure client library for Ruby. Like with the .NET libraries, you’ll want to create a service principal to use for login purposes, but then you’ll be able to manage Azure resources using your language of choice.
6. Use the Azure REST API directly
So far we’ve discussed five ways to manage your Azure resources – the portal, PowerShell, the CLI and the SDKs for a whole host of popular programming languages. But what they all have in common is that under the hood, they’re making calls to the Azure REST API.
What this means is that you can access the full range of capabilities of Azure, using any language that can make HTTP requests, by calling the REST API directly yourself. Obviously its a little less user-friendly than the other options we’ve discussed so far, but the documentation is quite comprehensive so you should be able to work out the correct headers and payload for each request.
Here’s a sample request to the REST API:
PUT /subscriptions/03f09293-ce69-483a-a092-d06ea46dfb8c/resourcegroups/ExampleResourceGroup?api-version=2016-02-01 HTTP/1.1
Authorization: Bearer <bearer-token>
Content-Length: 29
Content-Type: application/json
Host: management.azure.com
{
"location": "West US"
}
Bonus 7 - Use ARM Templates
Finally, this isn’t really an alternative to the previous options, but should be used in conjunction with them.
For example, creating a new virtual machine often involves creating several resources – a virtual network, a public IP address, a network security group, a storage account for the disk, and the virtual machine itself. Whilst you could write a script with a separate command for each of these items, a better approach is to create a single Azure Resource Manager template, which can be deployed with a single command.
There are several benefits here – the template can be parameterized and Azure can create resources in parallel for faster deployment. Deploying a template will also only create resources that are missing in the target resource group so can greatly simplify incremental deployments.
To get an idea for the sort of thing you can achieve with an ARM template, check out these samples – there are some very impressive demos, which you can try yourself easily simply by clicking the “deploy to Azure” button for each sample, and within minutes you have the sample deployed. If you’re worried about cost – cleaning up is really easy – just delete the resource group and everything that you created is gone. And since pricing in Azure is per minute, you’ll often end up paying hardly anything for a quick experimental deployment of a template.
Each of the six previous techniques we’ve discussed offer you a way to deploy using an ARM template, so whichever of those you use, once you’ve finalised what resources make up a deployment, it’s well worth your time creating a template to make deployments simple and repeatable.