How fast you can spin up a VM in Azure Cloud? Have you experienced an azure cloud shell and terraform builder tool? Let’s quickly spin up a Linux VM using terraform code from Azure Cloud Shell. There are 100 ways to build the VM but this article just shows the robustness of the terraform and command line.
1.Login to Azure portal and then access https://shell.azure.com/.
2. Create terraform deployment file for Linux like below.
Assumptions: Deploying in to existing resource group, existing vnet, subnet
provider "azurerm" { version = "~>2.0" features {} } # Reference existing resource group data "azurerm_resource_group" "main" { name = "UArg" } # Reference existing vnet data "azurerm_virtual_network" "main" { name = "UAVnet" resource_group_name = data.azurerm_resource_group.main.name } # Reference existing subnet data "azurerm_subnet" "main" { name = "test" virtual_network_name = data.azurerm_virtual_network.main.name resource_group_name = data.azurerm_resource_group.main.name } # Create network interface resource "azurerm_network_interface" "main" { name = "UAvm1-nic" location = "westus2" resource_group_name = data.azurerm_resource_group.main.name ip_configuration { name = "UAvm1-ip" subnet_id = data.azurerm_subnet.main.id private_ip_address_allocation = "Dynamic" } tags = { environment = "UnixArena Terraform Demo" } } #Create CentOS 7.5 virtual machince resource "azurerm_linux_virtual_machine" "main" { name = "UAvm1" location = "westus2" resource_group_name = data.azurerm_resource_group.main.name network_interface_ids = [azurerm_network_interface.main.id] size = "Standard_DS1_v2" os_disk { name = "UAvm1OSdisk" caching = "ReadWrite" storage_account_type = "Standard_LRS" } source_image_reference { publisher = "OpenLogic" offer = "CentOS" sku = "7.5" version = "latest" } computer_name = "myvm" admin_username = "azureuser" admin_password = "test@123" disable_password_authentication = false tags = { environment = "UnixArena Terraform Demo" } }
3. Execute terraform plan to preview the deployment .
UA@Azure:~/clouddrive/terraform/linux_vm$ terraform plan Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. data.azurerm_resource_group.main: Refreshing state... data.azurerm_virtual_network.main: Refreshing state... data.azurerm_subnet.main: Refreshing state... ------------------------------------------------------------------------ An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # azurerm_linux_virtual_machine.main will be created + resource "azurerm_linux_virtual_machine" "main" { + admin_password = (sensitive value) + admin_username = "azureuser" + allow_extension_operations = true + computer_name = "myvm" + disable_password_authentication = false + id = (known after apply) + location = "westus2" + max_bid_price = -1 + name = "UAvm1" + network_interface_ids = (known after apply) + priority = "Regular" + private_ip_address = (known after apply) + private_ip_addresses = (known after apply) + provision_vm_agent = true + public_ip_address = (known after apply) + public_ip_addresses = (known after apply) + resource_group_name = "UArg" + size = "Standard_DS1_v2" + tags = { + "environment" = "UnixArena Terraform Demo" } + virtual_machine_id = (known after apply) + zone = (known after apply) + os_disk { + caching = "ReadWrite" + disk_size_gb = (known after apply) + name = "UAvm1OSdisk" + storage_account_type = "Standard_LRS" + write_accelerator_enabled = false } + source_image_reference { + offer = "CentOS" + publisher = "OpenLogic" + sku = "7.5" + version = "latest" } } # azurerm_network_interface.main will be created + resource "azurerm_network_interface" "main" { + applied_dns_servers = (known after apply) + dns_servers = (known after apply) + enable_accelerated_networking = false + enable_ip_forwarding = false + id = (known after apply) + internal_dns_name_label = (known after apply) + internal_domain_name_suffix = (known after apply) + location = "westus2" + mac_address = (known after apply) + name = "UAvm1-nic" + private_ip_address = (known after apply) + private_ip_addresses = (known after apply) + resource_group_name = "UArg" + tags = { + "environment" = "UnixArena Terraform Demo" } + virtual_machine_id = (known after apply) + ip_configuration { + name = "UAvm1-ip" + primary = (known after apply) + private_ip_address = (known after apply) + private_ip_address_allocation = "dynamic" + private_ip_address_version = "IPv4" + subnet_id = "/subscriptions/585051ec-7aa0-48ab-a172-d1260ad72ee5/resourceGroups/UArg/providers/Microsoft.Network/virtualNetworks/UAVnet/subnets/test" } } Plan: 2 to add, 0 to change, 0 to destroy. ------------------------------------------------------------------------ Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run. UA@Azure:~/clouddrive/terraform/linux_vm$
4. If the preview of the deployment looks fine, execute terraform apply to create the VM.
UA@Azure:~/clouddrive/terraform/linux_vm$ terraform apply data.azurerm_resource_group.main: Refreshing state... data.azurerm_virtual_network.main: Refreshing state... data.azurerm_subnet.main: Refreshing state... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # azurerm_linux_virtual_machine.main will be created + resource "azurerm_linux_virtual_machine" "main" { + admin_password = (sensitive value) + admin_username = "azureuser" + allow_extension_operations = true + computer_name = "myvm" + disable_password_authentication = false + id = (known after apply) + location = "westus2" + max_bid_price = -1 + name = "UAvm1" + network_interface_ids = (known after apply) + priority = "Regular" + private_ip_address = (known after apply) + private_ip_addresses = (known after apply) + provision_vm_agent = true + public_ip_address = (known after apply) + public_ip_addresses = (known after apply) + resource_group_name = "UArg" + size = "Standard_DS1_v2" + tags = { + "environment" = "UnixArena Terraform Demo" } + virtual_machine_id = (known after apply) + zone = (known after apply) + os_disk { + caching = "ReadWrite" + disk_size_gb = (known after apply) + name = "UAvm1OSdisk" + storage_account_type = "Standard_LRS" + write_accelerator_enabled = false } + source_image_reference { + offer = "CentOS" + publisher = "OpenLogic" + sku = "7.5" + version = "latest" } } # azurerm_network_interface.main will be created + resource "azurerm_network_interface" "main" { + applied_dns_servers = (known after apply) + dns_servers = (known after apply) + enable_accelerated_networking = false + enable_ip_forwarding = false + id = (known after apply) + internal_dns_name_label = (known after apply) + internal_domain_name_suffix = (known after apply) + location = "westus2" + mac_address = (known after apply) + name = "UAvm1-nic" + private_ip_address = (known after apply) + private_ip_addresses = (known after apply) + resource_group_name = "UArg" + tags = { + "environment" = "UnixArena Terraform Demo" } + virtual_machine_id = (known after apply) + ip_configuration { + name = "UAvm1-ip" + primary = (known after apply) + private_ip_address = (known after apply) + private_ip_address_allocation = "dynamic" + private_ip_address_version = "IPv4" + subnet_id = "/subscriptions/585051ec-7aa0-48ab-a172-d1260ad72ee5/resourceGroups/UArg/providers/Microsoft.Network/virtualNetworks/UAVnet/subnets/test" } } Plan: 2 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes azurerm_network_interface.main: Creating... azurerm_network_interface.main: Creation complete after 2s [id=/subscriptions/585051ec-7aa0-48ab-a172-d1260ad72ee5/resourceGroups/UArg/providers/Microsoft.Network/networkInterfaces/UAvm1-nic] azurerm_linux_virtual_machine.main: Creating... azurerm_linux_virtual_machine.main: Still creating... [10s elapsed] azurerm_linux_virtual_machine.main: Still creating... [20s elapsed] azurerm_linux_virtual_machine.main: Still creating... [30s elapsed] azurerm_linux_virtual_machine.main: Still creating... [40s elapsed] azurerm_linux_virtual_machine.main: Still creating... [50s elapsed] azurerm_linux_virtual_machine.main: Still creating... [1m0s elapsed] azurerm_linux_virtual_machine.main: Still creating... [1m10s elapsed] azurerm_linux_virtual_machine.main: Still creating... [1m20s elapsed] azurerm_linux_virtual_machine.main: Still creating... [1m30s elapsed] azurerm_linux_virtual_machine.main: Creation complete after 1m37s [id=/subscriptions/585051ec-7aa0-48ab-a172-d1260ad72ee5/resourceGroups/UArg/providers/Microsoft.Compute/virtualMachines/UAvm1] Apply complete! Resources: 2 added, 0 changed, 0 destroyed. UA@Azure:~/clouddrive/terraform/linux_vm$
5. You can login to portal and check the newly created VM.
Hope this article is informative to you. Share it! Comment it!! Be sociable!!!
knopfler1980 says
Hello, my unknowledege about clouds like azure or amazon is not small 🙂 , I’m deploying a virtual machine in Google Cloud Platform at the moment (not with kubernetes engines because are paid version) but as always free Google option. Do Amazon and Azure have similar programs? With google I could take snapshots or create virtual machine through Google console but I prefer to manage graphically at the moment as I don’t need to automatize a deployment of hundreds of virtual machines or kubernetes.
Regards and thanks for the article
Lingeswaran R says
Azure offers 200$ credit to explore.
Lingesh