Azure Cross-Platform Automation
Step-by-Step: Create a Virtual Machine with Azure xPlat CLI
During this demonstration, we will explore the management of Microsoft Azure Resource Manager (ARM) Resource Groups, using the Microsoft Azure Cross-Platform (xPlat) CLI Tool. Using Azure xPlat CLI, you can create and delete Resource Groups, and perform deployments of ARM JSON Templates into Resource Groups.
Hello. In this demonstration, we'll be taking a look at how to use the Azure cross-platform CLI tool to manage our Azure resource manager resource groups.
So the first command we're going to call is "Azure account list" to ensure that we are operating on the appropriate subscription. As you can see, I have two subscriptions here. One of them is in the disabled state, so I can't use that one, and one of them is in the enabled state. And you can see that the currently selected subscription context is the account that's enabled. So I know that I'm running the correct account. However, I need to make sure that I'm also in the Azure resource manager mode.
So Azure has two different APIs, and they are called Azure service management, or ASM, and Azure resource manager. Pretty much all the operations you perform going forward will be in the Azure resource manager API. So let's call the "Azure config" command, and then we'll tack on the "mode" command. And if we take a look at the help for that command, you'll see that we have the Azure resource manager and ASM modes. And at the bottom of the command here, it actually tells us that our current mode is ARM, so we're already in the correct mode. If I did want to change the mode to ASM, I would simply call "Azure config mode ASM," or "Azure config mode ARM," to switch back and forth between those two different modes.
Because we're talking about resource groups in this particular demonstration, resource groups are a concept that are specific to Azure resource manager. The Azure service management API does not offer any concept such as a resource group. So let's go ahead and just run the Azure command to get a view of all the top-level commands that are available to me.
As you can see, we have a sub-command for Azure Xplat CLI called group. So let's go ahead and just call "Azure group," and you'll see a list of all the resource group related commands. We can do things such as creating a resource group, deleting a resource group, updating a resource group to have tags, which are custom pieces of metadata that you can apply to resource groups and individual resources. We can also list out all of the resource groups in our subscription. We can also use the "show" command for a specific resource group to show more detail about that resource group. We also have the ability to view the logs of a resource group by using the "Azure group log show" command.
So inside of a resource group, when we deploy resources using a declarative Azure resource manager JSON template, we would use the "Azure group deployment" command. So the "Azure group deployment command" will allow us to create a new declarative deployment, list the declarative deployments in a resource group. We can stop or delete a deployment into a resource group that's in progress, and we can also grab a list of operations that we can perform in that resource group.
We also have some commands to interact with our Azure resource manager template gallery, such as listing ARM JSON templates, showing the details of a specific template, downloading a template, or validating our own template against a resource group.
So let's start by going ahead and just creating a resource group. We'll call the "Azure group create" command, and then we'll specify the "--help" parameter to look at all of the different parameters that we can specify to the Azure group create command. As you can see, we can specify the resource group name, we can specify the Azure region or location that we want to deploy the resource group to, we can optionally specify a deployment name for a declarative deployment of an ARM JSON template. For ARM JSON template deployments, we can actually specify a template file that we want to deploy into that resource group as we create it. We can also specify a template file using a URI instead of a local file on the file system.
We also have the ability to specify input parameters to our declarative JSON templates by using the parameters parameter, or the parameters file parameter. So you can either pass in a JSON string into the parameters parameter, and you can pass in a JSON file into the parameters file. So we'll take a look at how to do that when we actually deploy a resource manager template.
Then finally, we have the tags parameter that allows us to specify key value pairs that we want to apply to the resource group when it's created. And then finally, we also have a subscription parameter so we can override the current subscription we're operating on and be more explicit about which subscription the operation to create a group should be performed on.
So let's go ahead and just create an empty resource group inside of our subscription. So we're going to call the "Azure group create" command. We're going to specify the two essential parameters, which are the name of the resource group, so we'll calln it "Cloud Academy RG," and also the location. So the location, we'll just use West US for the time being. So this command is going to go ahead and create a new empty resource group for us, and we've only specified the essential parameters, which are name and location. So now we can do "Azure group list," and you'll see a list of resource groups in our current subscription. As you can see, the Cloud Academy RG resource group was just created and is returned in that list. We can also do "Azure group list --json" to get the JSON output for all of our resource groups.
Now, if we want more details on a specific resource group, we can use the "Azure group show" command, and we'll specify the name of our resource group that we want to show the details for, but first we'll use the "--help" parameter to ensure that we look at all of the child parameters for the "Azure group show" command. I'm going to go ahead and cancel this "Azure group list" command as it's taking a little bit too long to run. So I'm going to call "Azure group show help," and you'll see that we can specify the name parameter for the group that we want to get more details for.
So "Azure group show" for the Cloud Academy RG resource group will give us a full list of details for that resource group. Now as you can see, when we created the resource group, we did not add any tags to it. So we can call "Azure group set" and look at the help for that command, and you'll see that we can either call no tags, which will delete any existing tags on that group, or we can add tags by using the "--tags."
So let's go ahead and call "Azure group set name Cloud Academy RG," and then we'll specify the tags parameter, and we'll specify a string for the tags, and we'll create a tag with the name company and set it to Cloud Academy. Now, we can also use a semicolon to delimit these tags, and so I'm going to create a second tag called department=marketing. So let's go ahead and hit enter to run that command. And that's going to create the tags on the resource group that we just created. So now, as you can see, the tags are company=Cloud Academy and department=marketing.
So let's do an "Azure group show name Cloud Academy RG," and use JSON output for it. So this is going to give us the full details. And as you can see, we've got the tags for that resource group broken out into key and value. Key and value. So that's how we create a resource group and configure the tags on it.
So now let's take a look at how to create a resource group deployment. So I'm going to go ahead and switch over to Google Chrome, and I'm going to go out to the Azure QuickStart templates page. So the Azure QuickSTart templates GitHub project allows us to view a bunch of pre-created templates that Microsoft has created for us to deploy with Resource Manager. So let's say, for example, that we wanted to deploy a Windows virtual machine. We could simply search the page for VM, and once we get far enough down, there's actually a template that allows us to deploy a simple Windows VM.
So what I'm going to do is grab the template URI here, which is a full path to the JSON file itself, and I'm going to go ahead and specify that in my script. I'm actually going to create a bash variable for that called "template URI." So now I've created a variable to store that value so I don't have to corrupt my command line with extremely long values.
So let's actually deploy the template. So we're going to call Azure group, and if you remember from earlier, the command is deployment. So we're going to call "Azure group deployment create," and look at the help for that. So the first parameter we need to specify is the resource group name. And the resource group name is Cloud Academy RG. Let me just fix that capitalization error there. So I've got Cloud Academy RG as the resource group name. I can optionally name the deployment. So we'll call this "Cloud Academy deployment." And then we can specify the template URI. So the template URI is the variable that we created. And that's going to be called "templateuri," all in lowercase.
Then we finally need to specify the parameters that we're going to pass into this deployment. So we're going to specify "--parameters," and then we're going to have to pass in a JSON formatted string of all the parameters for this deployment. So I'm going to go ahead and start the JSON object on the outside with braces, and I'm going to go ahead and specify all the parameters that we need inside this parameters object. So I'm going to specify the new storage account name, and the value will be "Cloud Academy storage." And then I'm going to terminate that object and create another object called admin username. And the admin username will be value of Cloud Academy. So that's the username that we're going to actually sign into our virtual machine with. Then I'm going to create another parameter called admin password, and set that to password, like so. And then I'm going to add the DNS name for public IP parameter. And this is the last one that we have to specify, because the Windows OS version parameter actually has a default value, so we don't have to specify that parameter if we don't want to. So the DNS name for public IP will have a value of Cloud Academy RG. Let's just add a 2 on there.
Okay, so now we've specified our input parameters, and as you can see, I've actually got an error there saying that it's got an extra curly brace. Okay, so I was actually missing a colon right after DNS name for public IP address here. So basically, I've specified all my parameters now, so I'm going to go ahead and hit enter, and you'll see that the Azure resource manager is now creating a new deployment inside of that resource group with the name Cloud Academy deployment. So it's going to retrieve the Azure resource manager JSON template from the template URI that we specified. It's going to pass in the input parameters that we specified at the command line, which you could also specify with a parameters JSON file, optionally. And now it's going to actually wait for that deployment to complete. So in a few minutes here, we'll go ahead and take a look at the results of that deployment.
But in the meantime, I would like to pull up the Azure portal. So let's go out to portal.azure.com, and we'll go ahead and sign in with our account here. And as soon as you enter the Azure portal, you can go over on the left-hand side and open up resource groups. So in the resource groups, we're going to actually filter for a list of resource groups that match the name cloud. So the name of the resource group that we just created was Cloud Academy RG, so we should also see a resource group here called Cloud Academy RG. If we select that resource group, you'll see that we have the last deployment, which is still in process. You'll see that we actually did get that named deployment from our command line, which was right here. So we created the resource group, and then the deployment name is Cloud Academy Deployment. And so if we click on that deployment, we can actually see a better breakdown of all the resources that are being provisioned into it.
So we've got the virtual machine, called My Windows VM, that's currently in progress, but all the other resources that have been declared in that file, such as my VM nick, my public IP, my storage account, and my virtual network have all been provisioned already. So after that virtual machine has finished provisioning, we'll be able to remotely access it with RDP.
So instead of waiting for this resource group to finish its deployment, we're going to go ahead and just go ahead and delete the resource group. So we're going to call "Azure group delete." And then if we look at the help for this command, you'll see that we can specify the name parameter. And the name parameter is simply the name of the resource group that we want to delete, which in this case is called Cloud Academy RG. I can also add the "--quiet" parameter, which will cause the command to not prompt for user input or confirmation that we actually do want to delete that resource group.
One other feature I would like to quickly demonstrate using resource groups is the ability to filter your subscription for resource groups with a particular tag. So I'm going to go ahead and call "Azure group create name Cloud Academy RG location West US," and then I'm also going to add the tags. And in this case, I'm going to call it "company=Cloud Academy." So that's the tag we're going to associate with this. I can also create another tag just called "marketing," which is empty, because it doesn't have a value. So now that I've created that resource group, I can use the "Azure group list" command, and as you can see, the group list command has a tags parameter. So I can call "Azure group list --tags," and then I can specify just the tag name. So in this case, I have a tag called marketing with no value. But I can also specify a specific value. So I can say show me all the resource groups where the company=Cloud Academy. And we should get back the same results, because this particular resource group matches both of those tags.
So in this session, we've reviewed how to create a resource group and how to deploy a resource group and tag a resource group using the Azure cross-platform CLI tool.
Trevor Sullivan is a Microsoft MVP for Windows PowerShell, and enjoys working with cloud and automation technologies. As a strong, vocal veteran of the Microsoft-centric IT field since 2004, Trevor has developed open source projects, provided significant amounts of product feedback, authored a large variety of training resources, and presented at IT functions including worldwide user groups and conferences.