How to Create Azure Linux Virtual Machine (VM) using Terraform

How to Create Azure Linux Virtual Machine (VM) using Terraform. In this post, we will introduce Terraform, its features and advantages. We will then show you step by step process on how to create Azure Linux Virtual Machine using Terraform.

Is your DevOps Team demanding a software tool that helps build, change, and version infrastructure safely? There are various tools available online that can help deploy a hybrid cloud or multi cloud environment, but we recommend Terraform. First of all, Terraform is a trusted open source infrastructure automation tool.  These tools help organizations in managing multiple cloud services and build infrastructure safely.

Shall we start with How to Create Azure Linux Virtual Machine (VM) using Terraform.

What is Terraform?

Terraform is a popular infrastructure as code (IaC) software tool. All in all, it uses HashiCorp Configuration Language (a JSON like configuration language) to manage an application’s underlying IT infrastructure. Using its excellent features and plugin based architecture, users can easily connect with several providers like AWS, Google Cloud, etc. to monitor and provision resources.

Further, using the configuration files and version control, DevOps teams can quickly define their entire infrastructure. Additionally, being open source software, terraform allows users to write new plugins or compile existing ones to create a new version.

Additionally, Terraform Core and Terraform Plugins are the main components of the software tool. Basically, they are responsible for reading and interpolation of resource graphs and configuration files. Whereas the Terraform Plugins help define resources for specific services.

In Terraform, the operators using application programming interfaces (APIs) calls can easily create and manage resources on different cloud platforms. These resources available on cloud and on prem can further be reused and shared.

Below we have listed some of the features and benefits of Terraform. We think that makes it one of the best infrastructure automation tools.

Features of Terraform

Here are the following features supported by Terraform that ais in building infrastructure safely:

1. Installable modules – At least one module is available for each terraform configuration. All the user needs to do is visit the Terraform registry, download it from the configuration and install one. Modules are easy to write and share resources that help generate abstractions for defining an infrastructure.

2. Allows writing Declarative configuration files – The configuration files use terraform language to update users on which plugins require installation, declare resources, update how to manage existing resources or create new ones, and what resources need to be eliminated.

3. Supports Dependency Graph – Another excellent feature supported by the software tool is the dependency graph used to generate plans. Resource Node, Provider Configuration Node, and Resource Meta-Node are the three nodes of a dependency graph.

4. Supports multiple providers – Users can choose from multiple Terraform providers and use them for provisioning or re provisioning infrastructure. What is more, Terraform supports around 1000+ providers, thus making it easier for you to use one for cloud platforms and services.

5. Allows Provisioning Infrastructure in similar languages  – Terraform uses HashiCorp Configuration Language (HCL) to read and write configurations. Also, it allows users to use other similar languages, including Go, C#, Java, Python, etc., for defining infrastructure code. As a result, operators have free will to choose from 1000+ existing Terraform providers and programming languages.

6. Translations – Terraform allows operators to translate HCL code into JSON format for easy management and building of infrastructure.

 7. Module Count – This feature help users get an accurate count of the total number of modules applied to infrastructure.

Up next with How to Create Azure Linux Virtual Machine (VM) using Terraform. We talk about advantages of Terraform.

Advantages of Terraform

Check out the below listed benefits of using Terraform.

1. Helps Track Infrastructure – Terraform is one of the best automation tools to track infrastructure in a file which is further used for planning and analysis, i.e. That is to figure out what changes are necessary to match the configuration.

2. Lessens Development Costs – The powerful automation tool helps users create on demand development and deploy a hybrid cloud or multi cloud environment. It also helps in evaluation before making final changes and saves on development costs.

3. Automate changes – Another advantage of Terraform is operators can efficiently automate changes and provision resources. Rather than writing down step by step instructions, it allows operators to use its advanced features and automate changes. It also supports a resource graph feature that helps create and modify non dependent resources. 

4. Easily Manages any Infrastructure – Terraform supports a wide array of plugins that help interact with multiple cloud platforms, including Amazon Web Services, Azure, GitHub, Google cloud platforms, etc.

5. Involves Less time for provisioning – Unlike other tools, terraform offers full deployments in minutes. It is a quick and efficient infrastructure automation tool used by operators for the provisioning of multiple cloud services in a standardized way.

6. Requires No Additional Documentation – Terraform provides written scripts that help in discovering the current status, new deployments, and configuration. It works similarly to the documentation. Thus, there is no extra need for documentation work with Terraform.

7. Allows Operators to Plan and Predict Changes – Terraform has a plan command that allows operators to verify if the proposed plan matches their prediction or not. DevOps teams can easily access and generate an execution plan for infrastructure using Terraform.

The main part of our article about How to Create Azure Linux Virtual Machine (VM) using Terraform.

How to Create Azure Linux Virtual Machine (VM) using Terraform


  • Azure credentials with permissions to manage Azure VM.
  • A CentOS or Ubuntu operating system installed on your local machine.

Install Terraform

Before starting, you will need to install the Terraform on your local machine.

Follow the below steps to install the Terraform on Ubuntu and Debian based operating systems.

First, install the Curl command line tool using the following command:

					sudo apt install curl -y

Next, download and add the Terraform GPG key using the following command:

					curl -fsSL | sudo apt-key add -

Now, add the Terraform repository to the APT using the following command:

					sudo apt-add-repository "deb [arch=amd64] $(lsb_release -cs) main"

