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

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-04-05 11:38:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-05 11:38:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-05 11:38:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-05 11:38:02       20 阅读

热门阅读

  1. 数据库更新两张相关联的表

    2024-04-05 11:38:02       15 阅读
  2. 【leetcode】向字符串添加空格

    2024-04-05 11:38:02       15 阅读
  3. 2024.3.17力扣每日一题——最小高度树

    2024-04-05 11:38:02       13 阅读
  4. Apache Spark 的基本概念和在大数据分析中的应用

    2024-04-05 11:38:02       14 阅读
  5. WPF如何使用 System.Windows.Forms.FolderBrowserDialog

    2024-04-05 11:38:02       16 阅读
  6. 找出字符串中所有偶数的个数

    2024-04-05 11:38:02       15 阅读
  7. 单例模式的多种写法

    2024-04-05 11:38:02       13 阅读
  8. 设计模式:单例模式六种实现

    2024-04-05 11:38:02       19 阅读
  9. 单例模式详解

    2024-04-05 11:38:02       13 阅读