Debug School

Rajesh Kumar
Rajesh Kumar

Posted on

Terraform Azure — Hands-On Assignments 2 - Linux

Assignment 2: Deploy Linux VM and Install Nginx Using Terraform Provisioner

Objective: Create an Ubuntu Linux VM in Azure and automatically configure Nginx Web Server using Terraform.

Requirements:

Create an Azure Resource Group, VNet, Subnet, NSG, Public IP, and Ubuntu Linux VM.

Use a Terraform provisioner to install Nginx.

Enable and start Nginx using systemctl.

Allow HTTP traffic on port 80 through the NSG.

Test Nginx by accessing http:// in a browser.

Submission: Write complete, standalone Terraform code and paste it into the comment section, along with the successful test result.

Top comments (5)

Collapse
 
gauri_thorat_c64a5d5bdfa0 profile image
Gauri Thorat •

terraform {
required_version = ">= 1.5.0"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 4.0"
}
}
}

provider "azurerm" {
features {}

subscription_id = "092c8f39-7113-46c4-8cce-d26a6afd9f67"
}

Resource Group

resource "azurerm_resource_group" "rg" {
name = "rg-linux-nginx-demo-gt"
location = "Central India"
}

Virtual Network

resource "azurerm_virtual_network" "vnet" {
name = "vnet-linux-demo"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
address_space = ["10.0.0.0/16"]
}

Subnet

resource "azurerm_subnet" "subnet" {
name = "subnet-linux-demo"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}

Network Security Group

resource "azurerm_network_security_group" "nsg" {
name = "nsg-linux-demo"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

security_rule {
name = "Allow-HTTP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "80"
source_address_prefix = "Internet"
destination_address_prefix = "
"
}
}

Public IP

resource "azurerm_public_ip" "public_ip" {
name = "pip-linux-demo"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Static"
sku = "Standard"
}

Network Interface

resource "azurerm_network_interface" "nic" {
name = "nic-linux-demo"
location = azurerm_resource_group.rg.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.public_ip.id
}
}

Associate NSG with NIC

resource "azurerm_network_interface_security_group_association" "nsg_association" {
network_interface_id = azurerm_network_interface.nic.id
network_security_group_id = azurerm_network_security_group.nsg.id
}

Ubuntu Linux VM

resource "azurerm_linux_virtual_machine" "vm" {
name = "vm-linux-nginx"
computer_name = "nginxvm"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = "Standard_B2s"

admin_username = "azureuser"
admin_password = "Linux@12345678!"
disable_password_authentication = false

network_interface_ids = [
azurerm_network_interface.nic.id
]

os_disk {
name = "osdisk-linux-nginx"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts-gen2"
version = "latest"
}
}

Terraform Provisioner to install Nginx

resource "terraform_data" "install_nginx" {
triggers_replace = [
azurerm_linux_virtual_machine.vm.id
]

provisioner "local-exec" {
interpreter = ["PowerShell", "-Command"]

command = <<-EOT
  az vm run-command invoke `
    --resource-group "${azurerm_resource_group.rg.name}" `
    --name "${azurerm_linux_virtual_machine.vm.name}" `
    --command-id RunShellScript `
    --scripts "sudo apt-get update && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y nginx && sudo systemctl enable nginx && sudo systemctl start nginx && sudo systemctl is-active nginx"
EOT
Enter fullscreen mode Exit fullscreen mode

}

depends_on = [
azurerm_linux_virtual_machine.vm,
azurerm_network_interface_security_group_association.nsg_association
]
}

Outputs

output "public_ip_address" {
value = azurerm_public_ip.public_ip.ip_address
}

output "nginx_url" {
value = "http://${azurerm_public_ip.public_ip.ip_address}"
}

Collapse
 
tariq_alghawanmeh_68388ac profile image
tariq alghawanmeh • • Edited


resource "azurerm_resource_group" "lab" {
  name     = var.resource_group_name
  location = var.location
}

resource "azurerm_virtual_network" "main" {
  name                = "vnet-ubuntu-lab"
  address_space       = ["10.10.0.0/16"]
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name
}

resource "azurerm_subnet" "main" {
  name                 = "subnet-ubuntu"
  resource_group_name  = azurerm_resource_group.lab.name
  virtual_network_name = azurerm_virtual_network.main.name
  address_prefixes     = ["10.10.1.0/24"]
}

resource "azurerm_public_ip" "ubuntu" {
  name                = "pip-ubuntu"
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name

  allocation_method = "Static"
  sku               = "Standard"
}

resource "azurerm_network_security_group" "main" {
  name                = "nsg-tg"
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name
}

