Learn how to easily create and configure an Azure Linux VM running Ubuntu 20.04 using infrastructure as code with Terraform.
This guide provides a step-by-step approach to deploying a Linux virtual machine (VM) on Microsoft Azure using Terraform. We will cover the essential configurations required to set up the VM, including creating a resource group, defining network resources, and establishing secure access to the instance. By following these instructions, you'll be able to leverage Terraform's infrastructure-as-code capabilities to efficiently provision and manage your Azure VM.
Install Terraform: Download and install Terraform from the official website (https://developer.hashicorp.com/terraform/downloads).
Authenticate to Azure: Use the Azure CLI or other methods to authenticate your Terraform scripts to your Azure subscription.
Define Azure Provider: In your Terraform configuration file (e.g., main.tf
), specify the Azure provider and your desired Azure region.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}
Create a Resource Group: Define a resource group to organize your Azure resources.
resource "azurerm_resource_group" "example" {
name = "my-resource-group"
location = "West Europe"
}
Define Network Resources: Create a virtual network and subnet for your VM.
resource "azurerm_virtual_network" "example" {
name = "my-network"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
address_space = ["10.0.0.0/16"]
}
resource "azurerm_subnet" "example" {
name = "my-subnet"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.1.0/24"]
}
Create a Public IP: If you need to access the VM from the internet, create a public IP address.
resource "azurerm_public_ip" "example" {
name = "my-public-ip"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
allocation_method = "Dynamic"
}
Define Network Interface: Create a network interface and attach it to the subnet and public IP.
resource "azurerm_network_interface" "example" {
name = "my-nic"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.example.id
}
}
Create the Linux VM: Define the azurerm_linux_virtual_machine
resource, specifying the Ubuntu 20.04 image, size, authentication details, and network settings.
resource "azurerm_linux_virtual_machine" "example" {
name = "my-vm"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = "Standard_B2s"
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
network_interface_ids = [azurerm_network_interface.example.id]
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "20.04-LTS"
version = "latest"
}
ssh_keys {
path = "~/.ssh/id_rsa.pub"
key_data = file("~/.ssh/id_rsa.pub")
}
}
Apply Terraform Configuration: Run terraform init
to initialize your Terraform project, then terraform apply
to create the resources.
Access Your VM: Once the VM is created, you can SSH into it using the public IP address and the SSH key pair you specified.
This Terraform code sets up basic infrastructure on Azure. It provisions a resource group, a virtual network with a subnet, a public IP address, and a network interface. Then, it creates a Linux virtual machine running Ubuntu 20.04 LTS, attaches it to the network, and configures SSH access using a provided public key.
# Configure the Azure Provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}
# Create a Resource Group
resource "azurerm_resource_group" "example" {
name = "my-resource-group"
location = "West Europe"
}
# Create a Virtual Network
resource "azurerm_virtual_network" "example" {
name = "my-network"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
address_space = ["10.0.0.0/16"]
}
# Create a Subnet
resource "azurerm_subnet" "example" {
name = "my-subnet"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.1.0/24"]
}
# Create a Public IP Address
resource "azurerm_public_ip" "example" {
name = "my-public-ip"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
allocation_method = "Dynamic"
}
# Create a Network Interface
resource "azurerm_network_interface" "example" {
name = "my-nic"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.example.id
}
}
# Create a Linux Virtual Machine
resource "azurerm_linux_virtual_machine" "example" {
name = "my-vm"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = "Standard_B2s"
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
network_interface_ids = [azurerm_network_interface.example.id]
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "20.04-LTS"
version = "latest"
}
# Replace with your actual SSH key path
ssh_keys {
path = "~/.ssh/id_rsa.pub"
key_data = file("~/.ssh/id_rsa.pub")
}
}
Before running the code:
~/.ssh/id_rsa.pub
with the path to your public key file.Steps to deploy:
main.tf
.terraform init
to initialize the Terraform project.terraform apply
and confirm the deployment by typing yes
.Terraform will create the resources in your Azure subscription. Once the deployment is complete, you can SSH into your Linux VM using the public IP address assigned to it and the private key associated with the public key you provided.
General:
Security:
Cost Optimization:
Customization:
Troubleshooting:
terraform apply -verbose
) for detailed information on resource creation and any errors encountered.Best Practices:
This guide provides a concise overview of deploying a Linux VM on Azure using Terraform.
Steps:
Preparation:
Terraform Configuration:
main.tf
file.Deployment:
terraform init
.terraform apply
.Access:
This summary outlines the essential steps for deploying a Linux VM on Azure with Terraform. For detailed code snippets and explanations, refer to the original article.
By following the steps outlined in this guide, you can effectively utilize Terraform to deploy and manage a Linux VM on Azure. This approach not only simplifies the provisioning process but also promotes infrastructure as code, enhancing consistency, repeatability, and scalability in your cloud deployments. Remember to consider the additional notes and best practices mentioned to optimize your VM configuration for security, cost-effectiveness, and customization based on your specific requirements.