Skip to content

Commit

Permalink
Examples of using Remote-Exec Provisioners for Windows and Linux (#1665)
Browse files Browse the repository at this point in the history
* Adding an example of using a remote-exec provisioner on linux

* Adding an example of using WinRM to run commands on a machine

* Adding an example of a VM with Unmanaged Disks

* Example of provisioning a Managed Disks VM from a Custom Image (ported from the VM Docs)

* Example of provisioning a Basic VM with Managed Disks

* Correctly documenting the delete methods

* Adding an example of attaching data disks to a vm

* Fixing the readme

* Updating the VM Docs to be clearer

* Fixing a bug in the attach disks demo

* additional validation for the vm resource

* Switching over to Standard_F2's for the examples
  • Loading branch information
tombuildsstuff authored Jul 30, 2018
1 parent 377b243 commit d88d774
Show file tree
Hide file tree
Showing 32 changed files with 1,079 additions and 368 deletions.
18 changes: 17 additions & 1 deletion azurerm/resource_arm_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ func resourceArmVirtualMachine() *schema.Resource {
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"HTTP",
"HTTPS",
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},
"certificate_url": {
Type: schema.TypeString,
Expand All @@ -427,17 +432,28 @@ func resourceArmVirtualMachine() *schema.Resource {
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// TODO: should we make `pass` and `component` Optional + Defaulted?
"pass": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"oobeSystem",
}, false),
},
"component": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"Microsoft-Windows-Shell-Setup",
}, false),
},
"setting_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"AutoLogon",
"FirstLogonCommands",
}, false),
},
"content": {
Type: schema.TypeString,
Expand Down Expand Up @@ -474,7 +490,7 @@ func resourceArmVirtualMachine() *schema.Resource {
},
"key_data": {
Type: schema.TypeString,
Optional: true,
Required: true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
resource "azurerm_resource_group" "main" {
name = "${var.prefix}-resources"
location = "${var.location}"
tags = "${var.tags}"
}

resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
tags = "${var.tags}"
}

resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = "${azurerm_resource_group.main.name}"
virtual_network_name = "${azurerm_virtual_network.main.name}"
address_prefix = "10.0.2.0/24"
}

resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
tags = "${var.tags}"

ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.internal.id}"
private_ip_address_allocation = "dynamic"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
resource "azurerm_virtual_machine" "main" {
name = "${var.prefix}-vm"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_F2"

# This means the OS Disk will be deleted when Terraform destroys the Virtual Machine
# NOTE: This may not be optimal in all cases.
delete_os_disk_on_termination = true

# This means the Data Disk Disk will be deleted when Terraform destroys the Virtual Machine
# NOTE: This may not be optimal in all cases.
delete_data_disks_on_termination = true

storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.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 = "${var.tags}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "azurerm_managed_disk" "external" {
count = "${var.number_of_disks}"
name = "${var.prefix}-disk${count.index+1}"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "10"
}

resource "azurerm_virtual_machine_data_disk_attachment" "external" {
count = "${var.number_of_disks}"
managed_disk_id = "${azurerm_managed_disk.external.*.id[count.index]}"
virtual_machine_id = "${azurerm_virtual_machine.main.id}"
lun = "${10+count.index}"
caching = "ReadWrite"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Example: Virtual Machine with Data Disks

This example provisions a Virtual Machine with 2 Data Disks and an OS Disk all of which are Managed Disks.

Notes:

- The files involved in this example are split out to make it easier to read, however all of the resources could be combined into a single file if needed.

### Variables

* `prefix` - (Required) The Prefix used for all resources in this example.
* `location` - (Required) The Azure Region in which the resources in this example should exist.
* `tags` - (Optional) Any tags which should be assigned to the resources in this example.
* `number_of_disks` - (Optional) The number of Data Disks which should be attached, defaults to `2`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
variable "prefix" {
description = "The Prefix used for all resources in this example"
}

variable "location" {
description = "The Azure Region in which the resources in this example should exist"
}

variable "tags" {
type = "map"
default = {}
description = "Any tags which should be assigned to the resources in this example"
}

variable "number_of_disks" {
description = "The number of Data Disks which should be attached"
default = 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
resource "azurerm_resource_group" "main" {
name = "${var.prefix}-resources"
location = "${var.location}"
tags = "${var.tags}"
}

resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
tags = "${var.tags}"
}

resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = "${azurerm_resource_group.main.name}"
virtual_network_name = "${azurerm_virtual_network.main.name}"
address_prefix = "10.0.2.0/24"
}

resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
tags = "${var.tags}"

ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.internal.id}"
private_ip_address_allocation = "dynamic"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
resource "azurerm_virtual_machine" "test" {
name = "${var.prefix}-vm"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_F2"

# This means the OS Disk will be deleted when Terraform destroys the Virtual Machine
# NOTE: This may not be optimal in all cases.
delete_os_disk_on_termination = true

# This means the Data Disk Disk will be deleted when Terraform destroys the Virtual Machine
# NOTE: This may not be optimal in all cases.
delete_data_disks_on_termination = true

storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.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 = "${var.tags}"
}
13 changes: 13 additions & 0 deletions examples/virtual-machines/managed-disks/basic-osdisk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Example: Basic Virtual Machine using a Managed Disk

This example provisions a Virtual Machine with no Data Disks with a Managed Disk as the main OS Disk.

Notes:

- The files involved in this example are split out to make it easier to read, however all of the resources could be combined into a single file if needed.

### Variables

* `prefix` - (Required) The Prefix used for all resources in this example.
* `location` - (Required) The Azure Region in which the resources in this example should exist.
* `tags` - (Optional) Any tags which should be assigned to the resources in this example.
13 changes: 13 additions & 0 deletions examples/virtual-machines/managed-disks/basic-osdisk/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "prefix" {
description = "The Prefix used for all resources in this example"
}

variable "location" {
description = "The Azure Region in which the resources in this example should exist"
}

variable "tags" {
type = "map"
default = {}
description = "Any tags which should be assigned to the resources in this example"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
resource "azurerm_resource_group" "main" {
name = "${var.prefix}-resources"
location = "${var.location}"
tags = "${var.tags}"
}

resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
tags = "${var.tags}"
}

resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = "${azurerm_resource_group.main.name}"
virtual_network_name = "${azurerm_virtual_network.main.name}"
address_prefix = "10.0.2.0/24"
}

resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
tags = "${var.tags}"

ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.internal.id}"
private_ip_address_allocation = "dynamic"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# we assume that this Custom Image already exists
data "azurerm_image" "custom" {
name = "${var.custom_image_name}"
resource_group_name = "${var.custom_image_resource_group_name}"
}

resource "azurerm_virtual_machine" "test" {
name = "${var.prefix}-vm"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_F2"

# This means the OS Disk will be deleted when Terraform destroys the Virtual Machine
# NOTE: This may not be optimal in all cases.
delete_os_disk_on_termination = true

# This means the Data Disk Disk will be deleted when Terraform destroys the Virtual Machine
# NOTE: This may not be optimal in all cases.
delete_data_disks_on_termination = true

storage_image_reference {
id = "${data.azurerm_image.custom.id}"
}

storage_os_disk {
name = "osdisk"
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 = "${var.tags}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Example: Virtual Machine with Managed Disks from a Custom Image

This example provisions a Virtual Machine with Managed Disks from a Custom Image that already exists.

Notes:

- The files involved in this example are split out to make it easier to read, however all of the resources could be combined into a single file if needed.
- This example assumes the Custom Image specified exists - if it doesn't this example will fail.

### Variables

* `prefix` - (Required) The Prefix used for all resources in this example.
* `location` - (Required) The Azure Region in which the resources in this example should exist.
* `tags` - (Optional) Any tags which should be assigned to the resources in this example.

* `custom_image_resource_group_name` - (Required) The name of the Resource Group in which the Custom Image exists.
* `custom_image_name` - (Required) The name of the Custom Image to provision this Virtual Machine from.
Loading

0 comments on commit d88d774

Please sign in to comment.