Introduction
Deploying a Virtual Machine in Azure can be done using the Azure Portal or using the command-line such as Azure PowerShell and Azure CLI. After deploying a Virtual Machine, you will need to configure the VM to suit your needs. In a perfect world, we need to automate our most important tasks in order to save time.
With Azure VM Extensions, you can configure for example:
- Monitoring
- Security
- Configuration management
- Backup
- And more…
Azure Virtual Machine Extensions are small applications that provide post-deployment configuration and will simplify the configuration management of Virtual Machines. This feature uses Windows and Linux Azure agents that are automatically installed during the provisioning process. This agent is named ‘Microsoft Azure Virtual Machine Agent’ and it is the only prerequisite in order to use the Azure VM Extensions.
Listing Azure VM Extensions
As shown below, you can see the list from the Azure Portal by navigating to the Virtual Machine blade:
- Go to the Azure portal.
- Locate the Virtual Machine that you want to add Extension.
- Click Extensions in the Settings area
You can also use Windows PowerShell to list the Azure VM Extensions using the following command. The Get-AzureRmVMExtensionImage cmdlet will list the VM Extensions available in the West Europe region:
1 2 3 4 5 |
PS > Get-AzureRmVmImagePublisher -Location <Azure_Region> | ` >> Get-AzureRmVMExtensionImageType | ` >> Get-AzureRmVMExtensionImage | Select Type, Version |
At the time of writing this article, in the West Europe region, there are 879 VM extensions at your disposal.
Using Azure VM Extensions
First, you need to check the VM Agent status using the following command:
1 |
PS > Get-AzureRmVM -Name <VM_Name> -ResourceGroupName <RG_Name> | Select -ExpandProperty OSProfile | Select -ExpandProperty Windowsconfiguration | Select ProvisionVMAgent |
The command returns True, which indicates that the VM Agent is installed, and running.
Log in to the VM and open the Task Manager to check the WindowsAzureGuestAgent.exe process. It means that the VM Agent is installed:
To finish with the Azure Agent, it is located in the following directory: C:\WindowsAzure
Installing Azure VM Extensions
Using the Azure Portal, you can easily add an extension to an existing Virtual Machine. Navigate to an existing Virtual Machine, then select Extensions and click Add.
Select the Extension you want to install from the list of extensions. In my case, I selected the Custom Script Extension, which is a tool that can be used to launch and execute customization tasks. With this extension, you can run PowerShell scripts to configure the Virtual Machine.
Provide the configuration parameters as required. Here, you must provide the script file and arguments.
Click OK to start the installation process.
Custom scripts are great to perform specific tasks. There are just few things to take into consideration, such as:
- The script can be stored on GitHub, Azure Blob Storage, or anywhere the VM can access the repository.
- If your script is located on the Internet, then the firewall rules must be opened.
- If you run a bash script, for example, you must run the script on supported OS’s (e.g. Linux OS)
Now, if you deploy a new Virtual Machine, you will notice the following tab called “Guest config“. The VM Extension can be configured within the deployment wizard.
Once the VM Extension is installed, you can check the status in the Extensions blade:
In my case, I can see two VM Extensions:
- A Custom PowerShell Script
- The Site24x7 agent
It is possible to check if there are VM Extensions already installed on a Virtual Machine using the Get-AzureRmVM cmdlet:
1 |
PS > Get-AzureRmVM -Name <VM_Name> -ResourceGroupName <RG_Name> | Format-List * |
To install a new VM Extension, there are several PowerShell cmdlets at your disposal that you can list using the following command:
1 |
PS > Get-Command Set-AzureRM*Extension* |
Here is an example of custom script implementation with PowerShell:
1 2 3 4 5 6 7 8 9 10 11 |
PS > Set-AzureRmVMCustomScriptExtension -ResourceGroupName <RG_Name> ` -VMName <VM_Name> ` -Location <Location> ` -FileUri <Script_URL> ` -Run <Command_to_execute> ` -Name <Extension_Name> |
You can store a custom script in your Azure Blob storage.
Conclusion
Thanks to Azure Virtual Machine Extensions, you can very easily and quickly configure your machines. You can also automate tasks that you performed manually.
If the agent is not installed in the Virtual Machine, you can install it very easily. In this case, you can download the agent and double-click the Windows installer file.
Thanks for reading!