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
  • 32
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EE的逆袭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值