github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/website/source/docs/providers/azurerm/r/virtual_machine.html.markdown (about)

     1  ---
     2  layout: "azurerm"
     3  page_title: "Azure Resource Manager: azurerm_virtual_machine"
     4  sidebar_current: "docs-azurerm-resource-virtualmachine"
     5  description: |-
     6    Create a Virtual Machine.
     7  ---
     8  
     9  # azurerm\_virtual\_machine
    10  
    11  Create a virtual machine.
    12  
    13  ## Example Usage
    14  
    15  ```
    16  resource "azurerm_resource_group" "test" {
    17    name     = "acctestrg"
    18    location = "West US"
    19  }
    20  
    21  resource "azurerm_virtual_network" "test" {
    22    name                = "acctvn"
    23    address_space       = ["10.0.0.0/16"]
    24    location            = "West US"
    25    resource_group_name = "${azurerm_resource_group.test.name}"
    26  }
    27  
    28  resource "azurerm_subnet" "test" {
    29    name                 = "acctsub"
    30    resource_group_name  = "${azurerm_resource_group.test.name}"
    31    virtual_network_name = "${azurerm_virtual_network.test.name}"
    32    address_prefix       = "10.0.2.0/24"
    33  }
    34  
    35  resource "azurerm_network_interface" "test" {
    36    name                = "acctni"
    37    location            = "West US"
    38    resource_group_name = "${azurerm_resource_group.test.name}"
    39  
    40    ip_configuration {
    41      name                          = "testconfiguration1"
    42      subnet_id                     = "${azurerm_subnet.test.id}"
    43      private_ip_address_allocation = "dynamic"
    44    }
    45  }
    46  
    47  resource "azurerm_storage_account" "test" {
    48    name                = "accsa"
    49    resource_group_name = "${azurerm_resource_group.test.name}"
    50    location            = "westus"
    51    account_type        = "Standard_LRS"
    52  
    53    tags {
    54      environment = "staging"
    55    }
    56  }
    57  
    58  resource "azurerm_storage_container" "test" {
    59    name                  = "vhds"
    60    resource_group_name   = "${azurerm_resource_group.test.name}"
    61    storage_account_name  = "${azurerm_storage_account.test.name}"
    62    container_access_type = "private"
    63  }
    64  
    65  resource "azurerm_virtual_machine" "test" {
    66    name                  = "acctvm"
    67    location              = "West US"
    68    resource_group_name   = "${azurerm_resource_group.test.name}"
    69    network_interface_ids = ["${azurerm_network_interface.test.id}"]
    70    vm_size               = "Standard_A0"
    71  
    72    storage_image_reference {
    73      publisher = "Canonical"
    74      offer     = "UbuntuServer"
    75      sku       = "14.04.2-LTS"
    76      version   = "latest"
    77    }
    78  
    79    storage_os_disk {
    80      name          = "myosdisk1"
    81      vhd_uri       = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
    82      caching       = "ReadWrite"
    83      create_option = "FromImage"
    84    }
    85  
    86    os_profile {
    87      computer_name  = "hostname"
    88      admin_username = "testadmin"
    89      admin_password = "Password1234!"
    90    }
    91  
    92    os_profile_linux_config {
    93      disable_password_authentication = false
    94    }
    95  
    96    tags {
    97      environment = "staging"
    98    }
    99  }
   100  ```
   101  
   102  ## Example Usage with additional Empty DataDisk
   103  
   104  ```
   105  resource "azurerm_resource_group" "test" {
   106    name     = "acctestrg"
   107    location = "West US"
   108  }
   109  
   110  resource "azurerm_virtual_network" "test" {
   111    name                = "acctvn"
   112    address_space       = ["10.0.0.0/16"]
   113    location            = "West US"
   114    resource_group_name = "${azurerm_resource_group.test.name}"
   115  }
   116  
   117  resource "azurerm_subnet" "test" {
   118    name                 = "acctsub"
   119    resource_group_name  = "${azurerm_resource_group.test.name}"
   120    virtual_network_name = "${azurerm_virtual_network.test.name}"
   121    address_prefix       = "10.0.2.0/24"
   122  }
   123  
   124  resource "azurerm_network_interface" "test" {
   125    name                = "acctni"
   126    location            = "West US"
   127    resource_group_name = "${azurerm_resource_group.test.name}"
   128  
   129    ip_configuration {
   130      name                          = "testconfiguration1"
   131      subnet_id                     = "${azurerm_subnet.test.id}"
   132      private_ip_address_allocation = "dynamic"
   133    }
   134  }
   135  
   136  resource "azurerm_storage_account" "test" {
   137    name                = "accsa"
   138    resource_group_name = "${azurerm_resource_group.test.name}"
   139    location            = "westus"
   140    account_type        = "Standard_LRS"
   141  
   142    tags {
   143      environment = "staging"
   144    }
   145  }
   146  
   147  resource "azurerm_storage_container" "test" {
   148    name                  = "vhds"
   149    resource_group_name   = "${azurerm_resource_group.test.name}"
   150    storage_account_name  = "${azurerm_storage_account.test.name}"
   151    container_access_type = "private"
   152  }
   153  
   154  resource "azurerm_virtual_machine" "test" {
   155    name                  = "acctvm"
   156    location              = "West US"
   157    resource_group_name   = "${azurerm_resource_group.test.name}"
   158    network_interface_ids = ["${azurerm_network_interface.test.id}"]
   159    vm_size               = "Standard_A0"
   160  
   161    storage_image_reference {
   162      publisher = "Canonical"
   163      offer     = "UbuntuServer"
   164      sku       = "14.04.2-LTS"
   165      version   = "latest"
   166    }
   167  
   168    storage_os_disk {
   169      name          = "myosdisk1"
   170      vhd_uri       = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
   171      caching       = "ReadWrite"
   172      create_option = "FromImage"
   173    }
   174  
   175    storage_data_disk {
   176      name          = "datadisk0"
   177      vhd_uri       = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/datadisk0.vhd"
   178      disk_size_gb  = "1023"
   179      create_option = "empty"
   180      lun           = 0
   181    }
   182  
   183    os_profile {
   184      computer_name  = "hostname"
   185      admin_username = "testadmin"
   186      admin_password = "Password1234!"
   187    }
   188  
   189    os_profile_linux_config {
   190      disable_password_authentication = false
   191    }
   192  
   193    tags {
   194      environment = "staging"
   195    }
   196  }
   197  ```
   198  
   199  ## Argument Reference
   200  
   201  The following arguments are supported:
   202  
   203  * `name` - (Required) Specifies the name of the virtual machine resource. Changing this forces a
   204      new resource to be created.
   205  * `resource_group_name` - (Required) The name of the resource group in which to
   206      create the virtual machine.
   207  * `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created.
   208  * `plan` - (Optional) A plan block as documented below.
   209  * `availability_set_id` - (Optional) The Id of the Availability Set in which to create the virtual machine
   210  * `boot_diagnostics` - (Optional) A boot diagnostics profile block as referenced below.
   211  * `vm_size` - (Required) Specifies the [size of the virtual machine](https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-size-specs/).
   212  * `storage_image_reference` - (Optional) A Storage Image Reference block as documented below.
   213  * `storage_os_disk` - (Required) A Storage OS Disk block as referenced below.
   214  * `delete_os_disk_on_termination` - (Optional) Flag to enable deletion of the OS Disk VHD blob when the VM is deleted, defaults to `false`
   215  * `storage_data_disk` - (Optional) A list of Storage Data disk blocks as referenced below.
   216  * `delete_data_disks_on_termination` - (Optional) Flag to enable deletion of Storage Disk VHD blobs when the VM is deleted, defaults to `false`
   217  * `os_profile` - (Required) An OS Profile block as documented below.
   218  * `license_type` - (Optional, when a windows machine) Specifies the Windows OS license type. The only allowable value, if supplied, is `Windows_Server`.
   219  * `os_profile_windows_config` - (Required, when a windows machine) A Windows config block as documented below.
   220  * `os_profile_linux_config` - (Required, when a linux machine) A Linux config block as documented below.
   221  * `os_profile_secrets` - (Optional) A collection of Secret blocks as documented below.
   222  * `network_interface_ids` - (Required) Specifies the list of resource IDs for the network interfaces associated with the virtual machine.
   223  * `tags` - (Optional) A mapping of tags to assign to the resource.
   224  
   225  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)
   226  
   227  `Plan` supports the following:
   228  
   229  * `name` - (Required) Specifies the name of the image from the marketplace.
   230  * `publisher` - (Optional) Specifies the publisher of the image.
   231  * `product` - (Optional) Specifies the product of the image from the marketplace.
   232  
   233  `boot_diagnostics` supports the following:
   234  
   235  * `enabled`: (Required) Whether to enable boot diagnostics for the virtual machine.
   236  * `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.
   237  
   238  `storage_image_reference` supports the following:
   239  
   240  * `publisher` - (Required) Specifies the publisher of the image used to create the virtual machine. Changing this forces a new resource to be created.
   241  * `offer` - (Required) Specifies the offer of the image used to create the virtual machine. Changing this forces a new resource to be created.
   242  * `sku` - (Required) Specifies the SKU of the image used to create the virtual machine. Changing this forces a new resource to be created.
   243  * `version` - (Optional) Specifies the version of the image used to create the virtual machine. Changing this forces a new resource to be created.
   244  
   245  `storage_os_disk` supports the following:
   246  
   247  * `name` - (Required) Specifies the disk name.
   248  * `vhd_uri` - (Required) Specifies the vhd uri. Changing this forces a new resource to be created.
   249  * `create_option` - (Required) Specifies how the virtual machine should be created. Possible values are `attach` and `FromImage`.
   250  * `caching` - (Optional) Specifies the caching requirements.
   251  * `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.
   252  * `os_type` - (Optional) Specifies the operating system Type, valid values are windows, linux.
   253  * `disk_size_gb` - (Optional) Specifies the size of the data disk in gigabytes.
   254  
   255  `storage_data_disk` supports the following:
   256  
   257  * `name` - (Required) Specifies the name of the data disk.
   258  * `vhd_uri` - (Required) Specifies the uri of the location in storage where the vhd for the virtual machine should be placed.
   259  * `create_option` - (Required) Specifies how the data disk should be created.
   260  * `disk_size_gb` - (Required) Specifies the size of the data disk in gigabytes.
   261  * `caching` - (Optional) Specifies the caching requirements.
   262  * `lun` - (Required) Specifies the logical unit number of the data disk.
   263  
   264  `os_profile` supports the following:
   265  
   266  * `computer_name` - (Required) Specifies the name of the virtual machine.
   267  * `admin_username` - (Required) Specifies the name of the administrator account.
   268  * `admin_password` - (Required) Specifies the password of the administrator account.
   269  * `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.
   270  
   271  ~> **NOTE:** `admin_password` must be between 6-72 characters long and must satisfy at least 3 of password complexity requirements from the following:
   272  1. Contains an uppercase character
   273  2. Contains a lowercase character
   274  3. Contains a numeric digit
   275  4. Contains a special character
   276  
   277  `os_profile_windows_config` supports the following:
   278  
   279  * `provision_vm_agent` - (Optional)
   280  * `enable_automatic_upgrades` - (Optional)
   281  * `winrm` - (Optional) A collection of WinRM configuration blocks as documented below.
   282  * `additional_unattend_config` - (Optional) An Additional Unattended Config block as documented below.
   283  
   284  `winrm` supports the following:
   285  
   286  * `protocol` - (Required) Specifies the protocol of listener
   287  * `certificate_url` - (Optional) Specifies URL of the certificate with which new Virtual Machines is provisioned.
   288  
   289  `additional_unattend_config` supports the following:
   290  
   291  * `pass` - (Required) Specifies the name of the pass that the content applies to. The only allowable value is `oobeSystem`.
   292  * `component` - (Required) Specifies the name of the component to configure with the added content. The only allowable value is `Microsoft-Windows-Shell-Setup`.
   293  * `setting_name` - (Required) Specifies the name of the setting to which the content applies. Possible values are: `FirstLogonCommands` and `AutoLogon`.
   294  * `content` - (Optional) Specifies the base-64 encoded XML formatted content that is added to the unattend.xml file for the specified path and component.
   295  
   296  `os_profile_linux_config` supports the following:
   297  
   298  * `disable_password_authentication` - (Required) Specifies whether password authentication should be disabled.
   299  * `ssh_keys` - (Optional) Specifies a collection of `path` and `key_data` to be placed on the virtual machine.
   300  
   301  ~> **Note:** Please note that the only allowed `path` is `/home/<username>/.ssh/authorized_keys` due to a limitation of Azure_
   302  
   303  `os_profile_secrets` supports the following:
   304  
   305  * `source_vault_id` - (Required) Specifies the key vault to use.
   306  * `vault_certificates` - (Required, on windows machines) A collection of Vault Certificates as documented below
   307  
   308  `vault_certificates` support the following:
   309  
   310  * `certificate_url` - (Required) It is the Base64 encoding of a JSON Object that which is encoded in UTF-8 of which the contents need to be `data`, `dataType` and `password`.
   311  * `certificate_store` - (Required, on windows machines) Specifies the certificate store on the Virtual Machine where the certificate should be added to.
   312  
   313  ## Attributes Reference
   314  
   315  The following attributes are exported:
   316  
   317  * `id` - The virtual machine ID.
   318  
   319  ## Import
   320  
   321  Virtual Machines can be imported using the `resource id`, e.g.
   322  
   323  ```
   324  terraform import azurerm_virtual_machine.test /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/microsoft.compute/virtualMachines/machine1
   325  ```