Create Linux Virtual Machine using ARM Templates (IaC) in Azure
There are various ways to create virtual machine in azure ,
Few of them are very well known as shown below:
1 Azure Portal
2 Azure PowerShell
3 Azure CLI
4 Create a json template and deploy json
template through power shell
5 Create and deploy virtual machine through visual
studio console.
In this article I will explain how
to create it with Infrastructure as a code or using azure ARM templates.
Practical Scenario : There can be
a practical use case while you work in a certain project and gets a requirement
to create a virtual machine from an existing infrastructure. There can be
various ways though the quickest one is through Azure templates.
Azure templates can be downloaded
from the resource already hosted. Now
What ARM templates perform : An
ARM templates keeps an entire contents of resource group or it can contain more
than one resource. While deployment it can be “Complete or Incremental”.
Whenever you perform any operation
through Azure ARM portal, Azure PowerShell ,Azure CLI or Rest API’s the Azure
ARM API handles your individual request, because each request handled by the
same API.
All the capabilities exists in
Azure Resource Manager Portal are easily available through PowerShell, CLI and
client SDK as well as RestAPI’s.
You can refer an image below to
understand how does all tools interact with ARM API. Refer arm-image
The API transmit request to Azure
resource manager service ,which further validates the requests and it further
routes the requests to appropriate service.
Now login into
https://portal.azure.com
and search for existing resource if exists (e.g. Virtual Machine).Open that in
portal as shown below in image
virtual-machine-linux
Search templates , it should
show export templates in that. Click on that.Kindly refer an image export-template.
Once you click on the export template option shown in last
step , it shows you the following screen
,from there you can download the templates . You should be able to some other
options also like CLI, PowerShell, .NET, Ruby. Refer download-template
Once you downloaded the files unzip that ,it has the
following structure which contains all
pertinent information about the various ways to deploy you resource to
ARM. For our purpose parameters and template.json files are essentials to
proceed further. Refer template-structure
template.Json files keeps an entire structure of the
resource you want to deploy while parameters contains runtime parameters
required in template.Json file.
Parameters.json
contains the information like “virtualMachineName”, virtualMachineRG,
osDiskType, virtualMachineSize and diagnosticsStorageAccountName. Parameters.json has the following structure.
{
"$schema":
"https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"value": "centralus"
},
"networkInterfaceName": {
"value": "master675"
},
"networkSecurityGroupName": {
"value": "master-nsg"
},
"networkSecurityGroupRules": {
"value": [
{
"name": "SSH",
"properties": {
"priority": 300,
"protocol": "TCP",
"access": "Allow",
"direction": "Inbound",
"sourceAddressPrefix": "*",
"sourcePortRange": "*",
"destinationAddressPrefix": "*",
"destinationPortRange": "22"
}
}
]
},
"subnetName": {
"value": "default"
},
"virtualNetworkName": {
"value": "kubeRG-vnet"
},
"addressPrefixes": {
"value":
[
"10.0.0.0/24"
]
},
"subnets": {
"value": [
{
"name": "default",
"properties": {
"addressPrefix": "10.0.0.0/24"
}
}
]
},
"publicIpAddressName": {
"value": "node3-ip"
},
"publicIpAddressType": {
"value": "Dynamic"
},
"publicIpAddressSku":
{
"value": "Basic"
},
"virtualMachineName": {
"value": "node3"
},
"virtualMachineRG": {
"value": "kubeRG"
},
"osDiskType": {
"value": "Premium_LRS"
},
"virtualMachineSize": {
"value": "Standard_D2s_v3"
},
"adminUsername": {
"value": "sachin"
},
"adminPublicKey": {
"value": null
},
"diagnosticsStorageAccountName":
{
"value": "kubergdiag642"
},
"diagnosticsStorageAccountId": {
"value":
"Microsoft.Storage/storageAccounts/kubergdiag642"
},
"diagnosticsStorageAccountType": {
"value": "Standard_LRS"
},
"diagnosticsStorageAccountKind": {
"value": "Storage"
},
"autoShutdownStatus": {
"value": "Enabled"
},
"autoShutdownTime": {
"value": "19:00"
},
"autoShutdownTimeZone": {
"value": "UTC"
},
"autoShutdownNotificationStatus": {
"value": "Disabled"
},
"autoShutdownNotificationLocale": {
"value": "en"
}
}
}
So far we are done and good to go. Open you PowerShell
window and type az login to get into your subscription.
Once you succeeded with
that type the following command to deploy your resource.
az group deployment create -n
"sachinDeployment" -g "kubeRG" --template-file
'C:\Learning\LinuxVM\VM\template.json' --parameters
'C:\Learning\LinuxVM\VM\parameters.json' --parameters
"adminPublicKey=ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAgQzk/MYIUMhMDpJgjgku6QdhLY0zagTdqFYWkJuTnz9tsBE7eRyFuzW9lK6PSTTSYHCbTPpWALJWGlwrEmWmXL62nss0ppa2IcuD9TMA3VkeFKE6EnOpiRF6lM6fBXyh+KtRFrzHIu6OUfeLbAy6UpPm1kRPcRtMX9nRX0hzpRKGLYdIh/gnwNYJsOHX/5wAvFiDfPSlIblYbL9HhWaw1Mm/b1r6vpV+WREhJ09q2Fh4uQwi75/XuUj9C+2c5NOM5HwKxILdiwad3FcTfNrGO0otHaXdAT0buAbZ7wL4QNKqLtcTelje2BGc6uunpyrYywQkn/VLBETC+LY21S4aBw==
rsa-key-20190723"
refer : power-shell-command
Note: in my case I’ve passed adminPublicKey as runtime parameter
which I have created with help of Agent Putty.
Yeeee , You are done. Go to azure portal and choose virtual
machine there.
You should get you virtual machine details there as in my
case it has created Node3.
Hope it helps you while you create VM using ARM templates
(Infrastructure as Code)