DEV Community

Harshal Sonar
Harshal Sonar

Posted on

Getting Started with Terraform: Deploy Your First Azure Virtual Machine

Introduction
Terraform is a tool that helps you create and manage cloud resources using simple code. Instead of clicking around in the Azure portal, you write a file that tells Azure what to build.

In this blog, I will show you how to create your first virtual machine on Microsoft Azure using Terraform — step by step.

Why Use Terraform?

Easy to use: Write code to manage infrastructure.

Repeatable: Use the same code to create the same resources anytime.

Works with many clouds: Azure, AWS, Google Cloud, and more.

Track changes: See what will happen before applying changes.

Step 1: Install Terraform
Go to terraform.io

Download Terraform for your computer

Install and open your terminal/command prompt

Check if Terraform is installed by typing:
Run below command in bash for checking version.

terraform version

Step 2: Write Your Terraform File
Create a new folder for your project. Inside that folder, create a file called main.tf.

Copy and paste this code into main.tf:

Copy

Copy
hclCopyEditprovider "azurerm" {
features {}
}

resource "azurerm_resource_group" "mygroup" {
name = "myResourceGroup"
location = "East US"
}

resource "azurerm_virtual_machine" "myvm" {
name = "myVM"
location = azurerm_resource_group.mygroup.location
resource_group_name = azurerm_resource_group.mygroup.name
network_interface_ids = []
vm_size = "Standard_DS1_v2"

storage_image_reference {
  publisher = "Canonical"
  offer     = "UbuntuServer"
  sku       = "18.04-LTS"
  version   = "latest"
}

storage_os_disk {
  name              = "myOsDisk"
  caching           = "ReadWrite"
  create_option     = "FromImage"
  managed_disk_type = "Standard_LRS"
}

os_profile {
  computer_name  = "myVM"
  admin_username = "azureuser"
  admin_password = "ChangeYourPassword123!"
}

os_profile_linux_config {
  disable_password_authentication = false
}
Enter fullscreen mode Exit fullscreen mode

}
Step 3: Run Terraform Commands
Open your terminal and run:

Copy

Copy
bashCopyEditterraform init # Prepare your project

terraform plan # See what Terraform will do

terraform apply # Create your resources
Step 4: Check Your Azure Portal
After terraform apply finishes, log in to your Azure Portal to see the virtual machine created by Terraform.

Conclusion
That’s it! You created your first Azure virtual machine using Terraform code. With Terraform, you can manage all your cloud resources easily and keep track of changes using code.

Add Canonical Link (To Avoid Duplicate SEO Issues)
https://harshalsonar.hashnode.dev/getting-started-with-terraform-deploy-your-first-azure-virtual-machine

Top comments (0)