Terraform: Deploy Azure Load Balancer

Deploy a Public Load Balancer

In this section, we will deploy a Public Load Balancer with Backend Pool consisting of two Azure Virtual Machines. These VMs will host a simple web page configured by using a custom script extension for Nginx web service. We will perform complete deployment using Terraform.

Public Load Balancer Lab Setup

This is how our architecture will look after the deployment is completed.

Create and Deploy Terraform script

  1. Create a directory and make it as your current directory.
    mkdir load-balancer-demo
    cd load-balancer-demo
  2. Create a file named providers.tf and paste the configuration below. Here we have configured azurerm as Terraform provider for creating and managing our Azure resources.
    terraform {
      required_version = ">=0.12"

      required_providers {
        azapi = {
          source  = "azure/azapi"
          version = "~>1.5"
        }
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>2.0"
        }
        random = {
          source  = "hashicorp/random"
          version = "~>3.0"
        }
      }
    }

    provider "azurerm" {
      features {}
    }
  3. Create a file named variables.tf and paste the configuration below. We declare all the variables that we intend to use in our Terraform deployment in the variables.tf file. You could modify the default values as per your choice or naming convention for Azure resources.
    variable "resource_group_location" {
      type        = string
      default     = "eastus"
      description = "Location of the resource group."
    }

    variable "resource_group_name" {
      type        = string
      default     = "test-group"
      description = "Name of the resource group."
    }

    variable "username" {
      type        = string
      default     = "microsoft"
      description = "The username for the local account that will be created on the new VM."
    }

    variable "password" {
      type        = string
      default     = "Microsoft@123"
      description = "The passoword for the local account that will be created on the new VM."
    }

    variable "virtual_network_name" {
      type        = string
      default     = "test-vnet"
      description = "Name of the Virtual Network."
    }

    variable "subnet_name" {
      type        = string
      default     = "test-subnet"
      description = "Name of the subnet."
    }

    variable public_ip_name {
      type        = string
      default     = "test-public-ip"
      description = "Name of the Public IP."
    }

    variable network_security_group_name {
      type        = string
      default     = "test-nsg"
      description = "Name of the Network Security Group."
    }

    variable "network_interface_name" {
      type        = string
      default     = "test-nic"
      description = "Name of the Network Interface."  
    }

    variable "virtual_machine_name" {
      type        = string
      default     = "test-vm"
      description = "Name of the Virtual Machine."
    }

    variable "virtual_machine_size" {
      type        = string
      default     = "Standard_B2s"
      description = "Size or SKU of the Virtual Machine."
    }

    variable "disk_name" {
      type        = string
      default     = "test-disk"
      description = "Name of the OS disk of the Virtual Machine."
    }

    variable "redundancy_type" {
      type        = string
      default     = "Standard_LRS"
      description = "Storage redundancy type of the OS disk."
    }

    variable "load_balancer_name" {
      type        = string
      default     = "test-lb"
      description = "Name of the Load Balancer."
    }
  4. Create a file named main.tf and paste the configuration below. The main.tf is our configuration file where we use to deploy our Azure resources.
    #Create Resource Group
    resource "azurerm_resource_group" "my_resource_group" {
      location = var.resource_group_location
      name     = var.resource_group_name
    }

    # Create Virtual Network
    resource "azurerm_virtual_network" "my_virtual_network" {
      name                = var.virtual_network_name
      address_space       = ["10.0.0.0/16"]
      location            = azurerm_resource_group.my_resource_group.location
      resource_group_name = azurerm_resource_group.my_resource_group.name
    }

    # Create a subnet in the Virtual Network
    resource "azurerm_subnet" "my_subnet" {
      name                 = var.subnet_name
      resource_group_name  = azurerm_resource_group.my_resource_group.name
      virtual_network_name = azurerm_virtual_network.my_virtual_network.name
      address_prefixes     = ["10.0.1.0/24"]
    }

    # Create Network Security Group and rules
    resource "azurerm_network_security_group" "my_nsg" {
      name                = var.network_security_group_name
      location            = azurerm_resource_group.my_resource_group.location
      resource_group_name = azurerm_resource_group.my_resource_group.na
上一篇:vue watch监听的多种使用


下一篇:Redis中是如何初始化服务器的?