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
- Resource Group, where you will place your Virtual Machine resources;
- Virtual Network, where the Virtual Machine will use to communicate with the internet and other resources;
- IP Address, used for your Virtual Machine communication;
- Network Interface, connecting your IP Address with your Virtual Network;
- Availability Set, handling the availability and reliability of your Virtual Machine;
- Operational System, to run your Virtual Machine;
- Admin credentials, to manage your Virtual Machine;
- Machine Size, with the memory, CPU and disk types;
- 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
Create your Resource Group
- $location = "westeurope"
- $resourceGroup = "azureCLiResourceGroup"
- az group create --name $resourceGroup --location $location
Create the Virtual Machine
- $vmName ="VMWithCli"
- $vmUser = "sampleUser"
- $vmPassword = "SamplePass1234"
- $resourceGroup = "azureCLiResourceGroup"
- az vm create --resource-group $resourceGroup --name $vmName --image win2016datacenter --admin-username $vmUser --admin-password $vmPassword
Result
Azure CLI response
- {
- "id": "/subscriptions/fcde3549-dbd1-45e1-ac40-06610ff281e1/resourceGroups/azureCLiResourceGroup",
- "location": "westeurope",
- "managedBy": null,
- "name": "azureCLiResourceGroup",
- "properties": {
- "provisioningState": "Succeeded"
- },
- "tags": null,
- "type": "Microsoft.Resources/resourceGroups"
- }
- {
- "fqdns": "",
- "id": "/subscriptions/fcde3549-dbd1-45e1-ac40-06610ff281e1/resourceGroups/azureCLiResourceGroup/providers/Microsoft.Compute/virtualMachines/VMWithCli",
- "location": "westeurope",
- "macAddress": "00-0D-3A-AB-70-94",
- "powerState": "VM running",
- "privateIpAddress": "10.0.0.4",
- "publicIpAddress": "65.52.131.227",
- "resourceGroup": "azureCLiResourceGroup",
- "zones": ""
- }
Azure Portal Validation
Create and Manage Azure VMs using C#
Requirements
Nuget Package
Have an Azure Service Principal account
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:
- var groupName = "sampleResourceGroup";
- var vmName = "VMWithCSharp";
- var location = Region.EuropeWest;
Connect to your Azure Account
- var credentials = SdkContext.AzureCredentialsFactory
- .FromServicePrincipal("clientId", "clientSecret", "tenantId", AzureEnvironment.AzureGlobalCloud);
-
- var azure = Azure
- .Configure()
- .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
- .Authenticate(credentials)
- .WithDefaultSubscription();
Create a Resource Group
- var resourceGroup = azure.ResourceGroups.Define(groupName)
- .WithRegion(location)
- .Create();
Create the Virtual Network
- var network = azure.Networks.Define("sampleVirtualNetwork")
- .WithRegion(location)
- .WithExistingResourceGroup(groupName)
- .WithAddressSpace("10.0.0.0/16")
- .WithSubnet("sampleSubNet", "10.0.0.0/24")
- .Create();
Create the Public IP Address
With dynamic IP:
- var publicIPAddress = azure.PublicIPAddresses.Define("samplePublicIP")
- .WithRegion(location)
- .WithExistingResourceGroup(groupName)
- .WithDynamicIP()
- .Create();
Create the Network Interface
- var networkInterface = azure.NetworkInterfaces.Define("sampleNetWorkInterface")
- .WithRegion(location)
- .WithExistingResourceGroup(groupName)
- .WithExistingPrimaryNetwork(network)
- .WithSubnet("sampleSubNet")
- .WithPrimaryPrivateIPAddressDynamic()
- .WithExistingPrimaryPublicIPAddress(publicIPAddress)
- .Create();
Create the Availability Set
With aligned SKU set type due to having a managed disk
- var availabilitySet = azure.AvailabilitySets.Define("sampleAvailabilitySet")
- .WithRegion(location)
- .WithExistingResourceGroup(groupName)
- .WithSku(AvailabilitySetSkuTypes.Aligned)
- .Create();
Create the Virtual Machine
Using Windows Server as Operating System with 2012 R2 image and with machine size set as Stardard B1.
- azure.VirtualMachines.Define(vmName)
- .WithRegion(location)
- .WithExistingResourceGroup(groupName)
- .WithExistingPrimaryNetworkInterface(networkInterface)
- .WithLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter")
- .WithAdminUsername("sampleUser")
- .WithAdminPassword("Sample123467")
- .WithComputerName(vmName)
- .WithExistingAvailabilitySet(availabilitySet)
- .WithSize(VirtualMachineSizeTypes.StandardB1s)
- .Create();
Complete code
- using Microsoft.Azure.Management.Compute.Fluent.Models;
- using Microsoft.Azure.Management.Fluent;
- using Microsoft.Azure.Management.ResourceManager.Fluent;
- using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
-
- namespace AzureVirtualMachine
- {
- class Program
- {
- static void Main(string[] args)
- {
- var credentials = SdkContext.AzureCredentialsFactory
- .FromServicePrincipal("clientId", "clientSecret", "tenantId", AzureEnvironment.AzureGlobalCloud);
-
-
- var azure = Azure
- .Configure()
- .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
- .Authenticate(credentials)
- .WithDefaultSubscription();
-
-
- var groupName = "sampleResourceGroup";
- var vmName = "VMWithCSharp";
- var location = Region.EuropeWest;
-
-
- var resourceGroup = azure.ResourceGroups.Define(groupName)
- .WithRegion(location)
- .Create();
-
- var network = azure.Networks.Define("sampleVirtualNetwork")
- .WithRegion(location)
- .WithExistingResourceGroup(groupName)
- .WithAddressSpace("10.0.0.0/16")
- .WithSubnet("sampleSubNet", "10.0.0.0/24")
- .Create();
-
- var publicIPAddress = azure.PublicIPAddresses.Define("samplePublicIP")
- .WithRegion(location)
- .WithExistingResourceGroup(groupName)
- .WithDynamicIP()
- .Create();
-
- var networkInterface = azure.NetworkInterfaces.Define("sampleNetWorkInterface")
- .WithRegion(location)
- .WithExistingResourceGroup(groupName)
- .WithExistingPrimaryNetwork(network)
- .WithSubnet("sampleSubNet")
- .WithPrimaryPrivateIPAddressDynamic()
- .WithExistingPrimaryPublicIPAddress(publicIPAddress)
- .Create();
-
- var availabilitySet = azure.AvailabilitySets.Define("sampleAvailabilitySet")
- .WithRegion(location)
- .WithExistingResourceGroup(groupName)
- .WithSku(AvailabilitySetSkuTypes.Aligned)
- .Create();
-
- azure.VirtualMachines.Define(vmName)
- .WithRegion(location)
- .WithExistingResourceGroup(groupName)
- .WithExistingPrimaryNetworkInterface(networkInterface)
- .WithLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter")
- .WithAdminUsername("sampleUser")
- .WithAdminPassword("Sample123467")
- .WithComputerName(vmName)
- .WithExistingAvailabilitySet(availabilitySet)
- .WithSize(VirtualMachineSizeTypes.StandardB1s)
- .Create();
- }
- }
- }
Result
Virtual Machine created successfully
Congratulations, you have successfully created your Azure Virtual Machine.
External References