Create Azure resources using CLI

Microsoft offers several ways to manage your Azure cloud resources. One using the GUI, the Azure Portal, but when you want to use automation to create resources you need something more versatile, for example, the command line. In this blog post, I want to show how to create and manage your Azure resources using Azure CLI.


Azure CLI is an OS-agnostic tool. You can use this tool on Windows, macOS, and Linux based platforms. I will stick to the Windows platform. It is possible to install Azure CLI in different ways.

Download the MSI installer from the Microsoft website:

Or install it through Chocolatey:

choco install azure-cli -y

Run the command az –version to check if Azure CLI is correctly installed.


The syntax of Azure CLI is structured, which helps while using it. It is pretty straight forward and intuitive to use:

az [ group ] [ subgroup ] [ command ] { parameters }
  • Group – Associates with the service you want to create, this can for example be a Kubernetes service, event hub, or function app.
  • Subgroup – A subgroup of a group. (see az vm -h for the list of subgroups of the group vm)
  • Command – This is an instruction for the group/subgroup. A command can be a stop or start function or to list SKU sizes.
  • Parameters – Extra information that the command needs. For example a resource group name or location.

CLI in action

Let’s say we want to create a virtual machine (VM) from scratch running on CentOS in Azure. With from scratch, I mean that we also have to consider the prerequisites before creating the virtual machine like creating a network, set up a network security group, and so on…

First, you have to log in to your Azure environment using az login. This is an interactive command, which means that you will sign in to Azure via your browser. After logging in the default subscription will be selected, this will be the place where the resources will be created.

Before we start running commands we first have to create a resource group that will hold our VMs assets. This resource group can also be created using the Azure CLI. Run the following command to create a resource group:

az group create --name rg-blogpost --location westeurope

After running an az command it returns JSON with metadata of the created resource. The above command returned the following JSON:

Before we are going to create the virtual machine, we need to identify the necessary infrastructure to run a virtual machine in Azure. You will need:

  • A virtual network
  • A public IP address
  • A network security group
  • A network interface card

All these resources can be created using the Azure CLI with the following commands:

[string] $resourceGroupName = "rg-blogpost"
# Create a VNET
az network vnet create --name vnet-blogpost `
--subnet-name subnet-blogpost `
--resource-group $resourceGroupName
# Create a Public IP
az network public-ip create --name publicip-blogpost `
--resource-group $resourceGroupName
# Create a NSG
az network nsg create --name nsg-blogpost `
--resource-group $resourceGroupName
# Create a NIC
az network nic create --name nic-blogpost `
--vnet-name vnet-blogpost `
--subnet subnet-blogpost `
--network-security-group nsg-blogpost `
--public-ip-address publicip-blogpost `
--resource-group $resourceGroupName

As you can see with the network interface card some resources are being coupled. The last step is creating the actual VM and attach the network interface card so everything is being connected.

[string] $resourceGroupName = "rg-blogpost"
az vm create --name vm-euw-blogpost-webserver01 `
--size Standard_B2s `
--nics nic-blogpost `
--image CentOS `
--admin-username "blogadmin" `
--authentication-type "ssh" `
--generate-ssh-keys `
--resource-group $resourceGroupName
view raw Create-VM.ps1 hosted with ❤ by GitHub

Now we have created a Linux VM using CentOS. As you can see we have set the authentication-type to ‘ssh’. By default, this port is not open and we have to manage the firewall to open port 22.

[string] $virtualMachineName = "vm-euw-blogpost-webserver01";
[string] $resourceGroupName = "rg-blogpost"
az vm open-port --name $virtualMachineName
--port 22
--priority 150
--nsg-name nsg-blogpost
--resource-group $resourceGroupName

Now we have opened up port 22 we can use ssh to connect to our server. In a later blog post, I will cover how the firewall is setup. After running these commands we have setup a VM. In the portal it will look like this:

Clean up

Clean up the resource group using the delete command:

az group delete --name rg-blogpost --yes

Tips and tricks

While using Azure CLI I stumbled across two useful things.

Finding commands

The az find command is an AI-based search command. The AI searches through Azure documentation for more information about the supplied command.

az find '<group>'

Default values

When I am creating Azure resources I specify which location I want to use. In my case, this is always westeurope. Before I knew there was a way to set default values I always created a variable [string] $loc = ‘westeurope’. This still requires some (a lot) type work…

The Azure CLI has a built-in functionality to set default values:

az configure --defaults location=westeurope

Now every command will fill the location with the default value westeurope. The default values are saved in your ~\.azure\config file.


This is how you use the Azure CLI. I hope this blog helps you with exploring more on how to use Azure CLI. Microsoft has fully open-sourced this project, so if you want to explore the source code see the GitHub project.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s