resource "azurerm_network_security_rule" "access" {
  for_each = {
    ssh = {
      priority = 100
      port     = "22"
      source   = var.allowed_ssh_cidr
    }

    http = {
      priority = 110
      port     = "80"
      source   = "*"
    }
  }

  name                       = "allow-${each.key}"
  priority                   = each.value.priority
  direction                  = "Inbound"
  access                     = "Allow"
  protocol                   = "Tcp"
  source_port_range          = "*"
  destination_port_range     = each.value.port
  source_address_prefix      = each.value.source
  destination_address_prefix = "*"

  resource_group_name         = azurerm_resource_group.lab.name
  network_security_group_name = azurerm_network_security_group.main.name
}

resource "azurerm_subnet_network_security_group_association" "main" {
  subnet_id                 = azurerm_subnet.main.id
  network_security_group_id = azurerm_network_security_group.main.id
}

resource "azurerm_network_interface" "ubuntu" {
  name                = "nic-ubuntu2604-01"
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.main.id
    private_ip_address_allocation = "Dynamic"

    public_ip_address_id = azurerm_public_ip.ubuntu.id
  }

  depends_on = [
    azurerm_subnet_network_security_group_association.main
  ]
}

resource "azurerm_linux_virtual_machine" "ubuntu" {
  name                = "vm-ubuntu-tg"
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name

  size           = var.vm_size
  admin_username = var.admin_username
  admin_password = var.admin_password

  disable_password_authentication = false

  network_interface_ids = [
    azurerm_network_interface.ubuntu.id
  ]

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

  source_image_reference {
    publisher = "Canonical"
    offer     = "ubuntu-26_04-lts"
    sku       = "server"
    version   = "latest"
  }

  depends_on = [
    azurerm_network_security_rule.access,
    azurerm_subnet_network_security_group_association.main
  ]

  provisioner "remote-exec" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get install -y nginx",
      "sudo systemctl enable nginx",
      "sudo systemctl start nginx"
    ]

    connection {
      type     = "ssh"
      host     = azurerm_public_ip.ubuntu.ip_address
      user     = var.admin_username
      password = var.admin_password
    }

  }
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
sakshi_gadekar profile image
Sakshi Gadekar •

main.tf

`# --------------------------------------------------

1. Resource Group

--------------------------------------------------

resource "azurerm_resource_group" "sakshi" {
name = var.resource_group_name
location = var.location

tags = {
Owner = "Sakshi"
Assignment = "Terraform-D2-ASS2"
Purpose = "Ubuntu-Nginx-Lab"
}
}

--------------------------------------------------

2. Virtual Network

--------------------------------------------------

resource "azurerm_virtual_network" "sakshi" {
name = "vnet-sakshi-d2-ass2"
address_space = ["10.30.0.0/16"]
location = azurerm_resource_group.sakshi.location
resource_group_name = azurerm_resource_group.sakshi.name

tags = {
Owner = "Sakshi"
}
}

--------------------------------------------------

3. Subnet

--------------------------------------------------

resource "azurerm_subnet" "sakshi" {
name = "subnet-sakshi-d2-ass2"
resource_group_name = azurerm_resource_group.sakshi.name
virtual_network_name = azurerm_virtual_network.sakshi.name
address_prefixes = ["10.30.1.0/24"]
}

--------------------------------------------------

4. Network Security Group

--------------------------------------------------

resource "azurerm_network_security_group" "sakshi" {
name = "nsg-sakshi-d2-ass2"
location = azurerm_resource_group.sakshi.location
resource_group_name = azurerm_resource_group.sakshi.name

# Allow HTTP traffic to the Nginx web server
security_rule {
name = "Allow-HTTP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "80"
source_address_prefix = "
"
destination_address_prefix = "*"
}

# Allow SSH only from your specified public IP range
security_rule {
name = "Allow-SSH-From-My-IP"
priority = 110
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "22"
source_address_prefix = var.allowed_source_cidr
destination_address_prefix = "
"
}

tags = {
Owner = "Sakshi"
}
}

--------------------------------------------------

5. Public IP

--------------------------------------------------

resource "azurerm_public_ip" "sakshi" {
name = "pip-sakshi-d2-ass2"
location = azurerm_resource_group.sakshi.location
resource_group_name = azurerm_resource_group.sakshi.name

allocation_method = "Static"
sku = "Standard"

tags = {
Owner = "Sakshi"
}
}

--------------------------------------------------

6. Network Interface

--------------------------------------------------

resource "azurerm_network_interface" "sakshi" {
name = "nic-sakshi-d2-ass2"
location = azurerm_resource_group.sakshi.location
resource_group_name = azurerm_resource_group.sakshi.name

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

tags = {
Owner = "Sakshi"
}
}

--------------------------------------------------

7. Associate NSG with NIC

--------------------------------------------------

resource "azurerm_network_interface_security_group_association" "sakshi" {
network_interface_id = azurerm_network_interface.sakshi.id
network_security_group_id = azurerm_network_security_group.sakshi.id
}

--------------------------------------------------

8. Ubuntu Linux VM

--------------------------------------------------

resource "azurerm_linux_virtual_machine" "sakshi" {
name = "sakshi-d2a2-vm"
computer_name = "sakshi-d2a2-vm"
resource_group_name = azurerm_resource_group.sakshi.name
location = azurerm_resource_group.sakshi.location
size = var.vm_size

admin_username = var.admin_username
disable_password_authentication = true

network_interface_ids = [
azurerm_network_interface.sakshi.id
]

admin_ssh_key {
username = var.admin_username

public_key = file(
  pathexpand("~/.ssh/terraform_sakshi.pub")
)
Enter fullscreen mode Exit fullscreen mode

}

os_disk {
name = "osdisk-sakshi-d2-ass2"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts-gen2"
version = "latest"
}

tags = {
Owner = "Sakshi"
Assignment = "Terraform-D2-ASS2"
Purpose = "Nginx-Web-Server"
}

# Make sure the NSG is associated before
# Terraform attempts to connect and install Nginx.
depends_on = [
azurerm_network_interface_security_group_association.sakshi
]

# ------------------------------------------------
# 9. Remote Connection
# ------------------------------------------------

connection {
type = "ssh"
host = azurerm_public_ip.sakshi.ip_address
user = var.admin_username
private_key = file(
pathexpand("~/.ssh/terraform_sakshi")
)
timeout = "5m"
}

# ------------------------------------------------
# 10. Terraform Provisioner
# ------------------------------------------------

provisioner "remote-exec" {
script = "${path.module}/install-nginx.sh"
}
}`

