Before installing the Azure Virtual Machines extension, we need to ensure that the Azure Account extension has been installed. This provides us with a single sign-in experience for all Azure extensions, including Azure Virtual Machines.
Now we can install the Azure Virtual Machines extension as well. You can simply search, find, and install it as shown below.
Once installed, an Azure icon will appear in the activity bar. This icon represents all Azure-related extensions, where you can manage a wide range of services. So when you install other Azure extensions, such as Azure Storage or Azure Functions, they also appear under this section. We can now see that Virtual Machines is added as a new service in VSCode.
It lists all VMs across all subscriptions over which you have permissions. Because the Azure Account extension is already installed, we no longer need to be authenticated for the Azure Virtual Machines extension to list VMs.
Create a new VM ^
To create a new VM, we can simply click on the plus sign (+) and follow the instructions in VSCode. It will then ask a set of questions to create the VM with the specified details.
Creating a new VM in VSCode
We need to specify the following to create the VM:
- The subscription in which the VM will be created.
- Name of the VM
- The passphrase that we will be using to connect to the VM via SSH
- The location where the VM will be created
After these simple steps, VSCode will start creating the VM with the info that we provided.
During the creation, we can check the logs to get a better view of what is being created. To check the logs, we need to select Azure Virtual Machines from the dropdown box in the upper right corner and then get to the "Output" section.
Once the provisioning is done, we can see the newly created VM under the subscription. We now have two options to manage this VM. We can either right-click it and select "Open in Portal" or manage it via SSH.
Connecting to VM in VSCode using Remote - SSH ^
I'd like to continue with the SSH option. So I need to install another extension, Remote - SSH. This will enable us to connect to our VM via SSH.
Once the Remote - SSH extension is installed, all we need to do is add our Linux VM as a new target. Here, I need to enter the following SSH command, which allows me to connect to an Azure VM using a default username, "azureuser," and the DNS name of the VM.
ssh firstname.lastname@example.org -A
The second step is the location of the SSH config file, where VSCode stores the details of the targets.
Once the target is added, we can right-click it and connect to it.
The last step is where we provide the passphrase that we created earlier, during provisioning.
Finally, the green checkmark appears on the target, which means that the VM has been connected and is ready to be used. The rest is just to manage the VM the way you like. In my scenario, I wanted to install PowerShell 7.
VSCode is growing more popular every day. Supporting a wide range of programming languages and services, its lightweight design and built-in integrations such as Git makes life much easier for both developers and system administrators. The Azure Virtual Machines extension in VSCode is yet another great service integration that allows us to deploy VMs on Azure with a few clicks.