Manages a Virtual Machine.
This example provisions a Virtual Machine with Managed Disks. Other examples of the azurerm_virtual_machine
resource can be found in the ./examples/virtual-machines
directory within the GitHub Repository
variable "prefix" {
default = "tfvmex"
}
resource "azurerm_resource_group" "example" {
name = "${var.prefix}-resources"
location = "West Europe"
}
resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = ["10.0.2.0/24"]
}
resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name = "testconfiguration1"
subnet_id = azurerm_subnet.internal.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_virtual_machine" "main" {
name = "${var.prefix}-vm"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
network_interface_ids = [azurerm_network_interface.main.id]
vm_size = "Standard_DS1_v2"
# Uncomment this line to delete the OS disk automatically when deleting the VM
# delete_os_disk_on_termination = true
# Uncomment this line to delete the data disks automatically when deleting the VM
# delete_data_disks_on_termination = true
storage_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags = {
environment = "staging"
}
}
The following arguments are supported:
name
- (Required) Specifies the name of the Virtual Machine. Changing this forces a new resource to be created.
resource_group_name
- (Required) Specifies the name of the Resource Group in which the Virtual Machine should exist. Changing this forces a new resource to be created.
location
- (Required) Specifies the Azure Region where the Virtual Machine exists. Changing this forces a new resource to be created.
network_interface_ids
- (Required) A list of Network Interface IDs which should be associated with the Virtual Machine.
os_profile_linux_config
- (Optional) (Required, when a Linux machine) An os_profile_linux_config
block as defined below.
os_profile_windows_config
- (Optional) (Required, when a Windows machine) An os_profile_windows_config
block as defined below.
vm_size
- (Required) Specifies the size of the Virtual Machine. See also Azure VM Naming Conventions.
availability_set_id
- (Optional) The ID of the Availability Set in which the Virtual Machine should exist. Changing this forces a new resource to be created.
boot_diagnostics
- (Optional) A boot_diagnostics
block as defined below.
additional_capabilities
- (Optional) An additional_capabilities
block as defined below.
delete_os_disk_on_termination
- (Optional) Should the OS Disk (either the Managed Disk / VHD Blob) be deleted when the Virtual Machine is destroyed? Defaults to false
.
delete_data_disks_on_termination
- (Optional) Should the Data Disks (either the Managed Disks / VHD Blobs) be deleted when the Virtual Machine is destroyed? Defaults to false
.identity
- (Optional) An identity
block as defined below.
license_type
- (Optional) Specifies the BYOL Type for this Virtual Machine. This is only applicable to Windows Virtual Machines. Possible values are Windows_Client
and Windows_Server
.
os_profile
- (Optional) An os_profile
block as defined below. Required when create_option
in the storage_os_disk
block is set to FromImage
.
os_profile_secrets
- (Optional) One or more os_profile_secrets
blocks as defined below.
plan
- (Optional) A plan
block as defined below.
primary_network_interface_id
- (Optional) The ID of the Network Interface (which must be attached to the Virtual Machine) which should be the Primary Network Interface for this Virtual Machine.
proximity_placement_group_id
- (Optional) The ID of the Proximity Placement Group to which this Virtual Machine should be assigned. Changing this forces a new resource to be created
storage_data_disk
- (Optional) One or more storage_data_disk
blocks as defined below.
storage_image_reference
- (Optional) A storage_image_reference
block as defined below. Changing this forces a new resource to be created.
storage_os_disk
- (Required) A storage_os_disk
block as defined below.
tags
- (Optional) A mapping of tags to assign to the Virtual Machine.
zones
- (Optional) A list of a single item of the Availability Zone which the Virtual Machine should be allocated in. Changing this forces a new resource to be created.
For more information on the different example configurations, please check out the Azure documentation
An additional_unattend_config
block supports the following:
pass
- (Required) Specifies the name of the pass that the content applies to. The only allowable value is oobeSystem
.
component
- (Required) Specifies the name of the component to configure with the added content. The only allowable value is Microsoft-Windows-Shell-Setup
.
setting_name
- (Required) Specifies the name of the setting to which the content applies. Possible values are: FirstLogonCommands
and AutoLogon
.
content
- (Required) Specifies the base-64 encoded XML formatted content that is added to the unattend.xml file for the specified path and component.
A boot_diagnostics
block supports the following:
enabled
- (Required) Should Boot Diagnostics be enabled for this Virtual Machine?
storage_uri
- (Required) The Storage Account's Blob Endpoint which should hold the virtual machine's diagnostic files.
A additional_capabilities
block supports the following:
ultra_ssd_enabled
- (Required) Should Ultra SSD disk be enabled for this Virtual Machine? Changing this forces a new resource to be created.A identity
block supports the following:
type
- (Required) Specifies the type of Managed Service Identity that should be configured on this Virtual Machine. Possible values are SystemAssigned
, UserAssigned
, SystemAssigned, UserAssigned
(to enable both).identity_ids
- (Optional) Specifies a list of User Assigned Managed Identity IDs to be assigned to this Virtual Machine.A os_profile
block supports the following:
computer_name
- (Required) Specifies the name of the Virtual Machine. Changing this forces a new resource to be created.
admin_username
- (Required) Specifies the name of the local administrator account.
admin_password
- (Optional) (Optional for Windows, Optional for Linux) The password associated with the local administrator account.
custom_data
- (Optional) Specifies custom data to supply to the machine. On Linux-based systems, this can be used as a cloud-init script. On other systems, this will be copied as a file on disk. Internally, Terraform will base64 encode this value before sending it to the API. The maximum length of the binary array is 65535 bytes. Changing this forces a new resource to be created.A os_profile_linux_config
block supports the following:
disable_password_authentication
- (Required) Specifies whether password authentication should be disabled. If set to false
, an admin_password
must be specified.
ssh_keys
- (Optional) One or more ssh_keys
blocks as defined below. This field is required if disable_password_authentication
is set to true
.
A os_profile_secrets
block supports the following:
source_vault_id
- (Required) Specifies the ID of the Key Vault to use.
vault_certificates
- (Optional) One or more vault_certificates
blocks as defined below.
A os_profile_windows_config
block supports the following:
provision_vm_agent
- (Optional) Should the Azure Virtual Machine Guest Agent be installed on this Virtual Machine? Defaults to false
.enable_automatic_upgrades
- (Optional) Are automatic updates enabled on this Virtual Machine? Defaults to false
.
timezone
- (Optional) Specifies the time zone of the virtual machine, the possible values are defined here. Changing this forces a new resource to be created.
winrm
- (Optional) One or more winrm
blocks as defined below.
additional_unattend_config
- (Optional) An additional_unattend_config
block as defined below.
A plan
block supports the following:
name
- (Required) Specifies the name of the image from the marketplace.
publisher
- (Required) Specifies the publisher of the image.
product
- (Required) Specifies the product of the image from the marketplace.
A ssh_keys
block supports the following:
key_data
- (Required) The Public SSH Key which should be written to the path
defined above.path
- (Required) The path of the destination file on the virtual machineA storage_image_reference
block supports the following:
This block provisions the Virtual Machine from one of two sources: an Azure Platform Image (e.g. Ubuntu/Windows Server) or a Custom Image.
To provision from an Azure Platform Image, the following fields are applicable:
publisher
- (Optional) Specifies the publisher of the image used to create the virtual machine. Changing this forces a new resource to be created.
offer
- (Optional) Specifies the offer of the image used to create the virtual machine. Changing this forces a new resource to be created.
sku
- (Optional) Specifies the SKU of the image used to create the virtual machine. Changing this forces a new resource to be created.
version
- (Optional) Specifies the version of the image used to create the virtual machine. Changing this forces a new resource to be created.
To provision a Custom Image, the following fields are applicable:
id
- (Optional) Specifies the ID of the Custom Image which the Virtual Machine should be created from. Changing this forces a new resource to be created.A storage_data_disk
block supports the following:
name
- (Required) The name of the Data Disk.
caching
- (Optional) Specifies the caching requirements for the Data Disk. Possible values include None
, ReadOnly
and ReadWrite
.
create_option
- (Required) Specifies how the data disk should be created. Possible values are Attach
, FromImage
and Empty
.
disk_size_gb
- (Optional) Specifies the size of the data disk in gigabytes.
lun
- (Required) Specifies the logical unit number of the data disk. This needs to be unique within all the Data Disks on the Virtual Machine.
write_accelerator_enabled
- (Optional) Specifies if Write Accelerator is enabled on the disk. This can only be enabled on Premium_LRS
managed disks with no caching and M-Series VMs. Defaults to false
.
The following properties apply when using Managed Disks:
managed_disk_type
- (Optional) Specifies the type of managed disk to create. Possible values are either Standard_LRS
, StandardSSD_LRS
, Premium_LRS
or UltraSSD_LRS
.managed_disk_id
- (Optional) Specifies the ID of an Existing Managed Disk which should be attached to this Virtual Machine. When this field is set create_option
must be set to Attach
.The following properties apply when using Unmanaged Disks:
vhd_uri
- (Optional) Specifies the URI of the VHD file backing this Unmanaged Data Disk. A storage_os_disk
block supports the following:
name
- (Required) Specifies the name of the OS Disk.
create_option
- (Required) Specifies how the OS Disk should be created. Possible values are Attach
(managed disks only) and FromImage
.
caching
- (Optional) Specifies the caching requirements for the OS Disk. Possible values include None
, ReadOnly
and ReadWrite
.
disk_size_gb
- (Optional) Specifies the size of the OS Disk in gigabytes.
image_uri
- (Optional) Specifies the Image URI in the format publisherName:offer:skus:version
. This field can also specify the VHD URI of a custom VM image to clone. When cloning a Custom (Unmanaged) Disk Image the os_type
field must be set.
os_type
- (Optional) Specifies the Operating System on the OS Disk. Possible values are Linux
and Windows
.
write_accelerator_enabled
- (Optional) Specifies if Write Accelerator is enabled on the disk. This can only be enabled on Premium_LRS
managed disks with no caching and M-Series VMs. Defaults to false
.
The following properties apply when using Managed Disks:
managed_disk_id
- (Optional) Specifies the ID of an existing Managed Disk which should be attached as the OS Disk of this Virtual Machine. If this is set then the create_option
must be set to Attach
. Changing this forces a new resource to be created.
managed_disk_type
- (Optional) Specifies the type of Managed Disk which should be created. Possible values are Standard_LRS
, StandardSSD_LRS
or Premium_LRS
.
The following properties apply when using Unmanaged Disks:
vhd_uri
- (Optional) Specifies the URI of the VHD file backing this Unmanaged OS Disk. Changing this forces a new resource to be created.A vault_certificates
block supports the following:
certificate_url
- (Required) The ID of the Key Vault Secret. Stored secret is the Base64 encoding of a JSON Object that which is encoded in UTF-8 of which the contents need to be:{
"data":"<Base64-encoded-certificate>",
"dataType":"pfx",
"password":"<pfx-file-password>"
}
certificate_store
- (Optional) (Required, on windows machines) Specifies the certificate store on the Virtual Machine where the certificate should be added to, such as My
.A winrm
block supports the following:
protocol
- (Required) Specifies the protocol of listener. Possible values are HTTP
or HTTPS
.
certificate_url
- (Optional) The ID of the Key Vault Secret which contains the encrypted Certificate which should be installed on the Virtual Machine. This certificate must also be specified in the vault_certificates
block within the os_profile_secrets
block.
In addition to the Arguments listed above - the following Attributes are exported:
id
- The ID of the Virtual Machine.
identity
- An identity
block as defined below.
An identity
block exports the following:
principal_id
- The Principal ID associated with this Managed Service Identity.The timeouts
block allows you to specify timeouts for certain actions:
create
- (Defaults to 60 minutes) Used when creating the Virtual Machine.update
- (Defaults to 60 minutes) Used when updating the Virtual Machine.read
- (Defaults to 5 minutes) Used when retrieving the Virtual Machine.delete
- (Defaults to 60 minutes) Used when deleting the Virtual Machine.Virtual Machines can be imported using the resource id
, e.g.
terraform import azurerm_virtual_machine.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Compute/virtualMachines/machine1