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 (1)

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
]
}

Some comments may only be visible to logged-in visitors. Sign in to view all comments.