Skip to content

About

Azure Virtual Machine Terraform module — parameterised, azurerm 4.x, CI-validated · AIRCLOUD

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

AIRCLOUD: DevOps, cloud and AI engineering

azure-virtual-machine-module

Azure Virtual Machine Terraform module — parameterised, azurerm 4.x, CI-validated.

CI License: MIT Release

Enterprise-grade Azure Virtual Machine module with comprehensive security, monitoring, and compliance features.

Features

✅ Full Linux and Windows Support - Complete support for both operating systems
✅ Advanced Security - Encryption at host, Secure Boot, vTPM, Azure Disk Encryption
✅ Managed Identity - System-assigned and user-assigned managed identities
✅ Comprehensive Monitoring - Azure Monitor Agent, Dependency Agent, Network Watcher
✅ Data Disk Management - Multiple data disks with encryption and custom caching
✅ High Availability - Availability Zones and Availability Sets support
✅ Azure Policy Integration - Built-in policy assignments for compliance
✅ Naming Conventions - Microsoft CAF compliant naming patterns
✅ Boot Diagnostics - Enhanced troubleshooting capabilities
✅ Patch Management - Automated patch orchestration
✅ Network Security - Accelerated networking, NSG integration

Usage

Pin the module to a release tag:

module "virtual_machine" {
  source = "git::https://github.com/AIRCLOUD-PL/azure-virtual-machine-module.git?ref=v1.0.0"

  # required inputs
  admin_username      = "..."
  environment         = "..."
  location            = "..."
  os_type             = "..."
  resource_group_name = "..."
  subnet_id           = "..."
  vm_size             = "..."
}

All inputs and outputs are listed in the generated reference below (Inputs, Outputs). Worked examples live in examples/.

Basic Example

module "virtual_machine" {
  source = "git::https://github.com/AIRCLOUD-PL/azure-virtual-machine-module.git?ref=v1.0.0"

  name                = "vm-prod-westeu-001"
  location            = "westeurope"
  resource_group_name = "rg-production"
  environment         = "prod"
  
  vm_size         = "Standard_D2s_v3"
  os_type         = "Linux"
  admin_username  = "azureuser"
  
  subnet_id = azurerm_subnet.main.id
  
  # SSH Key Authentication
  disable_password_authentication = true
  admin_ssh_public_key           = file("~/.ssh/id_rsa.pub")
  
  # Managed Identity
  identity_type = "SystemAssigned"
  
  tags = {
    Environment = "Production"
  }
}

Complete Example with All Features

module "virtual_machine" {
  source = "git::https://github.com/AIRCLOUD-PL/azure-virtual-machine-module.git?ref=v1.0.0"

  # Naming
  name                = "vm-prod-westeu-001"
  location            = "westeurope"
  resource_group_name = "rg-production"
  environment         = "prod"
  
  # VM Configuration
  vm_size    = "Standard_D4s_v3"
  os_type    = "Linux"
  admin_username = "azureuser"
  
  # Network
  subnet_id                     = azurerm_subnet.main.id
  enable_accelerated_networking = true
  private_ip_address_allocation = "Static"
  private_ip_address            = "10.0.1.10"
  
  # High Availability
  availability_zone = "1"
  
  # Image
  source_image_reference = {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-jammy"
    sku       = "22_04-lts-gen2"
    version   = "latest"
  }
  
  # Authentication
  disable_password_authentication = true
  admin_ssh_public_key           = var.ssh_public_key
  
  # Storage
  os_disk_storage_account_type = "Premium_LRS"
  os_disk_size_gb              = 128
  
  data_disks = [
    {
      lun                  = 0
      disk_size_gb         = 512
      storage_account_type = "Premium_LRS"
      caching              = "ReadWrite"
      create_option        = "Empty"
    }
  ]
  
  # Security
  encryption_at_host_enabled = true
  secure_boot_enabled        = true
  vtpm_enabled               = true
  
  # Identity
  identity_type = "SystemAssigned"
  
  # Monitoring
  enable_monitoring              = true
  log_analytics_workspace_id     = azurerm_log_analytics_workspace.main.workspace_id
  log_analytics_workspace_key    = azurerm_log_analytics_workspace.main.primary_shared_key
  enable_dependency_agent        = true
  enable_network_watcher         = true
  
  # Policies
  enable_policy_assignments = true
  
  # Patch Management
  patch_mode            = "AutomaticByPlatform"
  patch_assessment_mode = "AutomaticByPlatform"
  
  tags = {
    Environment = "Production"
    CostCenter  = "IT"
    Compliance  = "PCI-DSS"
  }
}

Examples

Security Features

Encryption

  • Encryption at Host - Hardware-based encryption
  • Azure Disk Encryption - BitLocker/dm-crypt encryption
  • Customer-Managed Keys - Disk Encryption Set support
  • TLS/HTTPS - Encrypted communications

