Azure Virtual Machines - Basics

Azure Virtual Machines - Basics

 
In this article, we are going through some basic details on Azure Virtual Machines; i.e., how to create and deploy through Azure Portal, Azure CLI, and Azure C# .Net.
 

What are Azure Virtual Machines?

 
Azure Virtual Machines is a product/service offered by Azure that hosts Windows or Linux image instances of Virtual Machines. It offers a large set of hardware configurations, including the most recent cutting-edge technologies from the market, in order to build the best Virtual Machine that suits your needs. It also offers a wide variety of ways to manage your VMs, from the Azure Portal Website up to Azure CLI, Powershell or C# code.
 

Benefits of Azure Virtual Machines

  • High Availability, with guaranteed SLA from 99,9 up to 99,99%;
  • Pay as you use, with the billing being sum per-second usage;
  • Security, offering many types of encryption;
  • Scalability, being able to scale from one to thousands Virtual Machines in minutes;
  • Hybrid environments, being able to access your cloud and on-premises environment;

Creation and management options

 
Azure Virtual Machine Creation checklist
  1. Resource Group, where you will place your Virtual Machine resources;
  2. Virtual Network, where the Virtual Machine will use to communicate with the internet and other resources;
  3. IP Address, used for your Virtual Machine communication;
  4. Network Interface, connecting your IP Address with your Virtual Network;
  5. Availability Set, handling the availability and reliability of your Virtual Machine;
  6. Operational System, to run your Virtual Machine;
  7. Admin credentials, to manage your Virtual Machine;
  8. Machine Size, with the memory, CPU and disk types;
  9. Virtual Machine, with your admin credentials, OS, size and availability set.
VMs Pricing Calculation
 
Due to the many types of possible configurations and prices, it would be very difficult to have an idea of how much you would pay for hosting your Virtual Machine within Azure. That's why Azure has provided a free and complete pricing calculator, you may simulate among the various types of configuration in order to check which one fits you better.

Create an Azure VM using the Azure portal

 
This is the easiest way to create your Virtual Machine, needing no code.
 
Go to the Virtual Machines section 
 
Option 1
 
 
Option 2
 
 
Virtual Machines Section 
 
 
Fill the form with your VM data,
 
And after filling the form, click in Review + Create 
 
 
Wait for validation,
 
After validation success, click on create. 
 
Ps.: You could also download the Virtual Machine template in order to automate the creating process with Azure ARM.
 
 
Validate the output and wait for deployment
 
 
Result
 
 
 

Create an Azure VM using Azure CLI

 
This method of creating your Virtual Machine is also very easy but requires some coding knowledge if you want to customize your VM.
 
Login 
 
In order for Azure  to know which account you are using, you need to log in 
  1. az login   
Create your Resource Group
  1. $location = "westeurope"  
  2. $resourceGroup = "azureCLiResourceGroup"  
  3. az group create --name $resourceGroup --location $location   
Create the Virtual Machine
  1. $vmName ="VMWithCli"  
  2. $vmUser = "sampleUser"  
  3. $vmPassword = "SamplePass1234"  
  4. $resourceGroup = "azureCLiResourceGroup"  
  5. az vm create   --resource-group $resourceGroup --name $vmName --image win2016datacenter --admin-username $vmUser --admin-password $vmPassword  
Result
 
Azure CLI response
  1. {  
  2.   "id""/subscriptions/fcde3549-dbd1-45e1-ac40-06610ff281e1/resourceGroups/azureCLiResourceGroup",  
  3.   "location""westeurope",  
  4.   "managedBy"null,  
  5.   "name""azureCLiResourceGroup",  
  6.   "properties": {  
  7.     "provisioningState""Succeeded"  
  8.   },  
  9.   "tags"null,  
  10.   "type""Microsoft.Resources/resourceGroups"  
  11. }  
  12. {  
  13.   "fqdns""",  
  14.   "id""/subscriptions/fcde3549-dbd1-45e1-ac40-06610ff281e1/resourceGroups/azureCLiResourceGroup/providers/Microsoft.Compute/virtualMachines/VMWithCli",  
  15.   "location""westeurope",  
  16.   "macAddress""00-0D-3A-AB-70-94",  
  17.   "powerState""VM running",  
  18.   "privateIpAddress""10.0.0.4",  
  19.   "publicIpAddress""65.52.131.227",  
  20.   "resourceGroup""azureCLiResourceGroup",  
  21.   "zones"""  
  22. }  
Azure Portal Validation
 
 

Create and Manage Azure VMs using C#

 
Requirements
 
Nuget Package
 
Have an Azure Service Principal account
 
To see how to create an Azure Service Principal, check Microsoft's official guide.
 
Create a new console application
 
Create a new console application for .Net Framework and name it as AzureVirtualMachine. Your project must look like this:
 
 
Setting Local Variables
 
Variables are going to be used through various commands:
  1. var groupName = "sampleResourceGroup";  
  2. var vmName = "VMWithCSharp";  
  3. var location = Region.EuropeWest;  
Connect to your Azure Account
  1. var credentials = SdkContext.AzureCredentialsFactory  
  2.               .FromServicePrincipal("clientId""clientSecret""tenantId", AzureEnvironment.AzureGlobalCloud);  
  3.   
  4.           var azure = Azure  
  5.               .Configure()  
  6.               .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)  
  7.               .Authenticate(credentials)  
  8.               .WithDefaultSubscription();  
Create a Resource Group
  1. var resourceGroup = azure.ResourceGroups.Define(groupName)  
  2.           .WithRegion(location)  
  3.           .Create();  
