Microsoft Azure can be accessed using Azure Portal url. The Azure portal provides rich and user friendly UI to access Azure Products / Services. You can easily search the menu options / resources you want to manage.
I am sure most us would have used Azure portal by now, for many different purposes like creating virtual machines, creating CDN, Redis cache instance, or creating SQL Database or Web App deployment to Azure, etc.
The obvious question which may come to anyone’s mind is:
Why use commands – if Portal is available ?
When you are working on enterprise application, many Azure resources would be required to be created. The Azure resources can be created through Azure Portal.
But every time we setup new environment for our application or try to replicate same resources in another Azure region, this becomes very time consuming job. Plus, there can be chances of human error.
The main purpose of command line tools is to provide
- It’s very quick to run the scripts than manually creating those resources
- Scripts are most efficient way if the same resources have to be created multiple times.
Hmm! Which tools are available ?
There are two types of command lines available for accessing Azure resources:
- Azure PowerShell – the Az module
- Azure CLI
Both of these platforms can be used on Windows, Linux and MacOS.
The Azure PowerShell module “AzureRM” was initially being used for accessing / managing Azure resources. These two modules have been officially replaced with the Az module.
If you already have these modules installed, remove them by using “Uninstall-AzureRM” PS command.
Souce: MSDN Blog and GitHub Announcement
So, how to setup these tools on my machine ?
All these command line interfaces are available on Windows, Linux and MacOS. In this article, we will see how to use these command line interfaces from Visual Studio Code.
Install Visual Studio Code
Download Visual Studio Code from this link and install it.
Once VS is installed, Click on “Terminal” menu and Select “New Terminal”. On Windows machine this would start a PowerShell terminal.
For Linux and MacOS, you will need to install PowerShell Core and you can find more information about installing PowerShell core on MSDN
PoweShell: AZ Module
Please make sure that you have only one active PowerShell session while performing below action. Please note that you have uninstalled AzureRM from the machine.
Then install the new AZ module using below command. Please ensure that you have administrative privilege while running this command. On Linx and MacOS, you would need Superuser privileges.
Install-Module -Name Az -AllowClobber
If you don’t have administrative / superuser privilege you can add scope parameter and specify to install the Az only for current user with below command.
Install-Module -Name Az -AllowClobber -Scope CurrentUser
By default, the PowerShell Gallery is not trusted repository for PowerShellGet. Hence you may receive warning to whether you want to continue this operation. You will have to select “Yes” or “Yes to All” option to continue the installation.
Then you can try logging in the Azure using below command
Connect-AzAccount
This would show you login popup and once login is done it would show the PowerShell prompt. If you have more than one subscription, you can select any one of them to continue your work.
Azure CLI
For windows, you can install CLI installer from this link. It would install the CLI commands for windows. Then these CLI commands can be used from PowerShell prompt (the Terminal window from VS Code) OR from the command prompt.
For installing MacOS, you can use below command on BASH prompt to install Azure CLI.
brew update && brew install azure-cli
For Linux installation, I would suggest to refer the MSDN documentation which explains the commands. The commands vary based on Linux flavor you have.
Once this is installed, you can use below command from Bash / PowerShell prompt to login using the CLI
Az Login
This would show you the login page for Azure in the browser. Once you login and select subscription, you can continue working on Bash terminal using the CLI to manage your Azure resources.
I hope this article provides you sufficient information on how you can install and get started on using command line tools with Azure. Do not forget to comment and let me know your thoughts.