install-nginx.sh

!/usr/bin/env bash

set -euo pipefail

echo "Starting Nginx installation for Sakshi's assignment..."

Update the package list

sudo apt-get update -y

Install Nginx

sudo apt-get install -y nginx

Configure Nginx to start automatically on boot

sudo systemctl enable nginx

Start Nginx immediately

sudo systemctl start nginx

Create a custom webpage

sudo tee /var/www/html/index.html > /dev/null <<'HTML'
<!DOCTYPE html>


Sakshi - Terraform Assignment 2


Hello from Sakshi's Azure Linux VM!


Nginx has been installed using Terraform remote-exec.


Assignment: Terraform Azure - Hands-On Assignment 2




HTML

Verify that Nginx is running and enabled

sudo systemctl is-active --quiet nginx
sudo systemctl is-enabled --quiet nginx

Verify that Nginx responds locally

curl --fail --silent --show-error 127.0.0.1/ > /dev/null

echo "Nginx installation completed successfully."
sudo systemctl status nginx --no-pager

Collapse
 
anirudha_ingole_9c8e0ffd0 profile image
Anirudha ingole •

resource "azurerm_resource_group" "rg" {
name = "rg-nginx-demo"
location = var.location
}

resource "azurerm_virtual_network" "vnet" {
name = "vnet-nginx"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

address_space = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "subnet" {
name = "subnet-nginx"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name

address_prefixes = ["10.0.1.0/24"]
}

resource "azurerm_public_ip" "pip" {
name = "pip-nginx"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

allocation_method = "Static"
sku = "Standard"
}

resource "azurerm_network_security_group" "nsg" {
name = "nsg-nginx"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}

SSH

resource "azurerm_network_security_rule" "ssh" {
name = "Allow-SSH"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"

source_port_range = "*"
destination_port_range = "22"

source_address_prefix = ""
destination_address_prefix = "
"

resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
}

HTTP

resource "azurerm_network_security_rule" "http" {
name = "Allow-HTTP"
priority = 110
direction = "Inbound"
access = "Allow"
protocol = "Tcp"

source_port_range = "*"
destination_port_range = "80"

source_address_prefix = ""
destination_address_prefix = "
"

resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
}

resource "azurerm_subnet_network_security_group_association" "assoc" {
subnet_id = azurerm_subnet.subnet.id
network_security_group_id = azurerm_network_security_group.nsg.id
}

resource "azurerm_network_interface" "nic" {
name = "nic-nginx"
location = azurerm_resource_group.rg.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.pip.id
}
}

resource "azurerm_linux_virtual_machine" "vm" {
name = "vm-nginx"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

size = "Standard_B2s"
admin_username = var.admin_username
admin_password = var.admin_password

disable_password_authentication = false

network_interface_ids = [
azurerm_network_interface.nic.id
]

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

source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts-gen2"
version = "latest"
}
}

