Skip to content

NKCODE TECH GEEK ZONE

  • RSS - Posts
Menu
  • Home
  • Cloud
    • Azure
    • Alibaba
    • AWS
  • Hardware
  • Linux
  • Network
  • Security
  • Windows Client / Servers
    • SQL
    • Windows Client OS
      • Windows 10
    • Windows Servers
      • Windows 2008R2
      • Windows Server 2012R2
      • Windows Server 2016
      • Windows Server 2019
  • VMWARE
  • Free Tools
  • About Me
    • Disclaimer
Menu

Using Terraform Deploying an Azure VM

Posted on June 14, 2021

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.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

Welcome to Teck Geek Zone

Alibaba & Azure Cloud with a free trial worth $200-1200 USD Click below Cloud Providers

  • Securing Your Cloud Environment with Alibaba Cloud Firewall
  • 🚢 Sailing into the Data Age: How Cloud and IoT are Revolutionizing the Marine Industry
  • What is Azure Grafana? A Comprehensive Guide to Monitoring and Visualization
  • 🔐 How to Enable Virtualization-Based Security (VBS) for Windows Workloads in VMware Cloud Foundation and vSphere
  • Microsoft’s Azure SRE Agent: AI-Powered Reliability Engineering

Categories

  • Cloud (186)
    • Alibaba (39)
    • AWS (39)
    • Azure (114)
  • Free Tools (5)
  • Hardware (17)
  • Linux (13)
  • Network (28)
  • Security (21)
  • VMWARE (54)
  • Windows OS (44)
    • Windows 10 (7)
  • Windows Servers (69)
    • SQL (3)
    • Windows 2008R2 (7)
    • Windows Server 2012R2 (15)
    • Windows Server 2016 (20)
    • Windows Server 2019 (10)

Subscribe to our newsletter

©2025 NKCODE TECH GEEK ZONE | Design: Newspaperly WordPress Theme
 

Loading Comments...
 

    %d