gitops/iac-template/terraform-hcl-standard/azure-cloud/modules/ec2/main.tf

105 lines
2.3 KiB
HCL

variable "resource_group_name" {
description = "Resource group for the VM"
type = string
}
variable "location" {
description = "Azure region"
type = string
}
variable "vm_name" {
description = "Virtual machine name"
type = string
default = "vm"
}
variable "subnet_id" {
description = "Subnet ID to attach the VM"
type = string
}
variable "admin_username" {
description = "Admin username for the VM"
type = string
default = "azureuser"
}
variable "ssh_public_key" {
description = "Public SSH key for login"
type = string
}
variable "vm_size" {
description = "Azure VM size"
type = string
default = "Standard_B2s"
}
variable "source_image" {
description = "Platform image definition"
type = object({
publisher = string
offer = string
sku = string
version = string
})
default = {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-focal"
sku = "20_04-lts"
version = "latest"
}
}
resource "azurerm_network_interface" "vm" {
name = "${var.vm_name}-nic"
location = var.location
resource_group_name = var.resource_group_name
ip_configuration {
name = "ipconfig1"
subnet_id = var.subnet_id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_linux_virtual_machine" "vm" {
name = var.vm_name
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.vm.id
]
admin_ssh_key {
username = var.admin_username
public_key = var.ssh_public_key
}
os_disk {
name = "${var.vm_name}-osdisk"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = var.source_image.publisher
offer = var.source_image.offer
sku = var.source_image.sku
version = var.source_image.version
}
}
output "vm_id" {
value = azurerm_linux_virtual_machine.vm.id
description = "Virtual machine resource ID"
}
output "nic_id" {
value = azurerm_network_interface.vm.id
description = "Network interface ID"
}