Skip to content

Commit 08b9078

Browse files
authored
Merge pull request #297484 from TomArcherMsft/101-azure-load-balancer-public
101-azure-load-balancer-public
2 parents 78a0b41 + febf552 commit 08b9078

File tree

1 file changed

+34
-310
lines changed

1 file changed

+34
-310
lines changed
Lines changed: 34 additions & 310 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
2-
title: "Quickstart: Create a public load balancer - Terraform"
2+
title: 'Quickstart: Create a public load balancer - Terraform'
33
titleSuffix: Azure Load Balancer
44
description: This quickstart shows how to create a load balancer by using Terraform.
55
services: load-balancer
66
author: mbender-ms
77
manager: kumudD
88
ms.service: azure-load-balancer
99
ms.topic: quickstart
10-
ms.date: 01/10/2025
10+
ms.date: 04/01/2025
1111
ms.author: mbender
1212
ms.custom: devx-track-terraform
1313
#Customer intent: I want to create a load balancer by using Terraform so that I can load balance internet traffic to VMs.
@@ -31,316 +31,34 @@ This quickstart shows you how to deploy a standard load balancer to load balance
3131
> * Create an Azure Virtual Machine Extension using [azurerm_virtual_machine_extension](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_extension)
3232
3333
## Prerequisites
34+
- Create an Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
3435

3536
- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure)
3637

3738
## Implement the Terraform code
3839

