Skip to content

Commit

Permalink
Optional boot diags, plus autoscale now working
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Cheney committed Jul 24, 2020
1 parent fc7c2ed commit c38f0a0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ provider.tf
provider.tf.example
.terraform
terraform.tfstate*
.*.swp
.*.swp
test.tf
55 changes: 34 additions & 21 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
terraform {
required_version = ">= 0.12.0"
/*
required_providers {
azurerm = ">= 2.20.0"
}
*/

// experiments = [variable_validation]
}

locals {
resource_group_name = coalesce(var.resource_group_name, lookup(var.defaults, "resource_group_name", "unspecified"))
location = coalesce(var.location, var.defaults.location)
tags = merge(lookup(var.defaults, "tags", {}), var.tags)
boot_diagnostics_uri = coalesce(var.boot_diagnostics_uri, var.defaults.boot_diagnostics_uri)
boot_diagnostics_uri = try(coalesce(var.boot_diagnostics_uri, var.defaults.boot_diagnostics_uri), null)
admin_username = coalesce(var.admin_username, var.defaults.admin_username, "ubuntu")
admin_ssh_public_key = try(coalesce(var.admin_ssh_public_key, var.defaults.admin_ssh_public_key), file("~/.ssh/id_rsa.pub"))
additional_ssh_keys = try(coalesce(var.additional_ssh_keys, var.defaults.additional_ssh_keys), [])
subnet_id = coalesce(var.subnet_id, var.defaults.subnet_id)
vm_size = coalesce(var.vm_size, var.defaults.vm_size, "Standard_B1ls")
identity_id = try(coalesce(var.identity_id, var.defaults.identity_id), null)
storage_account_type = coalesce(var.storage_account_type, var.defaults.storage_account_type, "Standard_LRS")

}

resource "azurerm_linux_virtual_machine_scale_set" "vmss" {
Expand Down Expand Up @@ -71,8 +83,12 @@ resource "azurerm_linux_virtual_machine_scale_set" "vmss" {
// enabled = true
// }

boot_diagnostics {
storage_account_uri = local.boot_diagnostics_uri
dynamic "boot_diagnostics" {
for_each = toset(local.boot_diagnostics_uri != null ? [1] : [])

content {
storage_account_uri = local.boot_diagnostics_uri
}
}

dynamic "identity" {
Expand All @@ -93,36 +109,34 @@ resource "azurerm_linux_virtual_machine_scale_set" "vmss" {
}
}

/*
THIS NEEDS VARIABLES AND TESTING FOR SUCCESSFUL AUTOSCALING
HAVE A SET OF THESE AND A VARIABLE TO CHOOSE - PERHAPS SPLIT E.G. CPU-75-25
resource "azurerm_monitor_autoscale_setting" "vmss" {
for_each = toset(var.autoscale != null ? [var.autoscale.rule.metric] : [])

resource "azurerm_monitor_autoscale_setting" "pool3" {
name = "autoscale-config"
resource_group_name = azurerm_resource_group.web.name
location = azurerm_resource_group.web.location
target_resource_id = azurerm_linux_virtual_machine_scale_set.web_pool3.id
depends_on = [azurerm_linux_virtual_machine_scale_set.web_pool3]
resource_group_name = local.resource_group_name
location = local.location
target_resource_id = azurerm_linux_virtual_machine_scale_set.vmss.id
depends_on = [azurerm_linux_virtual_machine_scale_set.vmss]

profile {
name = "AutoScale"

capacity {
default = 2
minimum = 1
maximum = 6
default = var.autoscale.capacity.default
minimum = var.autoscale.capacity.minimum
maximum = var.autoscale.capacity.maximum
}

rule {
metric_trigger {
metric_name = "Percentage CPU"
metric_resource_id = azurerm_linux_virtual_machine_scale_set.web_pool3.id
metric_name = var.autoscale.rule.metric
metric_resource_id = azurerm_linux_virtual_machine_scale_set.vmss.id
time_grain = "PT1M"
statistic = "Average"
time_window = "PT5M"
time_aggregation = "Average"
operator = "GreaterThan"
threshold = 75
threshold = var.autoscale.rule.upper
}

scale_action {
Expand All @@ -135,14 +149,14 @@ resource "azurerm_monitor_autoscale_setting" "pool3" {

rule {
metric_trigger {
metric_name = "Percentage CPU"
metric_resource_id = azurerm_linux_virtual_machine_scale_set.web_pool3.id
metric_name = var.autoscale.rule.metric
metric_resource_id = azurerm_linux_virtual_machine_scale_set.vmss.id
time_grain = "PT1M"
statistic = "Average"
time_window = "PT5M"
time_aggregation = "Average"
operator = "LessThan"
threshold = 25
threshold = var.autoscale.rule.lower
}

scale_action {
Expand All @@ -162,4 +176,3 @@ resource "azurerm_monitor_autoscale_setting" "pool3" {
// }
// }
}
*/
20 changes: 20 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ variable "source_image_reference" {

}

variable "autoscale" {
// Possible host metric values: https://docs.microsoft.com/azure/azure-monitor/platform/metrics-supported#microsoftcomputevirtualmachinescalesets
// e.g. "Percentage CPU"
// Look to add guest metrics?: https://docs.microsoft.com/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-mvss-guest-based-autoscale-linux
// Users could always create their own autoscaling rules to variantsnot covered by this module
type = object({
capacity = object({
default = number
minimum = number
maximum = number
})
rule = object({
metric = string
upper = number
lower = number
})
})
default = null
}

// ==============================================================================

variable "defaults" {
Expand Down

0 comments on commit c38f0a0

Please sign in to comment.