Effortless Azure Linux VM Deployment with Terraform and Ubuntu 20.04
Managing cloud infrastructure can be a daunting task, especially when it comes to deploying and configuring virtual machines (VMs). Luckily, tools like Terraform can simplify this process by automating the creation of infrastructure resources, including Azure Linux VMs. In this article, we'll walk through the steps of deploying an Ubuntu 20.04 VM on Azure using Terraform, offering a straightforward and repeatable method for your cloud infrastructure needs.
The Problem:
Creating a new Linux VM on Azure manually involves navigating the Azure portal, configuring various settings, and potentially waiting for long deployment times. This process can be repetitive, error-prone, and time-consuming.
The Solution:
Terraform provides a powerful and efficient solution by allowing you to define your desired infrastructure in a declarative way using configuration files. These files act as blueprints, detailing all the required resources and their settings. When you run Terraform, it analyzes your configuration and automatically provisions the specified resources on Azure, saving you time and effort.
Let's Get Started:
Here's a step-by-step guide to creating an Ubuntu 20.04 VM on Azure using Terraform:
-
Prerequisites:
- An active Azure subscription.
- Terraform installed on your machine.
- An Azure Service Principal with necessary permissions.
- Ubuntu 20.04 image available in your Azure region.
-
Create a Terraform configuration file (
main.tf
):
# Configure the Azure Provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
# Configure Azure Credentials
provider "azurerm" {
features {}
subscription_id = "YOUR_SUBSCRIPTION_ID"
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
tenant_id = "YOUR_TENANT_ID"
}
# Define the resource group
resource "azurerm_resource_group" "rg" {
name = "my-linux-vm-rg"
location = "westus2"
}
# Define the Virtual Network
resource "azurerm_virtual_network" "vnet" {
name = "my-linux-vm-vnet"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
address_space = ["10.0.0.0/16"]
}
# Define the Subnet
resource "azurerm_subnet" "subnet" {
name = "my-linux-vm-subnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}
# Define the Network Security Group
resource "azurerm_network_security_group" "nsg" {
name = "my-linux-vm-nsg"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
}
# Define the Public IP Address
resource "azurerm_public_ip" "pip" {
name = "my-linux-vm-pip"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
allocation_method = "Static"
}
# Define the Network Interface
resource "azurerm_network_interface" "nic" {
name = "my-linux-vm-nic"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
ip_configuration {
name = "ipconfig1"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.pip.id
}
}
# Define the Virtual Machine
resource "azurerm_linux_virtual_machine" "vm" {
name = "my-linux-vm"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = "Standard_B2s"
network_interface_ids = [azurerm_network_interface.nic.id]
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "20.04-LTS"
version = "Latest"
}
admin_username = "your_username"
admin_password = "your_password"
}
-
Initialize Terraform:
terraform init
-
Apply the configuration:
terraform apply -auto-approve
This command will create the specified resources in Azure, including the VM, network interface, and other required components.
-
Connect to the VM:
Once the deployment completes, you can connect to your newly created Ubuntu 20.04 VM using your preferred SSH client.
Customization and Advanced Options:
This configuration is a starting point. You can customize the VM settings further based on your specific needs:
- Size: Adjust the VM size to match your performance requirements.
- Disk configuration: Configure multiple disks, disk types, and sizes.
- Security group rules: Define network security group rules to control incoming and outgoing traffic.
- Tags: Add tags for organization and cost tracking.
Benefits of Using Terraform for Azure VM Deployment:
- Automation: Terraform automates the entire VM creation process, saving you time and effort.
- Consistency: Terraform ensures that your infrastructure is deployed consistently across environments.
- Version control: Terraform configurations can be version-controlled, providing a history of changes and making it easy to rollback to previous states.
- Scalability: Terraform allows you to easily scale your infrastructure by adding new resources with minimal effort.
- Collaboration: Terraform configurations can be shared and collaborated on, making it ideal for teams.
Conclusion:
Terraform provides a powerful and efficient way to deploy and manage Azure Linux VMs. With its declarative approach and automation capabilities, Terraform simplifies the process, reduces the risk of errors, and enables you to create and manage your cloud infrastructure effectively. By following this guide, you can confidently deploy Ubuntu 20.04 VMs on Azure with ease.
Additional Resources: