Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add boot diagnostics option #7

Merged
merged 17 commits into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ module "mycompute2" {
}
}
module "network" {
source = "Azure/compute/azurerm"
location = "westus"
source = "Azure/network/azurerm"
location = "westus"
resource_group_name = "mycompute2"
}
}

```
Authors
Expand Down
32 changes: 29 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ resource "azurerm_resource_group" "vm" {
tags = "${var.tags}"
}

resource "random_id" "vm-sa" {
keepers = {
vm_hostname = "${var.vm_hostname}"
}
byte_length = 6
}

resource "azurerm_storage_account" "vm-sa" {
count = "${var.boot_diagnostics == "true" ? 1 : 0}"
name = "bootdiag${lower(random_id.vm-sa.hex)}"
resource_group_name = "${azurerm_resource_group.vm.name}"
location = "${var.location}"
account_type = "${var.boot_diagnostics_sa_type}"
tags = "${var.tags}"
}

resource "azurerm_virtual_machine" "vm-linux" {
count = "${contains(list("${var.vm_os_simple}","${var.vm_os_offer}"), "WindowsServer") ? 0 : var.nb_instances}"
name = "${var.vm_hostname}${count.index}"
Expand All @@ -21,6 +37,7 @@ resource "azurerm_virtual_machine" "vm-linux" {
availability_set_id = "${azurerm_availability_set.vm.id}"
vm_size = "${var.vm_size}"
network_interface_ids = ["${element(azurerm_network_interface.vm.*.id, count.index)}"]
delete_os_disk_on_termination = "${var.delete_os_disk_on_termination}"

storage_image_reference {
id = "${var.vm_os_id}"
Expand All @@ -31,7 +48,7 @@ resource "azurerm_virtual_machine" "vm-linux" {
}

storage_os_disk {
name = "osdisk${count.index}"
name = "osdisk-${var.vm_hostname}-${count.index}"
create_option = "FromImage"
caching = "ReadWrite"
managed_disk_type = "${var.storage_account_type}"
Expand All @@ -52,6 +69,10 @@ resource "azurerm_virtual_machine" "vm-linux" {
key_data = "${file("${var.ssh_key}")}"
}
}
boot_diagnostics {
enabled = "${var.boot_diagnostics}"
storage_uri = "${var.boot_diagnostics == "true" ? join(",", azurerm_storage_account.vm-sa.*.primary_blob_endpoint) : "" }"
}
}

resource "azurerm_virtual_machine" "vm-windows" {
Expand All @@ -62,6 +83,7 @@ resource "azurerm_virtual_machine" "vm-windows" {
availability_set_id = "${azurerm_availability_set.vm.id}"
vm_size = "${var.vm_size}"
network_interface_ids = ["${element(azurerm_network_interface.vm.*.id, count.index)}"]
delete_os_disk_on_termination = "${var.delete_os_disk_on_termination}"

storage_image_reference {
id = "${var.vm_os_id}"
Expand All @@ -83,6 +105,10 @@ resource "azurerm_virtual_machine" "vm-windows" {
admin_username = "${var.admin_username}"
admin_password = "${var.admin_password}"
}
boot_diagnostics {
enabled = "${var.boot_diagnostics}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, it seems this code is not working for me because when I use the default of setting boot_diagnostics to false, I still get these errors:

Error running plan: 1 error(s) occurred:

* module.mycompute.azurerm_virtual_machine.vm-windows: 2 error(s) occurred:

* module.mycompute.azurerm_virtual_machine.vm-windows[1]: Resource 'azurerm_storage_account.vm-sa' not found for variable 'azurerm_storage_account.vm-sa.primary_blob_endpoint'
* module.mycompute.azurerm_virtual_machine.vm-windows[0]: Resource 'azurerm_storage_account.vm-sa' not found for variable 'azurerm_storage_account.vm-sa.primary_blob_endpoint'

When I set it to true, it works as expected. I'm using the latest terraform v0.10.7 with Azure module 0.2.2.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review. I fixed this issue in commit 4cc1a4a.

storage_uri = "${var.boot_diagnostics == "true" ? join(",", azurerm_storage_account.vm-sa.*.primary_blob_endpoint) : "" }"
}
}

resource "azurerm_availability_set" "vm" {
Expand Down Expand Up @@ -123,7 +149,7 @@ resource "azurerm_network_security_group" "vm" {

resource "azurerm_network_interface" "vm" {
count = "${var.nb_instances}"
name = "nic${count.index}"
name = "nic-${var.vm_hostname}-${count.index}"
location = "${azurerm_resource_group.vm.location}"
resource_group_name = "${azurerm_resource_group.vm.name}"
network_security_group_id = "${azurerm_network_security_group.vm.id}"
Expand All @@ -134,4 +160,4 @@ resource "azurerm_network_interface" "vm" {
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${count.index == 0 ? azurerm_public_ip.vm.id : ""}"
}
}
}
15 changes: 15 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,18 @@ variable "public_ip_address_allocation" {
description = "Defines how an IP address is assigned. Options are Static or Dynamic."
default = "static"
}

variable "delete_os_disk_on_termination" {
description = "Delete datadisk when machine is terminated"
default = "false"
}

variable "boot_diagnostics" {
description = "(Optional) Enable or Disable boot diagnostics"
default = "false"
}

variable "boot_diagnostics_sa_type" {
description = "(Optional) Storage account type for boot diagnostics"
default = "Standard_LRS"
}