github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 ```hcl 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 ```hcl 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 ## Example Usage with Managed Disks 200 201 ```hcl 202 resource "azurerm_resource_group" "test" { 203 name = "acctestrg" 204 location = "West US 2" 205 } 206 207 resource "azurerm_virtual_network" "test" { 208 name = "acctvn" 209 address_space = ["10.0.0.0/16"] 210 location = "West US 2" 211 resource_group_name = "${azurerm_resource_group.test.name}" 212 } 213 214 resource "azurerm_subnet" "test" { 215 name = "acctsub" 216 resource_group_name = "${azurerm_resource_group.test.name}" 217 virtual_network_name = "${azurerm_virtual_network.test.name}" 218 address_prefix = "10.0.2.0/24" 219 } 220 221 resource "azurerm_network_interface" "test" { 222 name = "acctni" 223 location = "West US 2" 224 resource_group_name = "${azurerm_resource_group.test.name}" 225 226 ip_configuration { 227 name = "testconfiguration1" 228 subnet_id = "${azurerm_subnet.test.id}" 229 private_ip_address_allocation = "dynamic" 230 } 231 } 232 233 resource "azurerm_managed_disk" "test" { 234 name = "datadisk_existing" 235 location = "West US 2" 236 resource_group_name = "${azurerm_resource_group.test.name}" 237 storage_account_type = "Standard_LRS" 238 create_option = "Empty" 239 disk_size_gb = "1023" 240 } 241 242 resource "azurerm_virtual_machine" "test" { 243 name = "acctvm" 244 location = "West US 2" 245 resource_group_name = "${azurerm_resource_group.test.name}" 246 network_interface_ids = ["${azurerm_network_interface.test.id}"] 247 vm_size = "Standard_DS1_v2" 248 249 storage_image_reference { 250 publisher = "Canonical" 251 offer = "UbuntuServer" 252 sku = "14.04.2-LTS" 253 version = "latest" 254 } 255 256 storage_os_disk { 257 name = "myosdisk1" 258 caching = "ReadWrite" 259 create_option = "FromImage" 260 managed_disk_type = "Standard_LRS" 261 } 262 263 storage_data_disk { 264 name = "datadisk_new" 265 managed_disk_type = "Standard_LRS" 266 create_option = "Empty" 267 lun = 0 268 disk_size_gb = "1023" 269 } 270 271 storage_data_disk { 272 name = "${azurerm_managed_disk.test.name}" 273 managed_disk_id = "${azurerm_managed_disk.test.id}" 274 create_option = "Attach" 275 lun = 1 276 disk_size_gb = "${azurerm_managed_disk.test.disk_size_gb}" 277 } 278 279 os_profile { 280 computer_name = "hostname" 281 admin_username = "testadmin" 282 admin_password = "Password1234!" 283 } 284 285 os_profile_linux_config { 286 disable_password_authentication = false 287 } 288 289 tags { 290 environment = "staging" 291 } 292 } 293 ``` 294 295 ## Argument Reference 296 297 The following arguments are supported: 298 299 * `name` - (Required) Specifies the name of the virtual machine resource. Changing this forces a 300 new resource to be created. 301 * `resource_group_name` - (Required) The name of the resource group in which to 302 create the virtual machine. 303 * `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created. 304 * `plan` - (Optional) A plan block as documented below. 305 * `availability_set_id` - (Optional) The Id of the Availability Set in which to create the virtual machine 306 * `boot_diagnostics` - (Optional) A boot diagnostics profile block as referenced below. 307 * `vm_size` - (Required) Specifies the [size of the virtual machine](https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-size-specs/). 308 * `storage_image_reference` - (Optional) A Storage Image Reference block as documented below. 309 * `storage_os_disk` - (Required) A Storage OS Disk block as referenced below. 310 * `delete_os_disk_on_termination` - (Optional) Flag to enable deletion of the OS Disk VHD blob when the VM is deleted, defaults to `false` 311 * `storage_data_disk` - (Optional) A list of Storage Data disk blocks as referenced below. 312 * `delete_data_disks_on_termination` - (Optional) Flag to enable deletion of Storage Disk VHD blobs when the VM is deleted, defaults to `false` 313 * `os_profile` - (Required) An OS Profile block as documented below. 314 * `license_type` - (Optional, when a windows machine) Specifies the Windows OS license type. The only allowable value, if supplied, is `Windows_Server`. 315 * `os_profile_windows_config` - (Required, when a windows machine) A Windows config block as documented below. 316 * `os_profile_linux_config` - (Required, when a linux machine) A Linux config block as documented below. 317 * `os_profile_secrets` - (Optional) A collection of Secret blocks as documented below. 318 * `network_interface_ids` - (Required) Specifies the list of resource IDs for the network interfaces associated with the virtual machine. 319 * `primary_network_interface_id` - (Optional) Specifies the resource ID for the primary network interface associated with the virtual machine. 320 * `tags` - (Optional) A mapping of tags to assign to the resource. 321 322 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) 323 324 `Plan` supports the following: 325 326 * `name` - (Required) Specifies the name of the image from the marketplace. 327 * `publisher` - (Optional) Specifies the publisher of the image. 328 * `product` - (Optional) Specifies the product of the image from the marketplace. 329 330 `boot_diagnostics` supports the following: 331 332 * `enabled`: (Required) Whether to enable boot diagnostics for the virtual machine. 333 * `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. 334 335 `storage_image_reference` supports the following: 336 337 * `publisher` - (Required) Specifies the publisher of the image used to create the virtual machine. Changing this forces a new resource to be created. 338 * `offer` - (Required) Specifies the offer of the image used to create the virtual machine. Changing this forces a new resource to be created. 339 * `sku` - (Required) Specifies the SKU of the image used to create the virtual machine. Changing this forces a new resource to be created. 340 * `version` - (Optional) Specifies the version of the image used to create the virtual machine. Changing this forces a new resource to be created. 341 342 `storage_os_disk` supports the following: 343 344 * `name` - (Required) Specifies the disk name. 345 * `vhd_uri` - (Optional) Specifies the vhd uri. Changing this forces a new resource to be created. Cannot be used with managed disks. 346 * `managed_disk_type` - (Optional) Specifies the type of managed disk to create. Value you must be either `Standard_LRS` or `Premium_LRS`. Cannot be used when `vhd_uri` is specified. 347 * `managed_disk_id` - (Optional) Specifies an existing managed disk to use by id. Can only be used when `create_option` is `Attach`. Cannot be used when `vhd_uri` is specified. 348 * `create_option` - (Required) Specifies how the virtual machine should be created. Possible values are `Attach` and `FromImage`. 349 * `caching` - (Optional) Specifies the caching requirements. 350 * `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. 351 * `os_type` - (Optional) Specifies the operating system Type, valid values are windows, linux. 352 * `disk_size_gb` - (Optional) Specifies the size of the data disk in gigabytes. 353 354 `storage_data_disk` supports the following: 355 356 * `name` - (Required) Specifies the name of the data disk. 357 * `vhd_uri` - (Optional) Specifies the uri of the location in storage where the vhd for the virtual machine should be placed. Cannot be used with managed disks. 358 * `managed_disk_type` - (Optional) Specifies the type of managed disk to create. Value you must be either `Standard_LRS` or `Premium_LRS`. Cannot be used when `vhd_uri` is specified. 359 * `managed_disk_id` - (Optional) Specifies an existing managed disk to use by id. Can only be used when `create_option` is `Attach`. Cannot be used when `vhd_uri` is specified. 360 * `create_option` - (Required) Specifies how the data disk should be created. Possible values are `Attach`, `FromImage` and `Empty`. 361 * `disk_size_gb` - (Required) Specifies the size of the data disk in gigabytes. 362 * `caching` - (Optional) Specifies the caching requirements. 363 * `lun` - (Required) Specifies the logical unit number of the data disk. 364 365 `os_profile` supports the following: 366 367 * `computer_name` - (Required) Specifies the name of the virtual machine. 368 * `admin_username` - (Required) Specifies the name of the administrator account. 369 * `admin_password` - (Required) Specifies the password of the administrator account. 370 * `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. 371 372 ~> **NOTE:** `admin_password` must be between 6-72 characters long and must satisfy at least 3 of password complexity requirements from the following: 373 1. Contains an uppercase character 374 2. Contains a lowercase character 375 3. Contains a numeric digit 376 4. Contains a special character 377 378 `os_profile_windows_config` supports the following: 379 380 * `provision_vm_agent` - (Optional) 381 * `enable_automatic_upgrades` - (Optional) 382 * `winrm` - (Optional) A collection of WinRM configuration blocks as documented below. 383 * `additional_unattend_config` - (Optional) An Additional Unattended Config block as documented below. 384 385 `winrm` supports the following: 386 387 * `protocol` - (Required) Specifies the protocol of listener 388 * `certificate_url` - (Optional) Specifies URL of the certificate with which new Virtual Machines is provisioned. 389 390 `additional_unattend_config` supports the following: 391 392 * `pass` - (Required) Specifies the name of the pass that the content applies to. The only allowable value is `oobeSystem`. 393 * `component` - (Required) Specifies the name of the component to configure with the added content. The only allowable value is `Microsoft-Windows-Shell-Setup`. 394 * `setting_name` - (Required) Specifies the name of the setting to which the content applies. Possible values are: `FirstLogonCommands` and `AutoLogon`. 395 * `content` - (Optional) Specifies the base-64 encoded XML formatted content that is added to the unattend.xml file for the specified path and component. 396 397 `os_profile_linux_config` supports the following: 398 399 * `disable_password_authentication` - (Required) Specifies whether password authentication should be disabled. 400 * `ssh_keys` - (Optional) Specifies a collection of `path` and `key_data` to be placed on the virtual machine. 401 402 ~> **Note:** Please note that the only allowed `path` is `/home/<username>/.ssh/authorized_keys` due to a limitation of Azure. 403 404 `os_profile_secrets` supports the following: 405 406 * `source_vault_id` - (Required) Specifies the key vault to use. 407 * `vault_certificates` - (Required) A collection of Vault Certificates as documented below 408 409 `vault_certificates` support the following: 410 411 * `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 412 413 ```json 414 { 415 "data":"<Base64-encoded-certificate>", 416 "dataType":"pfx", 417 "password":"<pfx-file-password>" 418 } 419 ``` 420 421 * `certificate_store` - (Required, on windows machines) Specifies the certificate store on the Virtual Machine where the certificate should be added to. 422 423 ## Attributes Reference 424 425 The following attributes are exported: 426 427 * `id` - The virtual machine ID. 428 429 ## Import 430 431 Virtual Machines can be imported using the `resource id`, e.g. 432 433 ``` 434 terraform import azurerm_virtual_machine.test /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/microsoft.compute/virtualMachines/machine1 435 ```