Create the Virtual Network 
  1. var network = azure.Networks.Define("sampleVirtualNetwork")  
  2.   .WithRegion(location)  
  3.   .WithExistingResourceGroup(groupName)  
  4.   .WithAddressSpace("10.0.0.0/16")  
  5.   .WithSubnet("sampleSubNet""10.0.0.0/24")  
  6.   .Create();  
Create the Public IP Address
 
With dynamic IP:
  1. var publicIPAddress = azure.PublicIPAddresses.Define("samplePublicIP")  
  2.     .WithRegion(location)  
  3.     .WithExistingResourceGroup(groupName)  
  4.     .WithDynamicIP()  
  5.     .Create();  
Create the Network Interface 
  1. var networkInterface = azure.NetworkInterfaces.Define("sampleNetWorkInterface")  
  2.                .WithRegion(location)  
  3.                .WithExistingResourceGroup(groupName)  
  4.                .WithExistingPrimaryNetwork(network)  
  5.                .WithSubnet("sampleSubNet")  
  6.                .WithPrimaryPrivateIPAddressDynamic()  
  7.                .WithExistingPrimaryPublicIPAddress(publicIPAddress)  
  8.                .Create();  
Create the Availability Set
 
With aligned SKU set type due to having a managed disk 
  1. var availabilitySet = azure.AvailabilitySets.Define("sampleAvailabilitySet")  
  2.     .WithRegion(location)  
  3.     .WithExistingResourceGroup(groupName)  
  4.     .WithSku(AvailabilitySetSkuTypes.Aligned)   
  5.     .Create();  
Create the Virtual Machine
 
Using Windows Server as Operating System with 2012 R2 image and with machine size set as Stardard B1.
  1. azure.VirtualMachines.Define(vmName)  
  2.     .WithRegion(location)  
  3.     .WithExistingResourceGroup(groupName)  
  4.     .WithExistingPrimaryNetworkInterface(networkInterface)                  
  5.     .WithLatestWindowsImage("MicrosoftWindowsServer""WindowsServer""2012-R2-Datacenter")  
  6.     .WithAdminUsername("sampleUser")  
  7.     .WithAdminPassword("Sample123467")  
  8.     .WithComputerName(vmName)  
  9.     .WithExistingAvailabilitySet(availabilitySet)  
  10.     .WithSize(VirtualMachineSizeTypes.StandardB1s)  
  11.     .Create();  
Complete code
  1. using Microsoft.Azure.Management.Compute.Fluent.Models;  
  2. using Microsoft.Azure.Management.Fluent;  
  3. using Microsoft.Azure.Management.ResourceManager.Fluent;  
  4. using Microsoft.Azure.Management.ResourceManager.Fluent.Core;  
  5.   
  6. namespace AzureVirtualMachine  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             var credentials = SdkContext.AzureCredentialsFactory  
  13.                 .FromServicePrincipal("clientId""clientSecret""tenantId", AzureEnvironment.AzureGlobalCloud);  
  14.   
  15.   
  16.             var azure = Azure  
  17.                 .Configure()  
  18.                 .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)  
  19.                 .Authenticate(credentials)  
  20.                 .WithDefaultSubscription();  
  21.   
  22.   
  23.             var groupName = "sampleResourceGroup";  
  24.             var vmName = "VMWithCSharp";  
  25.             var location = Region.EuropeWest;  
  26.   
  27.   
  28.             var resourceGroup = azure.ResourceGroups.Define(groupName)  
  29.                 .WithRegion(location)  
  30.                 .Create();  
  31.   
  32.             var network = azure.Networks.Define("sampleVirtualNetwork")  
  33.               .WithRegion(location)  
  34.               .WithExistingResourceGroup(groupName)  
  35.               .WithAddressSpace("10.0.0.0/16")  
  36.               .WithSubnet("sampleSubNet""10.0.0.0/24")  
  37.               .Create();  
  38.   
  39.             var publicIPAddress = azure.PublicIPAddresses.Define("samplePublicIP")  
  40.                 .WithRegion(location)  
  41.                 .WithExistingResourceGroup(groupName)  
  42.                 .WithDynamicIP()  
  43.                 .Create();  
  44.   
  45.             var networkInterface = azure.NetworkInterfaces.Define("sampleNetWorkInterface")  
  46.                 .WithRegion(location)  
  47.                 .WithExistingResourceGroup(groupName)  
  48.                 .WithExistingPrimaryNetwork(network)  
  49.                 .WithSubnet("sampleSubNet")  
  50.                 .WithPrimaryPrivateIPAddressDynamic()  
  51.                 .WithExistingPrimaryPublicIPAddress(publicIPAddress)  
  52.                 .Create();  
  53.   
  54.             var availabilitySet = azure.AvailabilitySets.Define("sampleAvailabilitySet")  
  55.                 .WithRegion(location)  
  56.                 .WithExistingResourceGroup(groupName)  
  57.                 .WithSku(AvailabilitySetSkuTypes.Aligned)   
  58.                 .Create();            
  59.   
  60.             azure.VirtualMachines.Define(vmName)  
  61.                 .WithRegion(location)  
  62.                 .WithExistingResourceGroup(groupName)  
  63.                 .WithExistingPrimaryNetworkInterface(networkInterface)                  
  64.                 .WithLatestWindowsImage("MicrosoftWindowsServer""WindowsServer""2012-R2-Datacenter")  
  65.                 .WithAdminUsername("sampleUser")  
  66.                 .WithAdminPassword("Sample123467")  
  67.                 .WithComputerName(vmName)  
  68.                 .WithExistingAvailabilitySet(availabilitySet)  
  69.                 .WithSize(VirtualMachineSizeTypes.StandardB1s)  
  70.                 .Create();  
  71.         }  
  72.     }  
  73. }  
Result
 
Virtual Machine created successfully
 
 
Congratulations, you have successfully created your Azure Virtual Machine.
 
 
External References


Similar Articles