Identity & Access

  • Managed Identity - Azure AD integrated identity
  • SSH Key Authentication - No password authentication for Linux
  • Just-In-Time Access - Azure Security Center JIT
  • RBAC Integration - Role-based access control

Monitoring & Compliance

  • Azure Monitor - Metrics, logs, and alerts
  • Network Watcher - Network diagnostics
  • Boot Diagnostics - Serial console and screenshots
  • Azure Policy - Compliance enforcement
  • Microsoft Defender - Threat protection

Network Security

  • NSG Integration - Network security groups
  • Application Security Groups - Micro-segmentation
  • Azure Firewall - Stateful firewall
  • Private Endpoints - Private connectivity

Naming Convention

The module follows Microsoft Cloud Adoption Framework naming conventions:

{workload}-{environment}-{region}-{resource-type}-{instance}

Example: web-prod-westeu-vm-001

See naming.tf for region and environment abbreviations.

Testing

Unit Tests

cd test
go test -v -timeout 30m

Specific Test

go test -v -run TestVirtualMachineModuleBasic

All Tests with Coverage

go test -v -coverprofile=coverage.out
go tool cover -html=coverage.out

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add/update tests
  5. Run tests locally
  6. Submit a pull request

License

MIT

Authors

Platform Engineering Team

Support

For issues and questions:

Changelog

See CHANGELOG.md for version history.

Reference

Requirements

Name Version
terraform >= 1.5.0
azurerm ~> 4.0

Providers

Name Version
azurerm 4.81.0

Modules

No modules.

Resources

Name Type
azurerm_linux_virtual_machine.main resource
azurerm_managed_disk.data resource
azurerm_network_interface.main resource
azurerm_network_interface_application_security_group_association.main resource
azurerm_resource_group_policy_assignment.approved_extensions resource
azurerm_resource_group_policy_assignment.backup_enabled resource
azurerm_resource_group_policy_assignment.encryption_at_host resource
azurerm_resource_group_policy_assignment.managed_disks resource
azurerm_resource_group_policy_assignment.secure_boot resource
azurerm_virtual_machine_data_disk_attachment.data resource
azurerm_virtual_machine_extension.antimalware resource
azurerm_virtual_machine_extension.azure_monitor_agent_linux resource
azurerm_virtual_machine_extension.azure_monitor_agent_windows resource
azurerm_virtual_machine_extension.custom_script resource
azurerm_virtual_machine_extension.dependency_agent_linux resource
azurerm_virtual_machine_extension.dependency_agent_windows resource
azurerm_virtual_machine_extension.disk_encryption_linux resource
azurerm_virtual_machine_extension.disk_encryption_windows resource
azurerm_virtual_machine_extension.network_watcher_linux resource
azurerm_virtual_machine_extension.network_watcher_windows resource
azurerm_windows_virtual_machine.main resource
azurerm_resource_group.main data source

Inputs

Name Description Type Default Required
admin_password Administrator password for the VM (required for Windows, optional for Linux) string null no
admin_ssh_public_key SSH public key for Linux VMs when password authentication is disabled string null no
admin_username Administrator username for the VM string n/a yes
antimalware_exclusions Antimalware exclusions configuration
object({
Extensions = optional(string, "")
Paths = optional(string, "")
Processes = optional(string, "")
})
{
"Extensions": "",
"Paths": "",
"Processes": ""
}
no
application_security_group_id ID of Application Security Group to associate with NIC string null no
approved_vm_extensions List of approved VM extension types list(string)
[
"MicrosoftMonitoringAgent",
"OmsAgentForLinux",
"AzureMonitorLinuxAgent",
"AzureMonitorWindowsAgent",
"DependencyAgentLinux",
"DependencyAgentWindows",
"AzureDiskEncryptionForLinux",
"AzureDiskEncryption",
"CustomScriptExtension",
"CustomScript"
]
no
availability_set_id ID of the availability set string null no
availability_zone Availability zone number (1, 2, or 3) string null no
boot_diagnostics_storage_account_uri Storage account URI for boot diagnostics (leave null for managed storage) string null no
bypass_platform_safety_checks_on_user_schedule_enabled Bypass platform safety checks on user schedule bool false no
custom_data Custom data to pass to the VM (cloud-init for Linux, will be base64 encoded) string null no
custom_script_extension Custom script extension configuration
object({
settings = map(any)
protected_settings = optional(map(any))
})
null no
data_disks List of data disks to attach to the VM
list(object({
lun = number
disk_size_gb = number
storage_account_type = string
caching = string
create_option = string
}))
[] no
disable_password_authentication Disable password authentication for Linux VMs (SSH key only) bool true no
disk_encryption_set_id ID of Disk Encryption Set for disk encryption with customer-managed keys string null no
dns_servers List of DNS servers for the network interface list(string) [] no
enable_accelerated_networking Enable accelerated networking (requires supported VM size) bool false no
enable_antimalware Enable Microsoft Antimalware extension (Windows only) bool true no
enable_automatic_updates Enable automatic updates for Windows VM bool true no
enable_azure_disk_encryption Enable Azure Disk Encryption extension bool false no
enable_backup_policy Enable backup policy requirement bool true no
enable_boot_diagnostics Enable boot diagnostics bool true no
enable_dependency_agent Enable Dependency Agent for VM Insights bool false no
enable_ip_forwarding Enable IP forwarding on the network interface bool false no
enable_monitoring Enable Azure Monitor Agent bool true no
enable_network_watcher Enable Network Watcher Agent bool true no
enable_policy_assignments Enable Azure Policy assignments for this VM bool true no
encryption_at_host_enabled Enable encryption at host (requires VM size support) bool false no
environment Environment name (e.g., prod, dev, test) string n/a yes
hotpatching_enabled Enable hotpatching for Windows VM bool false no
identity_ids List of User Assigned Identity IDs list(string) [] no
identity_type Type of Managed Identity: SystemAssigned, UserAssigned, or 'SystemAssigned, UserAssigned' string "SystemAssigned" no
instance_number Instance number for the VM (used in auto-generated names) string "001" no
key_encryption_key_url Key Encryption Key URL in Key Vault string null no
key_vault_id Key Vault resource ID for disk encryption string null no
key_vault_url Key Vault URL for disk encryption string null no
license_type License type for Azure Hybrid Benefit: Windows_Client, Windows_Server, RHEL_BYOS, SLES_BYOS string null no
location Azure region where resources will be created string n/a yes
log_analytics_workspace_id Log Analytics Workspace ID for monitoring string null no
log_analytics_workspace_key Log Analytics Workspace Key string null no
name Name of the virtual machine. If null, will be auto-generated based on naming convention. string null no
naming_prefix Prefix for resource naming when auto-generating names string "vm" no
network_interface_name Name of the network interface. If null, will be auto-generated. string null no
os_disk_caching OS disk caching type: None, ReadOnly, or ReadWrite string "ReadWrite" no
os_disk_diff_disk_settings Ephemeral OS disk settings
object({
option = string
placement = optional(string)
})
null no
os_disk_name Name of the OS disk. If null, will be auto-generated. string null no
os_disk_size_gb Size of the OS disk in GB (optional, defaults to image size) number null no
os_disk_storage_account_type OS disk storage account type: Standard_LRS, Premium_LRS, StandardSSD_LRS, UltraSSD_LRS string "Premium_LRS" no
os_type Operating system type: Linux or Windows string n/a yes
patch_assessment_mode Patch assessment mode: AutomaticByPlatform or ImageDefault string "AutomaticByPlatform" no
patch_mode Patch mode: AutomaticByPlatform, AutomaticByOS, Manual, ImageDefault string "AutomaticByPlatform" no
plan Plan information for marketplace images
object({
name = string
product = string
publisher = string
})
null no
private_ip_address Static private IP address (required if allocation is Static) string null no
private_ip_address_allocation Private IP address allocation method: Dynamic or Static string "Dynamic" no
public_ip_address_id ID of public IP address to associate with VM (optional) string null no
resource_group_name Name of the resource group string n/a yes
secure_boot_enabled Enable secure boot (requires trusted launch VM) bool false no
source_image_id ID of custom image (overrides source_image_reference) string null no
source_image_reference Source image reference for the VM
object({
publisher = string
offer = string
sku = string
version = string
})
{
"offer": "0001-com-ubuntu-server-jammy",
"publisher": "Canonical",
"sku": "22_04-lts-gen2",
"version": "latest"
}
no
subnet_id ID of the subnet where VM will be deployed string n/a yes
tags Tags to apply to all resources map(string) {} no
timezone Timezone for Windows VM string null no
ultra_ssd_enabled Enable Ultra SSD support bool false no
user_data User data to pass to the VM (will be base64 encoded) string null no
vm_size Size of the Virtual Machine (e.g., Standard_D2s_v3) string n/a yes
vtpm_enabled Enable vTPM (requires trusted launch VM) bool false no

Outputs

Name Description
admin_username Administrator username
data_disk_ids IDs of attached data disks
identity Managed identity block
identity_principal_id Principal ID of the system-assigned managed identity
naming_convention_example Naming convention example
naming_convention_pattern Naming convention pattern used
network_interface_id ID of the network interface
os_disk_id ID of the OS disk
private_ip_address Private IP address of the VM
public_ip_address Public IP address of the VM (if assigned)
vm_id ID of the virtual machine
vm_name Name of the virtual machine

Part of the AIRCLOUD Azure module catalog

Browse all modules with usage snippets in the module catalog or on github.com/AIRCLOUD-PL. Built and maintained by AIRCLOUD: DevOps, cloud and AI engineering. Need a hand with Azure landing zones or platform engineering? See aircloud.com.pl/consulting.

About

Azure Virtual Machine Terraform module — parameterised, azurerm 4.x, CI-validated · AIRCLOUD

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages