TECH

Free Hosting(VPS) in Microsoft Azure using Terraform

This article is about creating a free VPS in Microsoft Azure using Terraform. You need to have a valid educational email address and verify to get Azure credit. We can use VPS for hosting desired web applications.


    BY SISHIR WAGLE
    15 JAN 2022 • 2 MIN READ
Terraform in Microsoft Azure

This article is about creating a free VPS in Microsoft Azure without any credit card. You need to have a valid educational email address and verify to get Azure credit. We can use VPS for hosting desired web applications. 

In the previous article, we created VPS in Microsoft Azure using GUI (Graphical User Interface). However, it is a time-consuming method, and a simple mistake can result in credit being overused. Terraform lets you define configuration in a script that allows you to create, rebuild and track changes to your configuration. You need to install Azure CLI and terraform. Check this link to install Azure CLI and terraform.


In this article, we will create the following resources using Terraform.

For this tutorial, we will use Ubuntu VPS but feel free to use another operating system as per your wish.


Step 1 :

Create a folder and create a file named vps.tf and paste the following lines of code.

# Configure the Microsoft Azure Provider
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "~>2.0"
    }
  }
}
provider "azurerm" {
  features {}
}

# Create a resource group if it doesn't exist
resource "azurerm_resource_group" "myterraformgroup" {
    name     = "Demo-ResourceGroup"
    location = "eastus"

    tags = {
        environment = "Demo"
    }
}

# Create virtual network
resource "azurerm_virtual_network" "myterraformnetwork" {
    name                = "Demo-VirtualNetwork"
    address_space       = ["10.0.0.0/16"]
    location            = "eastus"
    resource_group_name = azurerm_resource_group.myterraformgroup.name

    tags = {
        environment = "Demo"
    }
}

# Create subnet
resource "azurerm_subnet" "myterraformsubnet" {
    name                 = "Demo-Subnet"
    resource_group_name  = azurerm_resource_group.myterraformgroup.name
    virtual_network_name = azurerm_virtual_network.myterraformnetwork.name
    address_prefixes       = ["10.0.1.0/24"]
}

# Create public IPs
resource "azurerm_public_ip" "myterraformpublicip" {
    name                         = "Demo-PublicIP"
    location                     = "eastus"
    resource_group_name          = azurerm_resource_group.myterraformgroup.name
    allocation_method            = "Dynamic"

    tags = {
        environment = "Demo"
    }
}

# Create Network Security Group and rule
resource "azurerm_network_security_group" "myterraformnsg" {
    name                = "Demo-NetworkSecurityGroup"
    location            = "eastus"
    resource_group_name = azurerm_resource_group.myterraformgroup.name

    security_rule {
        name                       = "SSH"
        priority                   = 1001
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    }

    tags = {
        environment = "Demo"
    }
}

# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
    name                      = "Demo-NIC"
    location                  = "eastus"
    resource_group_name       = azurerm_resource_group.myterraformgroup.name

    ip_configuration {
        name                          = "Demo-NicConfiguration"
        subnet_id                     = azurerm_subnet.myterraformsubnet.id
        private_ip_address_allocation = "Dynamic"
        public_ip_address_id          = azurerm_public_ip.myterraformpublicip.id
    }

    tags = {
        environment = "Demo"
    }
}

# Connect the security group to the network interface
resource "azurerm_network_interface_security_group_association" "example" {
    network_interface_id      = azurerm_network_interface.myterraformnic.id
    network_security_group_id = azurerm_network_security_group.myterraformnsg.id
}

# Generate random text for a unique storage account name
resource "random_id" "randomId" {
    keepers = {
        # Generate a new ID only when a new resource group is defined
        resource_group = azurerm_resource_group.myterraformgroup.name
    }

    byte_length = 8
}

# Create storage account for boot diagnostics

resource "azurerm_storage_account" "mystorageaccount" {
    name                        = "diag${random_id.randomId.hex}"
    resource_group_name         = azurerm_resource_group.myterraformgroup.name
    location                    = "eastus"
    account_tier                = "Standard"
    account_replication_type    = "LRS"

    tags = {
        environment = "Demo"
    }
}


# Create (and display) an SSH key
resource "tls_private_key" "example_ssh" {
  algorithm = "RSA"
  rsa_bits = 4096
}
output "tls_private_key" { 
    value = tls_private_key.example_ssh.private_key_pem 
    sensitive = true
}

// Saves the private SSH key to a file
resource "local_file" "private_key" {
  content         = tls_private_key.example_ssh.private_key_pem
  filename        = "Demo-SSH-key.pem"
  file_permission = "0600"
}

# Create virtual machine
resource "azurerm_linux_virtual_machine" "Demo-Ubuntu-VM" {
    name                  = "Demo-Ubuntu"
    location              = "eastus"
    resource_group_name   = azurerm_resource_group.myterraformgroup.name
    network_interface_ids = [azurerm_network_interface.myterraformnic.id]
    size                  = "Standard_B1s"

    os_disk {
        name              = "Demo-Ubuntu-Disk"
        caching           = "ReadWrite"
        storage_account_type = "Premium_LRS"
        disk_size_gb = 30
    }

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

    computer_name  = "Demo"
    admin_username = "Demo"
    disable_password_authentication = true

    admin_ssh_key {
        username       = "Demo"
        public_key     = tls_private_key.example_ssh.public_key_openssh
    }

    boot_diagnostics {
        storage_account_uri = azurerm_storage_account.mystorageaccount.primary_blob_endpoint
    }

    tags = {
        environment = "Demo"
    }
}

Step 2 :

Initialize Terraform 

terraform init

Step 3 :

Create a Terraform execution plan

terraform plan -out main.tfplan

Step 4 :

Apply Terraform execution plan

terraform apply main.tfplan

After the process has been completed, you can see an SSH file in your directory. Use that to log in to VM.


Tags


tech azure Deployment


Comments


warning   You need to Sign Up to Comment

Related Blogs

See all    
Machine Learning Project: Heart Disease Prediction System (HDPS) using Logistic Regression from Scratch
Create a comment and reply system in Django.
Free Hosting(VPS) in Microsoft Azure using Azure for students