2016-03-17 14:05:54 +01:00
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_virtual_machine"
sidebar_current: "docs-azurerm-resource-virtualmachine"
description: |-
Create a Virtual Machine.
---
# azurerm\_virtual\_machine
Create a virtual machine.
## Example Usage
```
resource "azurerm_resource_group" "test" {
2017-02-18 23:48:50 +01:00
name = "acctestrg"
location = "West US"
2016-03-17 14:05:54 +01:00
}
resource "azurerm_virtual_network" "test" {
2017-02-18 23:48:50 +01:00
name = "acctvn"
address_space = ["10.0.0.0/16"]
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
2016-03-17 14:05:54 +01:00
}
resource "azurerm_subnet" "test" {
2017-02-18 23:48:50 +01:00
name = "acctsub"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.2.0/24"
2016-03-17 14:05:54 +01:00
}
resource "azurerm_network_interface" "test" {
2017-02-18 23:48:50 +01:00
name = "acctni"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.test.id}"
private_ip_address_allocation = "dynamic"
}
2016-03-17 14:05:54 +01:00
}
resource "azurerm_storage_account" "test" {
2017-02-18 23:48:50 +01:00
name = "accsa"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "westus"
account_type = "Standard_LRS"
tags {
environment = "staging"
}
2016-03-17 14:05:54 +01:00
}
resource "azurerm_storage_container" "test" {
2017-02-18 23:48:50 +01:00
name = "vhds"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_name = "${azurerm_storage_account.test.name}"
container_access_type = "private"
2016-03-17 14:05:54 +01:00
}
resource "azurerm_virtual_machine" "test" {
2017-02-18 23:48:50 +01:00
name = "acctvm"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0"
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "14.04.2-LTS"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
caching = "ReadWrite"
create_option = "FromImage"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags {
environment = "staging"
}
2016-03-17 14:05:54 +01:00
}
```
2016-06-27 20:05:56 +02:00
## Example Usage with additional Empty DataDisk
```
resource "azurerm_resource_group" "test" {
name = "acctestrg"
location = "West US"
}
resource "azurerm_virtual_network" "test" {
name = "acctvn"
address_space = ["10.0.0.0/16"]
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
name = "acctsub"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.2.0/24"
}
resource "azurerm_network_interface" "test" {
name = "acctni"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.test.id}"
private_ip_address_allocation = "dynamic"
}
}
resource "azurerm_storage_account" "test" {
name = "accsa"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "westus"
account_type = "Standard_LRS"
tags {
environment = "staging"
}
}
resource "azurerm_storage_container" "test" {
name = "vhds"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_name = "${azurerm_storage_account.test.name}"
container_access_type = "private"
}
resource "azurerm_virtual_machine" "test" {
name = "acctvm"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0"
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "14.04.2-LTS"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
caching = "ReadWrite"
create_option = "FromImage"
}
storage_data_disk {
name = "datadisk0"
vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/datadisk0.vhd"
disk_size_gb = "1023"
create_option = "empty"
lun = 0
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags {
environment = "staging"
}
}
```
2016-03-17 14:05:54 +01:00
## Argument Reference
The following arguments are supported:
* `name` - (Required) Specifies the name of the virtual machine resource. Changing this forces a
new resource to be created.
* `resource_group_name` - (Required) The name of the resource group in which to
create the virtual machine.
* `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created.
* `plan` - (Optional) A plan block as documented below.
2016-09-12 08:04:04 +02:00
* `availability_set_id` - (Optional) The Id of the Availability Set in which to create the virtual machine
2016-09-29 13:32:19 +02:00
* `boot_diagnostics` - (Optional) A boot diagnostics profile block as referenced below.
2016-03-17 14:05:54 +01:00
* `vm_size` - (Required) Specifies the [size of the virtual machine ](https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-size-specs/ ).
* `storage_image_reference` - (Optional) A Storage Image Reference block as documented below.
* `storage_os_disk` - (Required) A Storage OS Disk block as referenced below.
2016-07-14 11:51:55 +02:00
* `delete_os_disk_on_termination` - (Optional) Flag to enable deletion of the OS Disk VHD blob when the VM is deleted, defaults to `false`
2016-03-17 14:05:54 +01:00
* `storage_data_disk` - (Optional) A list of Storage Data disk blocks as referenced below.
2016-07-25 18:57:02 +02:00
* `delete_data_disks_on_termination` - (Optional) Flag to enable deletion of Storage Disk VHD blobs when the VM is deleted, defaults to `false`
2016-03-17 14:05:54 +01:00
* `os_profile` - (Required) An OS Profile block as documented below.
2016-12-06 09:38:10 +01:00
* `license_type` - (Optional, when a windows machine) Specifies the Windows OS license type. The only allowable value, if supplied, is `Windows_Server` .
2016-03-17 14:05:54 +01:00
* `os_profile_windows_config` - (Required, when a windows machine) A Windows config block as documented below.
* `os_profile_linux_config` - (Required, when a linux machine) A Linux config block as documented below.
* `os_profile_secrets` - (Optional) A collection of Secret blocks as documented below.
* `network_interface_ids` - (Required) Specifies the list of resource IDs for the network interfaces associated with the virtual machine.
2017-03-23 17:42:01 +01:00
* `primary_network_interface_id` - (Optional) Specifies the resource ID for the primary network interface associated with the virtual machine.
2016-06-27 20:05:56 +02:00
* `tags` - (Optional) A mapping of tags to assign to the resource.
2016-03-17 14:05:54 +01:00
For more information on the different example configurations, please check out the [azure documentation ](https://msdn.microsoft.com/en-us/library/mt163591.aspx#Anchor_2 )
`Plan` supports the following:
* `name` - (Required) Specifies the name of the image from the marketplace.
* `publisher` - (Optional) Specifies the publisher of the image.
* `product` - (Optional) Specifies the product of the image from the marketplace.
2016-08-18 04:59:44 +02:00
`boot_diagnostics` supports the following:
* `enabled` : (Required) Whether to enable boot diagnostics for the virtual machine.
* `storage_uri` : (Required) Blob endpoint for the storage account to hold the virtual machine's diagnostic files. This must be the root of a storage account, and not a storage container.
2016-03-17 14:05:54 +01:00
`storage_image_reference` supports the following:
2016-11-24 16:22:50 +01:00
* `publisher` - (Required) Specifies the publisher of the image used to create the virtual machine. Changing this forces a new resource to be created.
* `offer` - (Required) Specifies the offer of the image used to create the virtual machine. Changing this forces a new resource to be created.
* `sku` - (Required) 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.
2016-03-17 14:05:54 +01:00
`storage_os_disk` supports the following:
* `name` - (Required) Specifies the disk name.
2016-11-24 16:22:50 +01:00
* `vhd_uri` - (Required) Specifies the vhd uri. Changing this forces a new resource to be created.
2016-03-17 14:05:54 +01:00
* `create_option` - (Required) Specifies how the virtual machine should be created. Possible values are `attach` and `FromImage` .
* `caching` - (Optional) Specifies the caching requirements.
2016-05-17 02:35:59 +02:00
* `image_uri` - (Optional) Specifies the image_uri in the form publisherName:offer:skus:version. `image_uri` can also specify the [VHD uri ](https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-cli-deploy-templates/#create-a-custom-vm-image ) of a custom VM image to clone. When cloning a custom disk image the `os_type` documented below becomes required.
2016-05-09 19:51:19 +02:00
* `os_type` - (Optional) Specifies the operating system Type, valid values are windows, linux.
2016-10-25 18:54:55 +02:00
* `disk_size_gb` - (Optional) Specifies the size of the data disk in gigabytes.
2016-03-17 14:05:54 +01:00
`storage_data_disk` supports the following:
* `name` - (Required) Specifies the name of the data disk.
* `vhd_uri` - (Required) Specifies the uri of the location in storage where the vhd for the virtual machine should be placed.
* `create_option` - (Required) Specifies how the data disk should be created.
* `disk_size_gb` - (Required) Specifies the size of the data disk in gigabytes.
2017-01-12 14:52:40 +01:00
* `caching` - (Optional) Specifies the caching requirements.
2016-06-27 20:05:56 +02:00
* `lun` - (Required) Specifies the logical unit number of the data disk.
2016-03-17 14:05:54 +01:00
`os_profile` supports the following:
2016-07-20 21:03:54 +02:00
* `computer_name` - (Required) Specifies the name of the virtual machine.
2016-03-17 14:05:54 +01:00
* `admin_username` - (Required) Specifies the name of the administrator account.
* `admin_password` - (Required) Specifies the password of the administrator account.
2017-02-22 17:26:35 +01:00
* `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.
2016-03-17 14:05:54 +01:00
2017-02-18 23:48:50 +01:00
~> **NOTE:** `admin_password` must be between 6-72 characters long and must satisfy at least 3 of password complexity requirements from the following:
1. Contains an uppercase character
2. Contains a lowercase character
3. Contains a numeric digit
2016-05-09 00:07:04 +02:00
4. Contains a special character
2016-03-17 14:05:54 +01:00
`os_profile_windows_config` supports the following:
* `provision_vm_agent` - (Optional)
* `enable_automatic_upgrades` - (Optional)
* `winrm` - (Optional) A collection of WinRM configuration blocks as documented below.
* `additional_unattend_config` - (Optional) An Additional Unattended Config block as documented below.
`winrm` supports the following:
* `protocol` - (Required) Specifies the protocol of listener
* `certificate_url` - (Optional) Specifies URL of the certificate with which new Virtual Machines is provisioned.
`additional_unattend_config` 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` - (Optional) Specifies the base-64 encoded XML formatted content that is added to the unattend.xml file for the specified path and component.
`os_profile_linux_config` supports the following:
* `disable_password_authentication` - (Required) Specifies whether password authentication should be disabled.
2016-06-27 20:05:56 +02:00
* `ssh_keys` - (Optional) Specifies a collection of `path` and `key_data` to be placed on the virtual machine.
2016-05-08 23:45:13 +02:00
2017-03-15 16:19:13 +01:00
~> **Note:** Please note that the only allowed `path` is `/home/<username>/.ssh/authorized_keys` due to a limitation of Azure.
2016-03-17 14:05:54 +01:00
`os_profile_secrets` supports the following:
* `source_vault_id` - (Required) Specifies the key vault to use.
2017-03-27 23:00:29 +02:00
* `vault_certificates` - (Required) A collection of Vault Certificates as documented below
2016-03-17 14:05:54 +01:00
`vault_certificates` support the following:
2017-03-27 23:00:29 +02:00
* `certificate_url` - (Required) Specifies the URI of the key vault secrets in the format of `https://<vaultEndpoint>/secrets/<secretName>/<secretVersion>` . 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 > "
}
```
2016-03-17 14:05:54 +01:00
* `certificate_store` - (Required, on windows machines) Specifies the certificate store on the Virtual Machine where the certificate should be added to.
## Attributes Reference
The following attributes are exported:
2016-04-14 01:24:01 +02:00
* `id` - The virtual machine ID.
2016-11-22 13:13:11 +01:00
## Import
2017-02-18 23:48:50 +01:00
Virtual Machines can be imported using the `resource id` , e.g.
2016-11-22 13:13:11 +01:00
```
terraform import azurerm_virtual_machine.test /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/microsoft.compute/virtualMachines/machine1
```