|
| 1 | +--- |
| 2 | +title: "Quickstart: Create a public load balancer - Terraform" |
| 3 | +titleSuffix: Azure Load Balancer |
| 4 | +description: This quickstart shows how to create a load balancer by using Terraform. |
| 5 | +services: load-balancer |
| 6 | +author: hisriram96 |
| 7 | +manager: vikasbagde |
| 8 | +ms.service: load-balancer |
| 9 | +ms.topic: quickstart |
| 10 | +ms.workload: infrastructure-services |
| 11 | +ms.date: 01/02/2024 |
| 12 | +ms.author: sriramiyer |
| 13 | +ms.custom: devx-track-terraform |
| 14 | +#Customer intent: I want to create a load balancer by using Terraform so that I can load balance internet traffic to VMs. |
| 15 | +--- |
| 16 | + |
| 17 | +# Quickstart: Create a public load balancer to load balance VMs using Terraform |
| 18 | + |
| 19 | +This quickstart shows you how to deploy a standard load balancer to load balance virtual machines using Terraform. |
| 20 | + |
| 21 | +[!INCLUDE [About Terraform](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 22 | + |
| 23 | +> [!div class="checklist"] |
| 24 | +> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) |
| 25 | +> * Create an Azure Virtual Network using [azurerm_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) |
| 26 | +> * Create an Azure subnet using [azurerm_subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet) |
| 27 | +> * Create an Azure public IP using [azurerm_public_ip](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip) |
| 28 | +> * Create an Azure Load Balancer using [azurerm_lb](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/lb) |
| 29 | +> * Create an Azure network interface using [azurerm_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) |
| 30 | +> * Create an Azure network interface load balancer backend address pool association using [azurerm_network_interface_backend_address_pool_association](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface_backend_address_pool_association) |
| 31 | +> * Create an Azure Linux Virtual Machine using [azurerm_linux_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) |
| 32 | +> * Create an Azure Virtual Machine Extension using [azurerm_virtual_machine_extension](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_extension) |
| 33 | +
|
| 34 | +## Prerequisites |
| 35 | + |
| 36 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 37 | + |
| 38 | +## Implement the Terraform code |
| 39 | + |
| 40 | +> [!NOTE] |
| 41 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 42 | +
|
| 43 | +1. Create a directory in which to test the sample Terraform code and make it the current directory. |
| 44 | + |
| 45 | +1. Create a file named `providers.tf` and insert the following code: |
| 46 | + |
| 47 | + ``` |
| 48 | + terraform { |
| 49 | + required_version = ">=0.12" |
| 50 | + |
| 51 | + required_providers { |
| 52 | + azapi = { |
| 53 | + source = "azure/azapi" |
| 54 | + version = "~>1.5" |
| 55 | + } |
| 56 | + azurerm = { |
| 57 | + source = "hashicorp/azurerm" |
| 58 | + version = "~>2.0" |
| 59 | + } |
| 60 | + random = { |
| 61 | + source = "hashicorp/random" |
| 62 | + version = "~>3.0" |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + provider "azurerm" { |
| 68 | + features {} |
| 69 | + } |
| 70 | + ``` |
| 71 | +
|
| 72 | +1. Create a file named `main.tf` and insert the following code: |
| 73 | +
|
| 74 | + ``` |
| 75 | + resource "random_string" "my_resource_group" { |
| 76 | + length = 8 |
| 77 | + upper = false |
| 78 | + special = false |
| 79 | + } |
| 80 | +
|
| 81 | + # Create Resource Group |
| 82 | + resource "azurerm_resource_group" "my_resource_group" { |
| 83 | + name = "test-group-${random_string.my_resource_group.result}" |
| 84 | + location = var.resource_group_location |
| 85 | + } |
| 86 | + |
| 87 | + # Create Virtual Network |
| 88 | + resource "azurerm_virtual_network" "my_virtual_network" { |
| 89 | + name = var.virtual_network_name |
| 90 | + address_space = ["10.0.0.0/16"] |
| 91 | + location = azurerm_resource_group.my_resource_group.location |
| 92 | + resource_group_name = azurerm_resource_group.my_resource_group.name |
| 93 | + } |
| 94 | + |
| 95 | + # Create a subnet in the Virtual Network |
| 96 | + resource "azurerm_subnet" "my_subnet" { |
| 97 | + name = var.subnet_name |
| 98 | + resource_group_name = azurerm_resource_group.my_resource_group.name |
| 99 | + virtual_network_name = azurerm_virtual_network.my_virtual_network.name |
| 100 | + address_prefixes = ["10.0.1.0/24"] |
| 101 | + } |
| 102 | + |
| 103 | + # Create Network Security Group and rules |
| 104 | + resource "azurerm_network_security_group" "my_nsg" { |
| 105 | + name = var.network_security_group_name |
| 106 | + location = azurerm_resource_group.my_resource_group.location |
| 107 | + resource_group_name = azurerm_resource_group.my_resource_group.name |
| 108 | + |
| 109 | + security_rule { |
| 110 | + name = "web" |
| 111 | + priority = 1008 |
| 112 | + direction = "Inbound" |
| 113 | + access = "Allow" |
| 114 | + protocol = "Tcp" |
| 115 | + source_port_range = "*" |
| 116 | + destination_port_range = "80" |
| 117 | + source_address_prefix = "*" |
| 118 | + destination_address_prefix = "10.0.1.0/24" |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + # Associate the Network Security Group to the subnet |
| 123 | + resource "azurerm_subnet_network_security_group_association" "my_nsg_association" { |
| 124 | + subnet_id = azurerm_subnet.my_subnet.id |
| 125 | + network_security_group_id = azurerm_network_security_group.my_nsg.id |
| 126 | + } |
| 127 | + |
| 128 | + # Create Public IP |
| 129 | + resource "azurerm_public_ip" "my_public_ip" { |
| 130 | + name = var.public_ip_name |
| 131 | + location = azurerm_resource_group.my_resource_group.location |
| 132 | + resource_group_name = azurerm_resource_group.my_resource_group.name |
| 133 | + allocation_method = "Static" |
| 134 | + sku = "Standard" |
| 135 | + } |
| 136 | + |
| 137 | + # Create Network Interface |
| 138 | + resource "azurerm_network_interface" "my_nic" { |
| 139 | + count = 2 |
| 140 | + name = "${var.network_interface_name}${count.index}" |
| 141 | + location = azurerm_resource_group.my_resource_group.location |
| 142 | + resource_group_name = azurerm_resource_group.my_resource_group.name |
| 143 | + |
| 144 | + ip_configuration { |
| 145 | + name = "ipconfig${count.index}" |
| 146 | + subnet_id = azurerm_subnet.my_subnet.id |
| 147 | + private_ip_address_allocation = "Dynamic" |
| 148 | + primary = true |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + # Associate Network Interface to the Backend Pool of the Load Balancer |
| 153 | + resource "azurerm_network_interface_backend_address_pool_association" "my_nic_lb_pool" { |
| 154 | + count = 2 |
| 155 | + network_interface_id = azurerm_network_interface.my_nic[count.index].id |
| 156 | + ip_configuration_name = "ipconfig${count.index}" |
| 157 | + backend_address_pool_id = azurerm_lb_backend_address_pool.my_lb_pool.id |
| 158 | + } |
| 159 | + |
| 160 | + # Create Virtual Machine |
| 161 | + resource "azurerm_linux_virtual_machine" "my_vm" { |
| 162 | + count = 2 |
| 163 | + name = "${var.virtual_machine_name}${count.index}" |
| 164 | + location = azurerm_resource_group.my_resource_group.location |
| 165 | + resource_group_name = azurerm_resource_group.my_resource_group.name |
| 166 | + network_interface_ids = [azurerm_network_interface.my_nic[count.index].id] |
| 167 | + size = var.virtual_machine_size |
| 168 | + |
| 169 | + os_disk { |
| 170 | + name = "${var.disk_name}${count.index}" |
| 171 | + caching = "ReadWrite" |
| 172 | + storage_account_type = var.redundancy_type |
| 173 | + } |
| 174 | + |
| 175 | + source_image_reference { |
| 176 | + publisher = "Canonical" |
| 177 | + offer = "0001-com-ubuntu-server-jammy" |
| 178 | + sku = "22_04-lts-gen2" |
| 179 | + version = "latest" |
| 180 | + } |
| 181 | + |
| 182 | + admin_username = var.username |
| 183 | + admin_password = var.password |
| 184 | + disable_password_authentication = false |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + # Enable virtual machine extension and install Nginx |
| 189 | + resource "azurerm_virtual_machine_extension" "my_vm_extension" { |
| 190 | + count = 2 |
| 191 | + name = "Nginx" |
| 192 | + virtual_machine_id = azurerm_linux_virtual_machine.my_vm[count.index].id |
| 193 | + publisher = "Microsoft.Azure.Extensions" |
| 194 | + type = "CustomScript" |
| 195 | + type_handler_version = "2.0" |
| 196 | + |
| 197 | + settings = <<SETTINGS |
| 198 | + { |
| 199 | + "commandToExecute": "sudo apt-get update && sudo apt-get install nginx -y && echo \"Hello World from $(hostname)\" > /var/www/html/index.html && sudo systemctl restart nginx" |
| 200 | + } |
| 201 | + SETTINGS |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + # Create Public Load Balancer |
| 206 | + resource "azurerm_lb" "my_lb" { |
| 207 | + name = var.load_balancer_name |
| 208 | + location = azurerm_resource_group.my_resource_group.location |
| 209 | + resource_group_name = azurerm_resource_group.my_resource_group.name |
| 210 | + sku = "Standard" |
| 211 | + |
| 212 | + frontend_ip_configuration { |
| 213 | + name = var.public_ip_name |
| 214 | + public_ip_address_id = azurerm_public_ip.my_public_ip.id |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + resource "azurerm_lb_backend_address_pool" "my_lb_pool" { |
| 219 | + loadbalancer_id = azurerm_lb.my_lb.id |
| 220 | + name = "test-pool" |
| 221 | + } |
| 222 | + |
| 223 | + resource "azurerm_lb_probe" "my_lb_probe" { |
| 224 | + resource_group_name = azurerm_resource_group.my_resource_group.name |
| 225 | + loadbalancer_id = azurerm_lb.my_lb.id |
| 226 | + name = "test-probe" |
| 227 | + port = 80 |
| 228 | + } |
| 229 | + |
| 230 | + resource "azurerm_lb_rule" "my_lb_rule" { |
| 231 | + resource_group_name = azurerm_resource_group.my_resource_group.name |
| 232 | + loadbalancer_id = azurerm_lb.my_lb.id |
| 233 | + name = "test-rule" |
| 234 | + protocol = "Tcp" |
| 235 | + frontend_port = 80 |
| 236 | + backend_port = 80 |
| 237 | + disable_outbound_snat = true |
| 238 | + frontend_ip_configuration_name = var.public_ip_name |
| 239 | + probe_id = azurerm_lb_probe.my_lb_probe.id |
| 240 | + backend_address_pool_ids = [azurerm_lb_backend_address_pool.my_lb_pool.id] |
| 241 | + } |
| 242 | + |
| 243 | + resource "azurerm_lb_outbound_rule" "my_lboutbound_rule" { |
| 244 | + resource_group_name = azurerm_resource_group.my_resource_group.name |
| 245 | + name = "test-outbound" |
| 246 | + loadbalancer_id = azurerm_lb.my_lb.id |
| 247 | + protocol = "Tcp" |
| 248 | + backend_address_pool_id = azurerm_lb_backend_address_pool.my_lb_pool.id |
| 249 | + |
| 250 | + frontend_ip_configuration { |
| 251 | + name = var.public_ip_name |
| 252 | + } |
| 253 | + } |
| 254 | + ``` |
| 255 | +
|
| 256 | +1. Create a file named `variables.tf` and insert the following code: |
| 257 | +
|
| 258 | + ``` |
| 259 | + variable "resource_group_location" { |
| 260 | + type = string |
| 261 | + default = "eastus" |
| 262 | + description = "Location of the resource group." |
| 263 | + } |
| 264 | + |
| 265 | + variable "username" { |
| 266 | + type = string |
| 267 | + default = "microsoft" |
| 268 | + description = "The username for the local account that will be created on the new VM." |
| 269 | + } |
| 270 | + |
| 271 | + variable "password" { |
| 272 | + type = string |
| 273 | + default = "Microsoft@123" |
| 274 | + description = "The passoword for the local account that will be created on the new VM." |
| 275 | + } |
| 276 | + |
| 277 | + variable "virtual_network_name" { |
| 278 | + type = string |
| 279 | + default = "test-vnet" |
| 280 | + description = "Name of the Virtual Network." |
| 281 | + } |
| 282 | + |
| 283 | + variable "subnet_name" { |
| 284 | + type = string |
| 285 | + default = "test-subnet" |
| 286 | + description = "Name of the subnet." |
| 287 | + } |
| 288 | + |
| 289 | + variable public_ip_name { |
| 290 | + type = string |
| 291 | + default = "test-public-ip" |
| 292 | + description = "Name of the Public IP." |
| 293 | + } |
| 294 | + |
| 295 | + variable network_security_group_name { |
| 296 | + type = string |
| 297 | + default = "test-nsg" |
| 298 | + description = "Name of the Network Security Group." |
| 299 | + } |
| 300 | + |
| 301 | + variable "network_interface_name" { |
| 302 | + type = string |
| 303 | + default = "test-nic" |
| 304 | + description = "Name of the Network Interface." |
| 305 | + } |
| 306 | + |
| 307 | + variable "virtual_machine_name" { |
| 308 | + type = string |
| 309 | + default = "test-vm" |
| 310 | + description = "Name of the Virtual Machine." |
| 311 | + } |
| 312 | + |
| 313 | + variable "virtual_machine_size" { |
| 314 | + type = string |
| 315 | + default = "Standard_B2s" |
| 316 | + description = "Size or SKU of the Virtual Machine." |
| 317 | + } |
| 318 | + |
| 319 | + variable "disk_name" { |
| 320 | + type = string |
| 321 | + default = "test-disk" |
| 322 | + description = "Name of the OS disk of the Virtual Machine." |
| 323 | + } |
| 324 | + |
| 325 | + variable "redundancy_type" { |
| 326 | + type = string |
| 327 | + default = "Standard_LRS" |
| 328 | + description = "Storage redundancy type of the OS disk." |
| 329 | + } |
| 330 | + |
| 331 | + variable "load_balancer_name" { |
| 332 | + type = string |
| 333 | + default = "test-lb" |
| 334 | + description = "Name of the Load Balancer." |
| 335 | + } |
| 336 | + ``` |
| 337 | +
|
| 338 | +1. Create a file named `outputs.tf` and insert the following code: |
| 339 | +
|
| 340 | + ``` |
| 341 | + output "public_ip_address" { |
| 342 | + value = "http://${azurerm_public_ip.my_public_ip.ip_address}" |
| 343 | + } |
| 344 | + ``` |
| 345 | +
|
| 346 | +## Initialize Terraform |
| 347 | +
|
| 348 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 349 | +
|
| 350 | +## Create a Terraform execution plan |
| 351 | +
|
| 352 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 353 | +
|
| 354 | +## Apply a Terraform execution plan |
| 355 | +
|
| 356 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 357 | +
|
| 358 | +## Verify the results |
| 359 | +
|
| 360 | +1. When you apply the execution plan, Terraform displays the frontend public IP address. If you've cleared the screen, you can retrieve that value with the following Terraform command: |
| 361 | +
|
| 362 | + ```console |
| 363 | + echo $(terraform output -raw public_ip_address) |
| 364 | + ``` |
| 365 | +
|
| 366 | +1. Paste the public IP address into the address bar of your web browser. The custom VM page of the Nginx web server is displayed in the browser. |
| 367 | +
|
| 368 | +## Clean up resources |
| 369 | +
|
| 370 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 371 | +
|
| 372 | +## Troubleshoot Terraform on Azure |
| 373 | +
|
| 374 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot) |
| 375 | +
|
| 376 | +## Next steps |
| 377 | +
|
| 378 | +In this quickstart, you: |
| 379 | +
|
| 380 | +* Created an Azure Load Balancer |
| 381 | +* Attached 2 VMs to the load balancer |
| 382 | +* Tested the load balancer |
| 383 | +
|
| 384 | +To learn more about Azure Load Balancer, continue to: |
| 385 | +> [!div class="nextstepaction"] |
| 386 | +> [What is Azure Load Balancer?](load-balancer-overview.md) |
0 commit comments