39-
> [!NOTE]
40-
> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform)
40+
The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-azure-load-balancer-public). You can view the log file containing the [test results from current and previous versions of Terraform](https://github.com/Azure/terraform/tree/master/quickstart/101-azure-load-balancer-public/TestRecord.md). See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform)
4141

42-
1. Create a directory in which to test the sample Terraform code and make it the current directory.
42+
1. Create a directory in which to test and run the sample Terraform code, and make it the current directory.
4343

44-
1. Create a file named `providers.tf` and insert the following code:
44+
1. Create a file named `providers.tf` and insert the following code.
45+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-load-balancer-public/providers.tf":::
4546

46-
```
47-
terraform {
48-
  required_version = ">=0.12"
49-
50-
  required_providers {
51-
    azapi = {
52-
      source  = "azure/azapi"
53-
      version = "~>1.5"
54-
    }
55-
    azurerm = {
56-
      source  = "hashicorp/azurerm"
57-
      version = "~>2.0"
58-
    }
59-
    random = {
60-
      source  = "hashicorp/random"
61-
      version = "~>3.0"
62-
    }
63-
  }
64-
}
65-
66-
provider "azurerm" {
67-
  features {}
68-
}
69-
```
70-
71-
1. Create a file named `main.tf` and insert the following code:
47+
1. Create a file named `main.tf` and insert the following code.
48+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-load-balancer-public/main.tf":::
7249

73-
```
74-
resource "random_string" "my_resource_group" {
75-
length = 8
76-
upper = false
77-
special = false
78-
}
79-
80-
# Create Resource Group
81-
resource "azurerm_resource_group" "my_resource_group" {
82-
name = "test-group-${random_string.my_resource_group.result}"
83-
location = var.resource_group_location
84-
}
85-
86-
# Create Virtual Network
87-
resource "azurerm_virtual_network" "my_virtual_network" {
88-
  name                = var.virtual_network_name
89-
  address_space       = ["10.0.0.0/16"]
90-
  location            = azurerm_resource_group.my_resource_group.location
91-
  resource_group_name = azurerm_resource_group.my_resource_group.name
92-
}
93-
94-
# Create a subnet in the Virtual Network
95-
resource "azurerm_subnet" "my_subnet" {
96-
  name                 = var.subnet_name
97-
  resource_group_name  = azurerm_resource_group.my_resource_group.name
98-
  virtual_network_name = azurerm_virtual_network.my_virtual_network.name
99-
  address_prefixes     = ["10.0.1.0/24"]
100-
}
101-
102-
# Create Network Security Group and rules
103-
resource "azurerm_network_security_group" "my_nsg" {
104-
  name                = var.network_security_group_name
105-
  location            = azurerm_resource_group.my_resource_group.location
106-
  resource_group_name = azurerm_resource_group.my_resource_group.name
107-
108-
  security_rule {
109-
    name                       = "web"
110-
    priority                   = 1008
111-
    direction                  = "Inbound"
112-
    access                     = "Allow"
113-
    protocol                   = "Tcp"
114-
    source_port_range          = "*"
115-
    destination_port_range     = "80"
116-
    source_address_prefix      = "*"
117-
    destination_address_prefix = "10.0.1.0/24"
118-
  }
119-
}
120-
121-
# Associate the Network Security Group to the subnet
122-
resource "azurerm_subnet_network_security_group_association" "my_nsg_association" {
123-
  subnet_id                 = azurerm_subnet.my_subnet.id
124-
  network_security_group_id = azurerm_network_security_group.my_nsg.id
125-
}
126-
127-
# Create Public IP
128-
resource "azurerm_public_ip" "my_public_ip" {
129-
  name                = var.public_ip_name
130-
  location            = azurerm_resource_group.my_resource_group.location
131-
  resource_group_name = azurerm_resource_group.my_resource_group.name
132-
  allocation_method   = "Static"
133-
  sku                 = "Standard"
134-
}
135-
136-
# Create Network Interface
137-
resource "azurerm_network_interface" "my_nic" {
138-
  count               = 2
139-
  name                = "${var.network_interface_name}${count.index}"
140-
  location            = azurerm_resource_group.my_resource_group.location
141-
  resource_group_name = azurerm_resource_group.my_resource_group.name
142-
143-
  ip_configuration {
144-
    name                          = "ipconfig${count.index}"
145-
    subnet_id                     = azurerm_subnet.my_subnet.id
146-
    private_ip_address_allocation = "Dynamic"
147-
    primary = true
148-
  }
149-
}
150-
151-
# Associate Network Interface to the Backend Pool of the Load Balancer
152-
resource "azurerm_network_interface_backend_address_pool_association" "my_nic_lb_pool" {
153-
  count                   = 2
154-
  network_interface_id    = azurerm_network_interface.my_nic[count.index].id
155-
  ip_configuration_name   = "ipconfig${count.index}"
156-
  backend_address_pool_id = azurerm_lb_backend_address_pool.my_lb_pool.id
157-
}
158-
159-
# Create Virtual Machine
160-
resource "azurerm_linux_virtual_machine" "my_vm" {
161-
  count                 = 2
162-
  name                  = "${var.virtual_machine_name}${count.index}"
163-
  location              = azurerm_resource_group.my_resource_group.location
164-
  resource_group_name   = azurerm_resource_group.my_resource_group.name
165-
  network_interface_ids = [azurerm_network_interface.my_nic[count.index].id]
166-
  size                  = var.virtual_machine_size
167-
168-
  os_disk {
169-
    name                 = "${var.disk_name}${count.index}"
170-
    caching              = "ReadWrite"
171-
    storage_account_type = var.redundancy_type
172-
  }
173-
174-
  source_image_reference {
175-
    publisher = "Canonical"
176-
    offer     = "0001-com-ubuntu-server-jammy"
177-
    sku       = "22_04-lts-gen2"
178-
    version   = "latest"
179-
  }
180-
181-
admin_username                  = var.username
182-
  admin_password                  = var.password
183-
  disable_password_authentication = false
184-
185-
}
186-
187-
# Enable virtual machine extension and install Nginx
188-
resource "azurerm_virtual_machine_extension" "my_vm_extension" {
189-
  count                = 2
190-
  name                 = "Nginx"
191-
  virtual_machine_id   = azurerm_linux_virtual_machine.my_vm[count.index].id
192-
  publisher            = "Microsoft.Azure.Extensions"
193-
  type                 = "CustomScript"
194-
  type_handler_version = "2.0"
195-
196-
  settings = <<SETTINGS
197-
 {
198-
  "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"
199-
 }
200-
SETTINGS
201-
202-
}
203-
204-
# Create Public Load Balancer
205-
resource "azurerm_lb" "my_lb" {
206-
  name                = var.load_balancer_name
207-
  location            = azurerm_resource_group.my_resource_group.location
208-
  resource_group_name = azurerm_resource_group.my_resource_group.name
209-
  sku                 = "Standard"
210-
211-
  frontend_ip_configuration {
212-
    name                 = var.public_ip_name
213-
    public_ip_address_id = azurerm_public_ip.my_public_ip.id
214-
  }
215-
}
216-
217-
resource "azurerm_lb_backend_address_pool" "my_lb_pool" {
218-
  loadbalancer_id      = azurerm_lb.my_lb.id
219-
  name                 = "test-pool"
220-
}
221-
222-
resource "azurerm_lb_probe" "my_lb_probe" {
223-
  resource_group_name = azurerm_resource_group.my_resource_group.name
224-
  loadbalancer_id     = azurerm_lb.my_lb.id
225-
  name                = "test-probe"
226-
  port                = 80
227-
}
228-
229-
resource "azurerm_lb_rule" "my_lb_rule" {
230-
  resource_group_name            = azurerm_resource_group.my_resource_group.name
231-
  loadbalancer_id                = azurerm_lb.my_lb.id
232-
  name                           = "test-rule"
233-
  protocol                       = "Tcp"
234-
  frontend_port                  = 80
235-
  backend_port                   = 80
236-
  disable_outbound_snat          = true
237-
  frontend_ip_configuration_name = var.public_ip_name
238-
  probe_id                       = azurerm_lb_probe.my_lb_probe.id
239-
  backend_address_pool_ids       = [azurerm_lb_backend_address_pool.my_lb_pool.id]
240-
}
241-
242-
resource "azurerm_lb_outbound_rule" "my_lboutbound_rule" {
243-
  resource_group_name     = azurerm_resource_group.my_resource_group.name
244-
  name                    = "test-outbound"
245-
  loadbalancer_id         = azurerm_lb.my_lb.id
246-
  protocol                = "Tcp"
247-
  backend_address_pool_id = azurerm_lb_backend_address_pool.my_lb_pool.id
248-
249-
  frontend_ip_configuration {
250-
    name = var.public_ip_name
251-
  }
252-
}
253-
```
50+
1. Create a file named `variables.tf` and insert the following code.
51+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-load-balancer-public/variables.tf":::
25452

255-
1. Create a file named `variables.tf` and insert the following code:
53+
1. Create a file named `outputs.tf` and insert the following code.
54+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-load-balancer-public/outputs.tf":::
25655

257-
```
258-
variable "resource_group_location" {
259-
  type        = string
260-
  default     = "eastus"
261-
  description = "Location of the resource group."
262-
}
263-
264-
variable "username" {
265-
  type        = string
266-
  default     = "microsoft"
267-
  description = "The username for the local account that will be created on the new VM."
268-
}
269-
270-
variable "password" {
271-
  type        = string
272-
  default     = "Microsoft@123"
273-
  description = "The password for the local account that will be created on the new VM."
274-
}
275-
276-
variable "virtual_network_name" {
277-
  type        = string
278-
  default     = "test-vnet"
279-
  description = "Name of the Virtual Network."
280-
}
281-
282-
variable "subnet_name" {
283-
  type        = string
284-
  default     = "test-subnet"
285-
  description = "Name of the subnet."
286-
}
287-
288-
variable public_ip_name {
289-
  type        = string
290-
  default     = "test-public-ip"
291-
  description = "Name of the Public IP."
292-
}
293-
294-
variable network_security_group_name {
295-
  type        = string
296-
  default     = "test-nsg"
297-
  description = "Name of the Network Security Group."
298-
}
299-
300-
variable "network_interface_name" {
301-
  type        = string
302-
  default     = "test-nic"
303-
  description = "Name of the Network Interface."  
304-
}
305-
306-
variable "virtual_machine_name" {
307-
  type        = string
308-
  default     = "test-vm"
309-
  description = "Name of the Virtual Machine."
310-
}
311-
312-
variable "virtual_machine_size" {
313-
  type        = string
314-
  default     = "Standard_B2s"
315-
  description = "Size or SKU of the Virtual Machine."
316-
}
317-
318-
variable "disk_name" {
319-
  type        = string
320-
  default     = "test-disk"
321-
  description = "Name of the OS disk of the Virtual Machine."
322-
}
323-
324-
variable "redundancy_type" {
325-
  type        = string
326-
  default     = "Standard_LRS"
327-
  description = "Storage redundancy type of the OS disk."
328-
}
329-
330-
variable "load_balancer_name" {
331-
  type        = string
332-
  default     = "test-lb"
333-
  description = "Name of the Load Balancer."
334-
}
335-
```
336-
337-
1. Create a file named `outputs.tf` and insert the following code:
338-
339-
```
340-
output "public_ip_address" {
341-
value = "http://${azurerm_public_ip.my_public_ip.ip_address}"
342-
}
343-
```
56+
> [!IMPORTANT]
57+
> If you're using the 4.x azurerm provider, you must [explicitly specify the Azure subscription ID](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/4.0-upgrade-guide#specifying-subscription-id-is-now-mandatory) to authenticate to Azure before running the Terraform commands.
58+
>
59+
> One way to specify the Azure subscription ID without putting it in the `providers` block is to specify the subscription ID in an environment variable named `ARM_SUBSCRIPTION_ID`.
60+
>
61+
> For more information, see the [Azure provider reference documentation](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#argument-reference).
34462
34563
## Initialize Terraform
34664

@@ -356,10 +74,22 @@ This quickstart shows you how to deploy a standard load balancer to load balance
35674

35775
## Verify the results
35876

359-
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:
77+
1. Display the Azure resource group name.
36078

36179
```console
362-
echo $(terraform output -raw public_ip_address)
80+
terraform output -raw resource_group_name
81+
```
82+
83+
1. Optionally, display the VM (virtual machine) password.
84+
85+
```console
86+
terraform output -raw vm_password
87+
```
88+
89+
1. Display the public IP address.
90+
91+
```console
92+
terraform output -raw public_ip_address
36393
```
36494

36595
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.
@@ -374,12 +104,6 @@ This quickstart shows you how to deploy a standard load balancer to load balance
374104

375105
## Next steps
376106

377-
In this quickstart, you:
378-
379-
* Created an Azure Load Balancer
380-
* Attached 2 VMs to the load balancer
381-
* Tested the load balancer
382-
383-
To learn more about Azure Load Balancer, continue to:
384107
> [!div class="nextstepaction"]
385108
> [What is Azure Load Balancer?](load-balancer-overview.md)
109+

0 commit comments

Comments
 (0)