In this section, we will explore how to use Terraform to provision resources on Microsoft Azure. By the end of this module, you will be able to create, manage, and destroy Azure resources using Terraform.

Key Concepts

  1. Azure Provider: The Azure provider is used to interact with the many resources supported by Azure.
  2. Resource Group: A container that holds related resources for an Azure solution.
  3. Virtual Network (VNet): A representation of your own network in the cloud.
  4. Subnets: Segments of a VNet that can contain resources.
  5. Virtual Machines (VMs): Compute resources that can run applications and services.

Prerequisites

  • An active Azure subscription.
  • Azure CLI installed and configured.
  • Basic understanding of Terraform configuration files.

Step-by-Step Guide

  1. Setting Up the Azure Provider

First, you need to configure the Azure provider in your Terraform configuration file.

provider "azurerm" {
  features {}
}

  1. Creating a Resource Group

A resource group is a logical container for Azure resources.

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

  1. Creating a Virtual Network (VNet)

A VNet is a fundamental building block for your private network in Azure.

resource "azurerm_virtual_network" "example" {
  name                = "example-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

  1. Creating Subnets

Subnets are segments of a VNet that can contain resources.

resource "azurerm_subnet" "example" {
  name                 = "example-subnet"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]
}

  1. Creating a Virtual Machine (VM)

A VM is a compute resource that can run applications and services.

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_machine" "example" {
  name                  = "example-vm"
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.example.id]
  vm_size               = "Standard_DS1_v2"

  storage_os_disk {
    name              = "example-os-disk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  os_profile {
    computer_name  = "example-vm"
    admin_username = "adminuser"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

  1. Applying the Configuration

To apply the configuration and create the resources, run the following commands:

terraform init
terraform apply

Practical Exercise

Exercise: Create a simple Azure infrastructure with a resource group, a virtual network, a subnet, and a virtual machine.

  1. Create a new directory for your Terraform configuration files.
  2. Write the configuration files as shown in the examples above.
  3. Initialize Terraform using terraform init.
  4. Apply the configuration using terraform apply.

Solution:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "example-subnet"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_machine" "example" {
  name                  = "example-vm"
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.example.id]
  vm_size               = "Standard_DS1_v2"

  storage_os_disk {
    name              = "example-os-disk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  os_profile {
    computer_name  = "example-vm"
    admin_username = "adminuser"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

Common Mistakes and Tips

  • Incorrect Provider Configuration: Ensure the Azure provider is correctly configured with the necessary features.
  • Resource Naming: Use meaningful names for resources to avoid confusion.
  • Location Consistency: Ensure all resources are created in the same location to avoid latency and additional costs.
  • Security: Never hard-code sensitive information like passwords in your configuration files. Use environment variables or secret management tools.

Conclusion

In this section, you learned how to provision Azure resources using Terraform. You created a resource group, a virtual network, a subnet, and a virtual machine. This foundational knowledge will enable you to build more complex Azure infrastructures using Terraform. In the next module, we will explore provisioning resources on Google Cloud Platform (GCP).

© Copyright 2024. All rights reserved