Terraform by HashiCorp is an infrastructure-as-code tool. It allows infrastructure automation to be deployed via code. With Terraform, infrastructure provisioning has been simplified by using a simple, human-readable syntax.

Prerequisites

  • An active Azure subscription
  • Azure CLI
  • Visual Studio Code

 

Terraform Workflow

The following image depicts a Terraform provider workflow to be used to deploy your resources

It may be easier to understand the image above by assigning commands to each step.

Step / Command Action
Init / terraform init Obtains the current directory so that other commands can be run from it.
Validate / terraform validate Validates the configuration
Plan / terraform plan Provides a list of changes required by the current configuration
Apply / terraform apply Executes Terraform plans.
Destroy / terraform destroy Destroy all remote objects managed by a Terraform configuration.

 

How install Terraform

Below is the list of the tools required for installing Terraform provider

  • Azure CLI
  • VS Code Editor
  • Terraform plugin for VS Code
  • Git Client

Quickstart: Install and Configure Terraform

Terraform on Azure

In this section, you will read about the Terraform providers that you will be able to use to deploy and manage Azure resources.

Overview of Terraform on Azure

Terraform Providers for Azure

Azure infrastructure can be managed by several Terraform providers:

  • AzureRM: Provide help to deploy Azure resources, such as virtual machines, storage accounts, and networking interfaces.
  • Azure AD: Provide access to Azure Active Directory resources such as groups, users, service principals, and applications.
  • AzAPI : Utilize the Azure Resource Manager APIs directly to manage Azure resources and functionality. In addition to the AzureRM provider, this provider also provides the ability to manage Azure resources that aren’t yet available.
  • AzureDevops : Provide management of Azure DevOps resources, such as agents, repositories, projects, pipelines, and queries.
  • Azure Stack : Provide management of Azure Stack resources such as virtual machines, DNS, VNets, and storage.

Terraform Syntax Example

The example below shows how you can create an Azure Resource group, with an Azure VM and all related resources, i.e., VNet, PIP, Storage, etc.

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.37.0"
    }
  }
}
provider "azurerm" {
  features {}
}

#Create a Resource Group
resource "azurerm_resource_group" "main" {

name = "res-coz-rg-westeurope"
location = "westeurope"

}

#Create a Virtual Network
resource "azurerm_virtual_network" "main" {

name = "res-coz-vnet-westeurope"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
address_space = ["10.0.0.0/21"]

}

#Create subnets

resource "azurerm_subnet" "main" {

    name = "res-coz-subnet-web-westeurope"
    virtual_network_name = azurerm_virtual_network.main.name
    resource_group_name = azurerm_resource_group.main.name
    address_prefixes = ["10.0.1.0/24"]

}

#Create network interface card (NIC)

resource "azurerm_network_interface" "internal" {

    name = "res-coz-nic-int-westeurope"
    location = azurerm_resource_group.main.location
    resource_group_name = azurerm_resource_group.main.name
    
    ip_configuration {

        name = "internal"
        subnet_id = azurerm_subnet.main.id
        private_ip_address_allocation = "Dynamic"

    }

}

#Create windows virtual machine

resource "azurerm_windows_virtual_machine" "main" {
  name = "res-coz-vm-we"
  resource_group_name = azurerm_resource_group.main.name
  location            = azurerm_resource_group.main.location
  size                = "Standard_B2s"
  admin_username      = "username-example"
  admin_password      = "############"
  network_interface_ids = [
    azurerm_network_interface.internal.id
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2019-Datacenter"
    version   = "latest"
  }
}

resource "azurerm_managed_disk" "main" {
  name                 = "disk-westeurope"
  location             = azurerm_resource_group.main.location
  resource_group_name  = azurerm_resource_group.main.name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb         = "1024"

  tags = {
    environment = "staging"
  }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Share This