Using Terraform Deploying an Azure VM

Virtual machine deployment is the primary goal of almost all automation attempts. Terraform provides grammar that is easier to read than ARM templates and allows you to create the prerequisites you want or need without having to jump between multiple portal screens.

Set Up the Command Line Interface (CLI)

  1. In the Azure Portal, in the top-left corner of the page, copy the Resource group name for later use.
  2. Click the Cloud Shell icon (>_) in the upper right.
  3. Select PowerShell.
  4. Click Show advanced settings.
  5. For Storage account, select Create new and give it a globally unique name (e.g., “cloudshellnkg” ). Copy this name for later use.
  6. For File share, select Create new and give it a name of “fileshare1”.
  7. Click Create storage.

Deploy a Ubuntu VM

This code block sets up the:

  • VNet/Subnet
  • Public IP address
  • Network interface
  • Boot diagnostic account
  • Virtual Machine
  1. Copy the following codeblock and in a text editor update the fields using the information copied earlier:

provider “azurerm” {
version = 1.38
}

# Create virtual network
resource “azurerm_virtual_network” “TFNet” {
name = “”
address_space = [“10.0.0.0/16”]
location = “East US”
resource_group_name = “”

tags = {
environment = “Terraform VNET”
}
}
# Create subnet
resource “azurerm_subnet” “tfsubnet” {
name = “default”
resource_group_name = “”
virtual_network_name = azurerm_virtual_network.TFNet.name
address_prefix = “10.0.1.0/24”
}

#Deploy Public IP
resource “azurerm_public_ip” “example” {
name = “pubip1”
location = “East US”
resource_group_name = “”
allocation_method = “Dynamic”
sku = “Basic”
}

#Create NIC
resource “azurerm_network_interface” “example” {
name = “Enter name for this NIC”
location = “East US”
resource_group_name = “”

ip_configuration {
name = “ipconfig1”
subnet_id = azurerm_subnet.tfsubnet.id
private_ip_address_allocation = “Dynamic”
public_ip_address_id = azurerm_public_ip.example.id
}
}

#Create Boot Diagnostic Account
resource “azurerm_storage_account” “sa” {
name = “Enter Name for Diagnostic Account”
resource_group_name = “”
location = “East US”
account_tier = “Standard”
account_replication_type = “LRS”

tags = {
environment = “Boot Diagnostic Storage”
CreatedBy = “Admin”
}
}

#Create Virtual Machine
resource “azurerm_virtual_machine” “example” {
name = “Enter AzureVM Name”
location = “East US”
resource_group_name = “Enter Resource Group Name”
network_interface_ids = [azurerm_network_interface.example.id]
vm_size = “Standard_B1s”
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true

storage_image_reference {
publisher = “Canonical”
offer = “UbuntuServer”
sku = “16.04-LTS”
version = “latest”
}

storage_os_disk {
name = “osdisk1”
disk_size_gb = “128”
caching = “ReadWrite”
create_option = “FromImage”
managed_disk_type = “Standard_LRS”
}

os_profile {
computer_name = “Enter Server Name”
admin_username = “vmadmin”
admin_password = “Password12345!”
}

os_profile_linux_config {
disable_password_authentication = false
}

boot_diagnostics {
enabled = “true”
storage_uri = azurerm_storage_account.sa.primary_blob_endpoint
}
}

Note: Each resource will need the resource_group_name field to be updated, and several still require a unique name. Update each “Enter Name” field with a unique name before proceeding.

  1. Once updated, save the file as “test.tf”.
  2. In the Cloud Shell, click the Upload/Download files menu and click Upload.

  1. In the Cloud Shell, initialize the working directory:
    terraform init

  2. Create the execution plan:
    terraform plan
  3. Apply the execution plan:
    terraform apply

  4. Once complete, back in the Azure Portal, click the Refresh button and review the resources that were created.

  1. Click the robot virtual machine.
  2. In the left-hand menu, under Support + Troubleshooting, click Serial console.