20 ‐ Azure ‐ Terraform - SanjeevOCI/Azure GitHub Wiki

Steps to Create Azure Resources Using Terraform in Azure Cloud Shell


1. Open Azure Cloud Shell

  1. Log in to the Azure Portal.
  2. Click on the Cloud Shell icon in the top-right corner.
  3. Select Bash as the shell environment.

Terraform_1

Terraform_2

Terraform_3

Terraform_4

Terraform_5


2. Initialize Terraform in Azure Cloud Shell

  1. Verify Terraform is pre-installed by running:
    terraform --version
    
  2. Create a working directory for your Terraform files:
    mkdir terraform-azure && cd terraform-azure
    

20_Terraform_Resource_Creation_1


3. Create a Terraform Configuration File

  1. Create a file named main.tf:
    vi main.tf
    

20_Terraform_Resource_Creation_2

  1. Add the following configuration to create Azure resources:
provider "azurerm" {
  features {}
  subscription_id = "024da94d-4ab2-429f-9b42-a588276a504b"
}

variable "resource_group_name" {
  default = "hub1_rg"
}

variable "location" {
  default = "East US"
}

variable "vnet_name" {
  default = "hub1_vnet"
}

variable "subnet_name" {
  default = "hub1_pub_subnet"
}

# Create Resource Group
resource "azurerm_resource_group" "rg" {
  name     = var.resource_group_name
  location = var.location
}

# Create Virtual Network
resource "azurerm_virtual_network" "vnet" {
  name                = var.vnet_name
  address_space       = ["172.1.0.0/24"]
  location            = var.location
  resource_group_name = azurerm_resource_group.rg.name
}

# Create Subnet
resource "azurerm_subnet" "subnet" {
  name                 = var.subnet_name
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["172.1.0.0/27"]
}

# Public IP
resource "azurerm_public_ip" "vm_public_ip" {
  name                = "linux-vm1-public-ip"
  location            = var.location
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Static"
  sku                 = "Basic"
}

# NIC
resource "azurerm_network_interface" "vm_nic" {
  name                = "linuxserver1-nic"
  location            = var.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.vm_public_ip.id
  }
}

# Linux VM
resource "azurerm_linux_virtual_machine" "vm" {
  name                            = "LinuxServer1"
  resource_group_name             = azurerm_resource_group.rg.name
  location                        = var.location
  size                            = "Standard_B1ls"
  admin_username                  = "azureuser"
  admin_password                  = "Testuser@2025"
  disable_password_authentication = false

  network_interface_ids = [azurerm_network_interface.vm_nic.id]

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

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

20_Terraform_Resource_Creation_3

  1. Get the subscription id by navigating to Subscriptions directory

20_Terraform_Resource_Creation_4

  1. Save and exit the editor.

4. Create a Terraform Output File

  1. Create a file named output.tf:
    vi output.tf
    

20_Terraform_Resource_Creation_4_1

  1. Add the following in the output.tf file:
output "resource_group_name" {
  value       = azurerm_resource_group.rg.name
  description = "The name of the resource group"
}

output "virtual_network_name" {
  value       = azurerm_virtual_network.vnet.name
  description = "The name of the virtual network"
}

output "public_subnet_name" {
  value       = azurerm_subnet.subnet.name
  description = "The name of the public subnet"
}

output "linux_vm_name" {
  value       = azurerm_linux_virtual_machine.vm.name
  description = "The name of the Linux virtual machine"
}

output "linux_vm_public_ip" {
  value       = azurerm_public_ip.vm_public_ip.ip_address
  description = "The public IP address of the Linux VM"
}

output "linux_vm_private_ip" {
  value       = azurerm_network_interface.vm_nic.ip_configuration[0].private_ip_address
  description = "The private IP address of the Linux VM"
}

output "linux_vm_nic_id" {
  value       = azurerm_network_interface.vm_nic.id
  description = "The ID of the network interface attached to the Linux VM"
}


5. Initialize Terraform

  1. Run the following command to initialize Terraform and download the Azure provider:
    terraform init
    

20_Terraform_Resource_Creation_5


6. Plan the Deployment

  1. Generate an execution plan to preview the resources Terraform will create:
    terraform plan
    

20_Terraform_Resource_Creation_6_1

20_Terraform_Resource_Creation_6_2


7. Apply the Configuration

  1. Deploy the resources to Azure:

    terraform apply
    
  2. Confirm the deployment by typing yes when prompted.

20_Terraform_Resource_Creation_7_1

20_Terraform_Resource_Creation_7_2


8. Verify the Resources

Check the created resources in the Azure Portal:

20_Terraform_Resource_Creation_8_1

20_Terraform_Resource_Creation_8_2

20_Terraform_Resource_Creation_8_3


9. Clean Up Resources

  1. To delete the resources created by Terraform, run:
    terraform destroy
    
  2. Confirm the destruction by typing yes when prompted.

20_Terraform_Resource_Creation_9_1

20_Terraform_Resource_Creation_9_2


Key Notes

  • Azure Cloud Shell comes pre-installed with Terraform, so no additional setup is required.
  • Always use terraform plan to preview changes before applying them.
  • Store your Terraform files in a version control system like GitHub for better management.

This step-by-step guide ensures you can quickly create and manage Azure resources using Terraform in Azure Cloud Shell.