Next, run the following command to install the Terraform:

					sudo apt-get install terraform -y

After the successful installation, verify the Terraform version using the following command:

					terraform --version

You should see the Terraform version in the following output:

					Terraform v1.2.8
on linux_amd64

Follow the below steps to install the Terraform on CentOS, RHEL and Rocky Linux operating systems.

First, install the yum-utils package using the following command:

					sudo yum install -y yum-utils

Next, add the Terraform repo to your server with the following command:

					sudo yum-config-manager --add-repo

Once the Terraform repo is ready, you can install the Terraform with the following command:

					sudo yum -y install terraform

After installing Terraform package on your server, you can proceed to the next step.

Create the Azure Provider

First, you will need to create a directory to store your Terraform configuration file. You can create it with the following command:

					mkdir azure

Next, create a Terraform providers configuration file inside the azure directory:

					cd azure

Define your cloud provider, and credentials to authenticate Azure:

					terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.37.0"
# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
  subscription_id = "<Your-Subscription Id>"
  tenant_id       = "<Your-Tenant-Id>"
  client_id       = "<Your-Client-Id>"
  client_secret   = "<Your-Client-Secret>"


Save and close the file after you finish. You can then proceed to the next step.

Define the Azure Resource Group

Next, create a Terraform main configuration file and create a new resource group to define your region:


Add the following configurations to define your region:

					resource   "azurerm_resource_group"   "rg"   { 
   name   =   "my-first-terraform-rg" 
   location   =   "northeurope" 

Create Network Security Group and Rule

Next, create a new resource group to allow incoming SSH traffic on Linux VM:

					resource "azurerm_network_security_group" "myterraformnsg" {
    name                = "myNetworkSecurityGroup"
    location            = "eastus"
    resource_group_name =

    security_rule {
        name                       = "SSH"
        priority                   = 1001
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"

    tags = {
        environment = "Terraform Demo"


Define a Virtual Network and Subnet

Next, create a resource group to define a virtual network and address:

					resource   "azurerm_virtual_network"   "myvnet"   { 
   name   =   "my-vnet" 
   address_space   =   [ "" ] 
   location   =   "northeurope" 
   resource_group_name   = 

 resource   "azurerm_subnet"   "frontendsubnet"   { 
   name   =   "frontendSubnet" 
   resource_group_name   = 
   virtual_network_name   = 
   address_prefix   =   "" 


Define a New Public IP Address

Next, create a resource group to define VM public IP:

					resource   "azurerm_public_ip"   "azurevm1publicip"   { 
   name   =   "pip1" 
   location   =   "northeurope" 
   resource_group_name   = 
   allocation_method   =   "Dynamic" 
   sku   =   "Basic" 

Define a Network Interface For VM

Next, create a resource group to define a network interface:

					resource   "azurerm_network_interface"   "azurevm1nic"   { 
   name   =   "azurevm1-nic" 
   location   =   "northeurope" 
   resource_group_name   = 

   ip_configuration   { 
     name   =   "ipconfig1" 
     subnet_id   = 
     private_ip_address_allocation   =   "Dynamic" 
     public_ip_address_id   =   azurerm_public_ip.myvm 1 


Define the Virtual Machine

Next, create a resource group to define the Virtual machine image, admin user and password:

					resource   "azurerm_linux_virtual_machine"   "myterraformvm"   { 
   name                    =   "azurevm1"   
   location                =   "northeurope" 
   resource_group_name     = 
   network_interface_ids   =   [ azurerm_network_interface.myvm 1 ] 
   size                    =   "Standard_B1s" 
   computer_name           =   "linuxvm"
   admin_username          =   "hiteshj" 
   admin_password          =   "securepassword!" 

   source_image_reference   { 
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = "20.04-LTS"
        version   = "latest"

   os_disk   { 
     caching             =   "ReadWrite" 
     storage_account_type   = "Standard_LRS" 


Create a Resource Group to Print VM Public IP

Next, define a resource group to display the Public IP address of Linux VM.

					output "resource_group_name" {
  value =

output "public_ip_address" {
  value = azurerm_linux_virtual_machine.myterraformvm.public_ip_address


Save and close the file after you finish.

Deploy Azure VM with Terraform

At this point, the Terraform configuration file is ready to deploy your first Azure VM.

First, run the terrafrom init command to download all resources which we have define in the Terraform configuration file.

					terraform init

Next, run the terraform plan command to preview the actions Terraform would take to modify your infrastructure.

					terraform plan

Finally, run the terraform apply command to apply the execution plan to your Azure cloud infrastructure and create a Linux VM:

					terraform apply

Once your Linux VM is deployed on the Azure cloud, you will get the Public IP address of your Linux VM.

You can now use the SSH command to connect the Linux VM:

					ssh hiteshj@public-ip

Destroy Azure VM

If you don’t require your Azure Virtual Machine and want to remove it. Then, you can destroy it by just running the following commad:

					terraform destroy

Thank you for reading the whole article How to Create Azure Linux Virtual Machine (VM) using Terraform. Let’s conclude. 

How to Create Azure Linux Virtual Machine (VM) using Terraform Conclusion

In this tutorial, we explained each and every steps required to deploy a Linux VM on the Azure cloud. You can now write your own Terraform configuration file and deploy any VM on the Azure cloud. Terraform allow you to deploy infrastructure at quicker speeds with less manual intervention.

Please read more about Terraform in our blog here

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x