resource "null_resource" "nginx_install" {

depends_on = [
azurerm_linux_virtual_machine.vm
]

connection {
type = "ssh"
host = azurerm_public_ip.pip.ip_address
user = var.admin_username
password = var.admin_password
timeout = "5m"
}

# local-exec
provisioner "local-exec" {
command = "echo Installing Nginx on ${azurerm_public_ip.pip.ip_address}"
}

# file
provisioner "file" {
source = "install-nginx.sh"
destination = "/tmp/install-nginx.sh"
}

# remote-exec
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/install-nginx.sh",
"sudo /tmp/install-nginx.sh"
]
}
}

Collapse
 
loni_hatimuria_48462a7278 profile image
Loni Hatimuria •

main.tf
terraform {
required_version = ">= 1.15.0, < 2.0.0"

required_providers {
  azurerm = {
    source  = "hashicorp/azurerm"
  version = "~> 5.9.0"
}
Enter fullscreen mode Exit fullscreen mode

}
}

provider "azurerm" {
features {}

subscription_id = var.subscription_id
}

1. Resource Group

resource "azurerm_resource_group" "nginx" {
name = "rg-linux-nginx-demo"
location = var.location
}

2. Virtual Network

resource "azurerm_virtual_network" "nginx" {
name = "vnet-linux-nginx"
location = azurerm_resource_group.nginx.location
resource_group_name = azurerm_resource_group.nginx.name

address_space = ["10.40.0.0/16"]
}

3. Subnet

resource "azurerm_subnet" "nginx" {
name = "snet-linux-nginx"
resource_group_name = azurerm_resource_group.nginx.name
virtual_network_name = azurerm_virtual_network.nginx.name

address_prefixes = ["10.40.1.0/24"]
}

4. Network Security Group

resource "azurerm_network_security_group" "nginx" {
name = "nsg-linux-nginx"
location = azurerm_resource_group.nginx.location
resource_group_name = azurerm_resource_group.nginx.name

# Allow HTTP website access
security_rule {
name = "Allow-HTTP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "80"
source_address_prefix = "
"
destination_address_prefix = "*"
}

# Allow SSH only from your public IP
security_rule {
name = "Allow-SSH-Admin"
priority = 110
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "22"
source_address_prefix = var.admin_ip_cidr
destination_address_prefix = "
"
}
}

5. Public IP

resource "azurerm_public_ip" "nginx" {
name = "pip-linux-nginx"
location = azurerm_resource_group.nginx.location
resource_group_name = azurerm_resource_group.nginx.name

allocation_method = "Static"
sku = "Standard"
}

6. Network Interface

resource "azurerm_network_interface" "nginx" {
name = "nic-linux-nginx"
location = azurerm_resource_group.nginx.location
resource_group_name = azurerm_resource_group.nginx.name

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

7. Associate NSG with NIC

resource "azurerm_network_interface_security_group_association" "nginx" {
network_interface_id = azurerm_network_interface.nginx.id
network_security_group_id = azurerm_network_security_group.nginx.id
}

8. Ubuntu Linux VM

resource "azurerm_linux_virtual_machine" "nginx" {
name = "vm-linux-nginx"
computer_name = "LINUXNGINX"
location = azurerm_resource_group.nginx.location
resource_group_name = azurerm_resource_group.nginx.name
size = var.vm_size

admin_username = var.admin_username
disable_password_authentication = true

network_interface_ids = [
azurerm_network_interface.nginx.id
]

admin_ssh_key {
username = var.admin_username
public_key = file(var.ssh_public_key_path)
}

os_disk {
name = "disk-linux-nginx-os"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts-gen2"
version = "latest"
}

depends_on = [
azurerm_network_interface_security_group_association.nginx
]
}

9. Install and configure Nginx using remote-exec

resource "terraform_data" "configure_nginx" {
triggers_replace = [
azurerm_linux_virtual_machine.nginx.id
]

connection {
type = "ssh"
host = azurerm_public_ip.nginx.ip_address
user = var.admin_username
private_key = file(var.ssh_private_key_path)
port = 22
timeout = "15m"
}

provisioner "remote-exec" {
inline = [
"sudo apt-get update -y",
"sudo apt-get install -y nginx",
"sudo systemctl enable nginx",
"sudo systemctl start nginx",
"echo 'Nginx successfully installed by Terraform remote-exec!' | sudo tee /var/www/html/index.html",
"systemctl is-active nginx"
]
}

depends_on = [
azurerm_linux_virtual_machine.nginx
]
}