Skip to content

claranet/terraform-azurerm-autoscale

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure Autoscale

Changelog Notice Apache V2 License OpenTofu Registry

This Terraform module manage autoscaling configuration on a given Azure resource.

This module comes with a default profile working with Virtual Machine Scale Sets and App Service Plans based on Azure Monitor CPU and memory metrics. Also, this module allows to override it with custom profiles and rules.

Global versioning rule for Claranet Azure modules

Module version Terraform version OpenTofu version AzureRM version
>= 8.x.x Unverified 1.8.x >= 4.0
>= 7.x.x 1.3.x >= 3.0
>= 6.x.x 1.x >= 3.0
>= 5.x.x 0.15.x >= 2.0
>= 4.x.x 0.13.x / 0.14.x >= 2.0
>= 3.x.x 0.12.x >= 2.0
>= 2.x.x 0.12.x < 2.0
< 2.x.x 0.11.x < 2.0

Contributing

If you want to contribute to this repository, feel free to use our pre-commit git hook configuration which will help you automatically update and format some files for you by enforcing our Terraform code module best-practices.

More details are available in the CONTRIBUTING.md file.

Usage

This module is optimized to work with the Claranet terraform-wrapper tool which set some terraform variables in the environment needed by this module. More details about variables set by the terraform-wrapper available in the documentation.

⚠️ Since modules version v8.0.0, we do not maintain/check anymore the compatibility with Hashicorp Terraform. Instead, we recommend to use OpenTofu.

module "linux_scaleset" {
  source  = "claranet/linux-scaleset/azurerm"
  version = "x.x.x"

  client_name    = var.client_name
  environment    = var.environment
  stack          = var.stack
  location       = module.azure_region.location
  location_short = module.azure_region.location_short

  resource_group_name = module.rg.name

  admin_username = "myusername"
  ssh_public_key = var.ssh_public_key

  vm_size = "Standard_B2s"

  subnet = {
    id = module.subnet.id
  }

  source_image_reference = {
    publisher = "Debian"
    offer     = "debian-10"
    sku       = "10"
    version   = "latest"
  }

  azure_monitor_data_collection_rule = {
    id = module.run.data_collection_rule_id
  }

  identity = {
    type = "SystemAssigned"
  }
}

module "autoscale" {
  source  = "claranet/autoscale/azurerm"
  version = "x.x.x"

  client_name         = var.client_name
  environment         = var.environment
  stack               = var.stack
  location            = module.azure_region.location
  location_short      = module.azure_region.location_short
  resource_group_name = module.rg.name

  target_resource_id = module.linux_scaleset.id

  profile = {
    "default" = {
      capacity = {
        default = 2
        minimum = 2
        maximum = 5
      }
      rules = [
        {
          metric_trigger = {
            metric_name        = "Percentage CPU"
            metric_resource_id = module.linux_scaleset.id
            time_grain         = "PT1M"
            time_window        = "PT5M"
            time_aggregation   = "Average"
            statistic          = "Average"
            operator           = "GreaterThanOrEqual"
            threshold          = 75
            metric_namespace   = "microsoft.compute/virtualmachinescalesets"
          }

          scale_action = {
            direction = "Increase"
            type      = "ChangeCount"
            value     = "1"
            cooldown  = "PT1M"
          }
        },
        {
          metric_trigger = {
            metric_name        = "Percentage CPU"
            metric_resource_id = module.linux_scaleset.id
            time_grain         = "PT1M"
            time_window        = "PT5M"
            time_aggregation   = "Average"
            statistic          = "Average"
            operator           = "LessThan"
            threshold          = 25
            metric_namespace   = "microsoft.compute/virtualmachinescalesets"
          }

          scale_action = {
            direction = "Decrease"
            type      = "ChangeCount"
            value     = "1"
            cooldown  = "PT1M"
          }
        }
      ]
    }
  }

  notification = {
    email = {
      custom_emails = ["[email protected]"]
    }
  }

  logs_destinations_ids = [module.run.log_analytics_workspace_id]
}

Providers

Name Version
azurecaf ~> 1.2.28
azurerm ~> 4.0

Modules

Name Source Version
diagnostics claranet/diagnostic-settings/azurerm ~> 8.0.0

Resources

Name Type
azurerm_monitor_autoscale_setting.main resource
azurecaf_name.autoscale data source

Inputs

Name Description Type Default Required
autoscale_enabled Specifies whether automatic scaling is enabled for the target resource. bool true no
client_name Client name/account used in naming. string n/a yes
custom_name Custom name for Autoscale setting, generated if not set. string "" no
default_tags_enabled Option to enable or disable default tags. bool true no
diagnostic_settings_custom_name Custom name of the diagnostics settings, name will be 'default' if not set. string "default" no
environment Project environment. string n/a yes
extra_tags Additional tags to associate with your autoscale setting. map(string) {} no
location Azure location/region to use. string n/a yes
location_short Short string for Azure location. string n/a yes
logs_categories Log categories to send to destinations. list(string) null no
logs_destinations_ids List of destination resources IDs for logs diagnostic destination.
Can be Storage Account, Log Analytics Workspace and Event Hub. No more than one of each can be set.
If you want to use Azure EventHub as a destination, you must provide a formatted string containing both the EventHub Namespace authorization send ID and the EventHub name (name of the queue to use in the Namespace) separated by the | character.
list(string) n/a yes
logs_metrics_categories Metrics categories to send to destinations. list(string) null no
name_prefix Optional prefix for the generated name. string "" no
name_suffix Optional suffix for the generated name. string "" no
notification Manage emailing and webhooks for sending notifications.
object({
email = optional(object({
send_to_subscription_administrator = optional(bool, false)
send_to_subscription_co_administrator = optional(bool, false)
custom_emails = optional(list(string))
}))
webhooks = optional(list(object({
service_uri = string
properties = optional(map(string))
})), [])
})
null no
profile One or more (up to 20) autoscale profile blocks.
map(object({
capacity = object({
default = number
minimum = optional(number, 1)
maximum = optional(number, 5)
})
rules = optional(list(object({
metric_trigger = object({
metric_name = string
metric_resource_id = string
operator = string
statistic = string
time_aggregation = string
time_grain = string
time_window = string
threshold = number
metric_namespace = optional(string)
divide_by_instance_count = optional(bool)
dimensions = optional(list(object({
name = string
operator = string
values = list(string)
})), [])
})
scale_action = object({
cooldown = string
direction = string
type = string
value = number
})
})), [])
fixed_date = optional(object({
end = string
start = string
timezone = string
}))
recurrence = optional(object({
timezone = string
days = list(string)
hours = list(number)
minutes = list(number)
}))
}))
n/a yes
resource_group_name Custom resource group name to attach autoscale configuration to. Target resource group by default. string null no
stack Project stack name. string n/a yes
target_resource_id ID of the resource to apply the autoscale setting to. string n/a yes

Outputs

Name Description
id ID of the Azure Autoscale setting.
module_diagnostics Diagnostics Settings module output.
name Name of the Azure Autoscale setting.
resource Azure Autoscale setting resource object.

Related documentation

Microsoft Azure documentation - Virtual Machine Scale Sets Autoscale: docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-autoscale-overview

Microsoft Azure documentation - App Services Autoscale: docs.microsoft.com/en-us/azure/app-service/manage-scale-up

Microsoft Azure documentation - Metrics supported: docs.microsoft.com/en-us/azure/azure-monitor/essentials/metrics-supported