Files
iac_demo/modules/compute/main.tf
T
2025-11-10 06:48:33 +00:00

72 lines
2.1 KiB
Terraform

resource "azurerm_network_interface" "nic" {
name = "vnic"
location = var.location
resource_group_name = var.resource_group_name
ip_configuration {
name = "internal"
subnet_id = var.subnet_id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.vm_public.id
}
}
resource "azurerm_network_interface_security_group_association" "example" {
network_interface_id = azurerm_network_interface.nic.id
network_security_group_id = azurerm_network_security_group.vm_nsg.id
depends_on = [azurerm_network_security_group.vm_nsg]
}
resource "azurerm_network_security_group" "vm_nsg" {
name = "vm-nsg"
resource_group_name = var.resource_group_name
location = var.location
security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = var.ex_ip
destination_address_prefix = "*"
}
}
resource "azurerm_public_ip" "vm_public" {
name = "example-pip"
location = var.location
resource_group_name = var.resource_group_name
allocation_method = "Static"
}
resource "azurerm_linux_virtual_machine" "vm" {
name = "example-vm"
resource_group_name = var.resource_group_name
location = var.location
size = var.vm_size
admin_username = var.admin_username
network_interface_ids = [azurerm_network_interface.nic.id]
admin_ssh_key {
username = var.admin_username
public_key = var.ssh_public_key
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
offer = "ubuntu-24_04-lts"
publisher = "Canonical"
sku = "server"
version = "latest"
}
tags = {
volatile = true
}
}