برنامه نویسی
با استفاده از Terraform یک Azure VM را با گروه امنیتی شبکه (NSG) مستقر کنید

🧭 مقدمه
در این پست ، ما نحوه استفاده از Terraform را برای استقرار یک ماشین مجازی ساده لاجورد (VM) به همراه یک گروه امنیتی شبکه (NSG) طی خواهیم کرد. این یک نمونه عالی برای مبتدیانی است که به دنبال درک چگونگی استفاده از زیرساخت ها به عنوان کد (IAC) در شبکه های ابری و محاسبه منابع هستند.
🗂 ساختار پروژه
این پروژه حاوی پرونده های Terraform زیر است:
azure-vm-nsg-template/
main.tf # تعاریف منابع اصلی (VM ، NSG ، NIC ، RG)
متغیرهای متغیر. اعلامیه های متغیر ورودی
terraform.tfvars # مقادیر متغیر (به عنوان مثال نام VM ، مکان)
├= outputs.tf # مقادیر خروجی مانند آدرس IP
🔧 تعاریف منابع
1. گروه منابع
hcl
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.location
}
2. Network Security Group (NSG)
resource "azurerm_network_security_group" "nsg" {
name = "demo-nsg"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
security_rule {
name = "Allow-SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
3. Virtual Network + Subnet
resource "azurerm_virtual_network" "vnet" {
name = "demo-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_subnet" "subnet" {
name = "demo-subnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}
4. Network Interface
resource "azurerm_network_interface" "nic" {
name = "demo-nic"
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"
}
}
5. Associate NSG with NIC
resource "azurerm_network_interface_security_group_association" "nsg_assoc" {
network_interface_id = azurerm_network_interface.nic.id
network_security_group_id = azurerm_network_security_group.nsg.id
}
6. Virtual Machine
resource "azurerm_linux_virtual_machine" "vm" {
name = var.vm_name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = "Standard_B1s"
admin_username = var.admin_username
network_interface_ids = [azurerm_network_interface.nic.id]
admin_password = var.admin_password
disable_password_authentication = false
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
🚀 How to Deploy (Optional)
az login
terraform init
terraform plan
terraform apply
✅ Conclusion
This example demonstrates how to deploy a basic virtual machine with an associated NSG using Terraform. It’s a great step toward building more complex cloud environments and is especially helpful for those preparing for Azure infrastructure roles or certifications.
👉 GitHub Repository: https://github.com/Smallsun2025/azure-vm-nsg-template