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 "azure_region" {
  source  = "claranet/regions/azurerm"
  version = "x.x.x"

  azure_region = var.azure_region
}

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

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

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

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

  vnet_cidr = ["192.168.0.0/21"]
}

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

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

  virtual_network_name = module.vnet.virtual_network_name
  subnet_cidr_list     = ["192.168.0.0/24"]
}


module "run" {
  source  = "claranet/run/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.resource_group_name

  monitoring_function_enabled = false
  vm_monitoring_enabled       = true
}

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.resource_group_name

  admin_username = "myusername"
  ssh_public_key = var.ssh_public_key

  vms_size = "Standard_B2s"

  subnet_id = module.subnet.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
}

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.resource_group_name

  target_resource_id = module.linux_scaleset.scale_set_id

  autoscale_profile = {
    "default" = {
      capacity = {
        default = 2
        minimum = 2
        maximum = 5
      }
      rules = [
        {
          metric_trigger = {
            metric_name        = "Percentage CPU"
            metric_resource_id = module.linux_scaleset.scale_set_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.scale_set_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 = ["myemail@example.com"]
    }
  }

  logs_destinations_ids = [module.run.log_analytics_workspace_id]
}

Providers

Name Version
azurecaf ~> 1.2, >= 1.2.22
azurerm ~> 3.39

Modules

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

Resources

Name Type
azurerm_monitor_autoscale_setting.autoscale resource
azurecaf_name.autoscale data source

Inputs

Name Description Type Default Required
autoscale_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
client_name Client name/account used in naming. string n/a yes
custom_autoscale_setting_name Custom Autoscale setting name string "" no
custom_diagnostic_settings_name Custom name of the diagnostics settings, name will be 'default' if not set. string "default" no
default_tags_enabled Option to enable or disable default tags bool true no
enable_autoscale Specifies whether automatic scaling is enabled for the target resource. bool true 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 specify an Azure EventHub to send logs and metrics to, you need to provide a formated string with 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
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
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
use_caf_naming Use the Azure CAF naming provider to generate default resource name. custom_autoscale_setting_name override this if set. Legacy default name is used if this is set to false. bool true no

Outputs

Name Description
autoscale_setting_id Azure Autoscale setting ID
autoscale_setting_name Azure Autoscale setting name

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