Azure Virtual Machine Course

Table of Content

  1. Terraform Code for Azure Virtual Machine
  2. Azure Course Slides

1. Terraform Code for Azure Virtual Machine

 1## main.tf
 2
 3resource "azurerm_linux_virtual_machine" "dev_eu_north_ubuntu_vm" {
 4  name                            = "dev-eu-north-ubuntu2204-vm"
 5  resource_group_name             = azurerm_resource_group.dev_eu_north_rg.name
 6  location                        = azurerm_resource_group.dev_eu_north_rg.location
 7  size                            = "Standard_F2"
 8  admin_username                  = "rahulwagh"
 9  #admin_password                  = "G<7€YraRgk_7lnksE}yu37`Fe"
10  disable_password_authentication = true
11  admin_ssh_key {
12    username       = "rahulwagh"
13    public_key     = file("/Users/rahulwagh/.ssh/dev-vm-ssh-key-pair.pub")
14  }
15
16  network_interface_ids = [
17    azurerm_network_interface.dev_eu_north_nic.id,
18  ]
19
20  os_disk {
21    caching              = "ReadWrite"
22    storage_account_type = "Standard_LRS"
23  }
24
25  source_image_reference {
26    publisher = "Canonical"
27    offer     = "0001-com-ubuntu-server-jammy"
28    sku       = "22_04-lts"
29    version   = "latest"
30  }
31}
32
33resource "azurerm_network_interface" "dev_eu_north_nic" {
34  name                = "dev-eu-north-nic"
35  location            = azurerm_resource_group.dev_eu_north_rg.location
36  resource_group_name = azurerm_resource_group.dev_eu_north_rg.name
37
38  ip_configuration {
39    name                          = "internal"
40    subnet_id                     = azurerm_subnet.dev_eu_north_public_subnet.id
41    private_ip_address_allocation = "Dynamic"
42    public_ip_address_id          = azurerm_public_ip.dev_eu_north_static_public_ip.id
43  }
44  depends_on = [azurerm_network_security_group.dev_eu_north_ssh_nsg, azurerm_public_ip.dev_eu_north_static_public_ip]
45}
46
47resource "azurerm_public_ip" "dev_eu_north_static_public_ip" {
48  name                = "dev-eu-north-static-public-ip"
49  resource_group_name = azurerm_resource_group.dev_eu_north_rg.name
50  location            = azurerm_resource_group.dev_eu_north_rg.location
51  allocation_method   = "Static"
52}
53
54# Create Network Security Group and rule
55resource "azurerm_network_security_group" "dev_eu_north_ssh_nsg" {
56  name                = "dev-eu-north-ssh-nsg"
57  location            = azurerm_resource_group.dev_eu_north_rg.location
58  resource_group_name = azurerm_resource_group.dev_eu_north_rg.name
59
60  security_rule {
61    name                       = "SSH"
62    priority                   = 1001
63    direction                  = "Inbound"
64    access                     = "Allow"
65    protocol                   = "Tcp"
66    source_port_range          = "*"
67    destination_port_range     = "22"
68    source_address_prefix      = "*"
69    destination_address_prefix = "*"
70  }
71
72  tags = {
73    environment = "Terraform Demo"
74  }
75}
76
77#Connect the security group to the network interface
78resource "azurerm_network_interface_security_group_association" "dev_eu_north_nsg_nic_association" {
79  network_interface_id      = azurerm_network_interface.dev_eu_north_nic.id
80  network_security_group_id = azurerm_network_security_group.dev_eu_north_ssh_nsg.id
81  depends_on = [azurerm_network_interface.dev_eu_north_nic]
82}
 1## networking.tf
 2resource "azurerm_virtual_network" "dev_eu_north_vnet" {
 3  name                = "dev_eu_north_vnet"
 4  resource_group_name = azurerm_resource_group.dev_eu_north_rg.name
 5  location            = azurerm_resource_group.dev_eu_north_rg.location
 6  address_space       = ["10.0.0.0/16"]
 7}
 8
 9resource "azurerm_subnet" "dev_eu_north_public_subnet" {
10  name                 = "dev-eu-north-public-subnet"
11  resource_group_name  = azurerm_resource_group.dev_eu_north_rg.name
12  virtual_network_name = azurerm_virtual_network.dev_eu_north_vnet.name
13  address_prefixes     = ["10.0.1.0/24"]
14}
15
16resource "azurerm_route_table" "dev_eu_north_public_rt" {
17  name                = "dev-eu-north-public-rt"
18  location            = azurerm_resource_group.dev_eu_north_rg.location
19  resource_group_name = azurerm_resource_group.dev_eu_north_rg.name
20
21  route {
22    name           = "public_internet"
23    address_prefix = "0.0.0.0/0"
24    next_hop_type  = "Internet"
25  }
26}
27
28resource "azurerm_subnet_route_table_association" "dev_eu_north_public_subent_rt_association" {
29  subnet_id      = azurerm_subnet.dev_eu_north_public_subnet.id
30  route_table_id = azurerm_route_table.dev_eu_north_public_rt.id
31} 
 1## provider.tf
 2terraform {
 3  required_providers {
 4    azurerm = {
 5      source  = "hashicorp/azurerm"
 6      version = "=3.0.0"
 7    }
 8  }
 9}
10
11provider "azurerm" {
12  features {}
13} 
1## resources-group.tf
2# Create a resource group
3resource "azurerm_resource_group" "dev_eu_north_rg" {
4  name     = "dev-eu-north-rg"
5  location = "North Europe"
6} 

2. Azure Intro

Azure Course Intro


Azure Course Intro


Azure Course Intro


2. Azure Zones

Azure Course Intro


Azure Course Intro


3. Azure Signup

Azure Course Intro


4. Azure Account and Subscription

Azure Course Intro


Azure Course Intro


9. Azure Resource Group

Azure Course Intro


Azure Course Intro


10. Azure CLI and PowerShell

Azure Course Intro


Azure Course Intro


Azure Course Intro


11. Azure Storage Account


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


12. Azure Compute - Virtual machine

Azure Course Intro


Azure Course Intro


Azure Course Intro


13. Azure ARM Template

Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


13. Azure Terraform

Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


14. Azure Network Security Group

Azure Course Intro


Azure Course Intro


Azure Course Intro


15. Azure Virtual Machine

Azure Course Intro


Azure Course Intro


Azure Course Intro


16. Azure Loadbalancer

Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


17. Azure Virtual Machine Scale Set

Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Azure Course Intro


Posts in this series