๐ Post Content
๐ Deploy Azure Load Balancer and Virtual Machines using Terraform
In this blog, Iโll demonstrate how to use Terraform to deploy a basic Azure infrastructure including:
- A resource group
- A virtual network and subnet
- Two Windows virtual machines
- A public Load Balancer with a backend pool and health probe
๐ง Files Overview
File Name | Description |
---|---|
main.tf |
Defines all resources |
variables.tf |
Declares variables |
terraform.tfvars |
Sets variable values |
outputs.tf |
Outputs the public IP of Load Balancer |
๐ main.tf
hcl
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.location
}
resource "azurerm_virtual_network" "vnet" {
name = "example-vnet"
address_space = ["10.0.0.0/16"]
location = var.location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_subnet" "subnet" {
name = "example-subnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_network_interface" "nic" {
count = 2
name = "example-nic-${count.index}"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_windows_virtual_machine" "vm" {
count = 2
name = "example-vm-${count.index}"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
size = "Standard_B1s"
admin_username = var.admin_username
admin_password = var.admin_password
network_interface_ids = [azurerm_network_interface.nic[count.index].id]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter"
version = "latest"
}
}
resource "azurerm_public_ip" "lb_public_ip" {
name = "example-lb-pip"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Static"
sku = "Basic"
}
resource "azurerm_lb" "lb" {
name = "example-lb"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
sku = "Basic"
frontend_ip_configuration {
name = "PublicIPAddress"
public_ip_address_id = azurerm_public_ip.lb_public_ip.id
}
}
resource "azurerm_lb_backend_address_pool" "bepool" {
name = "backend-pool"
resource_group_name = azurerm_resource_group.rg.name
loadbalancer_id = azurerm_lb.lb.id
}
resource "azurerm_lb_probe" "probe" {
name = "http-probe"
resource_group_name = azurerm_resource_group.rg.name
loadbalancer_id = azurerm_lb.lb.id
protocol = "Tcp"
port = 80
}
resource "azurerm_lb_rule" "lbrule" {
name = "http-rule"
resource_group_name = azurerm_resource_group.rg.name
loadbalancer_id = azurerm_lb.lb.id
protocol = "Tcp"
frontend_port = 80
backend_port = 80
frontend_ip_configuration_name = "PublicIPAddress"
backend_address_pool_id = azurerm_lb_backend_address_pool.bepool.id
probe_id = azurerm_lb_probe.probe.id
}
๐ variables.tf
variable "location" {
description = "Azure region"
default = "japaneast"
}
variable "resource_group_name" {
description = "Resource Group name"
}
variable "admin_username" {
description = "VM admin username"
}
variable "admin_password" {
description = "VM admin password"
sensitive = true
}
๐ terraform.tfvars
resource_group_name = "rg-lb-vm-demo"
admin_username = "azureuser"
admin_password = "P@ssw0rd1234!"
๐ outputs.tf
output "public_ip" {
value = azurerm_public_ip.lb_public_ip.ip_address
}
โถ๏ธ Deploy Steps
1.Open terminal and navigate to your project folder
2.Initialize Terraform:
terraform init
3.Preview the plan:
terraform plan
4.Apply the configuration:
terraform apply
Once the deployment is done, Terraform will output a public IP. You can access this in your browser to test the Load Balancer after setting up web services on each VM.
โ
Whatโs Next
In the next post, Iโll show how to automatically install Nginx or IIS on the VM using remote-exec provisioner, so the VMs will instantly become web servers after deployment.
Stay tuned!
Thanks for reading ๐
If you liked this article, feel free to โญ๏ธ the repo or leave a comment!
Top comments (0)