From f0131013adbfb1b3b16e916db183842d729268fb Mon Sep 17 00:00:00 2001 From: Greta Quadrati <75862507+gquadrati@users.noreply.github.com> Date: Mon, 22 Apr 2024 09:27:17 +0200 Subject: [PATCH] [#IOPID-1781] Set basic, morning and evening profiles for appbackend and fn-app (#957) --- src/core/app_backend.tf | 660 ++++++++++++++++++----------- src/core/env/prod/terraform.tfvars | 4 +- src/core/function_app.tf | 252 +++++++---- 3 files changed, 574 insertions(+), 342 deletions(-) diff --git a/src/core/app_backend.tf b/src/core/app_backend.tf index 6dd6d1d5c..c3703103a 100644 --- a/src/core/app_backend.tf +++ b/src/core/app_backend.tf @@ -373,6 +373,64 @@ locals { pn_api_url_prod = "https://api-io.notifichedigitali.it" + autoscale_profiles = [ + { + name = "{\"name\":\"default\",\"for\":\"evening\"}", + + recurrence = { + hours = 22 + minutes = 59 + } + + capacity = { + default = var.app_backend_autoscale_default + 1 + minimum = var.app_backend_autoscale_minimum + 1 + maximum = var.app_backend_autoscale_maximum + } + }, + { + name = "{\"name\":\"default\",\"for\":\"night\"}", + + recurrence = { + hours = 5 + minutes = 0 + } + + capacity = { + default = var.app_backend_autoscale_default + 1 + minimum = var.app_backend_autoscale_minimum + 1 + maximum = var.app_backend_autoscale_maximum + } + }, + { + name = "evening" + + recurrence = { + hours = 19 + minutes = 30 + } + + capacity = { + default = var.app_backend_autoscale_default + 2 + minimum = var.app_backend_autoscale_minimum + 2 + maximum = var.app_backend_autoscale_maximum + } + }, + { + name = "night" + + recurrence = { + hours = 23 + minutes = 0 + } + + capacity = { + default = var.app_backend_autoscale_default + minimum = var.app_backend_autoscale_minimum + maximum = var.app_backend_autoscale_maximum + } + } + ] } resource "azurerm_resource_group" "rg_linux" { @@ -748,104 +806,134 @@ resource "azurerm_monitor_autoscale_setting" "appservice_app_backendl1" { location = azurerm_resource_group.rg_linux.location target_resource_id = module.appservice_app_backendl1.plan_id - profile { - name = "default" - capacity { - default = var.app_backend_autoscale_default - minimum = var.app_backend_autoscale_minimum - maximum = var.app_backend_autoscale_maximum - } - - # Increase rules - - rule { - metric_trigger { - metric_name = "Requests" - metric_resource_id = module.appservice_app_backendl1.id - metric_namespace = "microsoft.web/sites" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT1M" - time_aggregation = "Average" - operator = "GreaterThan" - threshold = 4000 - divide_by_instance_count = false + # Scaling strategy + # 05 - 19,30 -> min 3 + # 19,30 - 23 -> min 4 + # 23 - 05 -> min 2 + dynamic "profile" { + for_each = local.autoscale_profiles + iterator = profile_info + + content { + name = profile_info.value.name + + dynamic "recurrence" { + for_each = profile_info.value.recurrence != null ? [profile_info.value.recurrence] : [] + iterator = recurrence_info + + content { + timezone = "W. Europe Standard Time" + hours = [recurrence_info.value.hours] + minutes = [recurrence_info.value.minutes] + days = [ + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday" + ] + } } - scale_action { - direction = "Increase" - type = "ChangeCount" - value = "2" - cooldown = "PT1M" + capacity { + default = profile_info.value.capacity.default + minimum = profile_info.value.capacity.minimum + maximum = profile_info.value.capacity.maximum } - } - rule { - metric_trigger { - metric_name = "CpuPercentage" - metric_resource_id = module.appservice_app_backendl1.plan_id - metric_namespace = "microsoft.web/serverfarms" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT1M" - time_aggregation = "Average" - operator = "GreaterThan" - threshold = 40 - divide_by_instance_count = false - } + # Increase rules + + rule { + metric_trigger { + metric_name = "Requests" + metric_resource_id = module.appservice_app_backendl1.id + metric_namespace = "microsoft.web/sites" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT1M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 4000 + divide_by_instance_count = false + } - scale_action { - direction = "Increase" - type = "ChangeCount" - value = "2" - cooldown = "PT1M" + scale_action { + direction = "Increase" + type = "ChangeCount" + value = "2" + cooldown = "PT1M" + } } - } - # Decrease rules - - rule { - metric_trigger { - metric_name = "Requests" - metric_resource_id = module.appservice_app_backendl1.id - metric_namespace = "microsoft.web/sites" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT15M" - time_aggregation = "Average" - operator = "LessThan" - threshold = 1500 - divide_by_instance_count = false - } + rule { + metric_trigger { + metric_name = "CpuPercentage" + metric_resource_id = module.appservice_app_backendl1.plan_id + metric_namespace = "microsoft.web/serverfarms" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT1M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 40 + divide_by_instance_count = false + } - scale_action { - direction = "Decrease" - type = "ChangeCount" - value = "1" - cooldown = "PT30M" + scale_action { + direction = "Increase" + type = "ChangeCount" + value = "2" + cooldown = "PT1M" + } } - } - rule { - metric_trigger { - metric_name = "CpuPercentage" - metric_resource_id = module.appservice_app_backendl1.plan_id - metric_namespace = "microsoft.web/serverfarms" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT15M" - time_aggregation = "Average" - operator = "LessThan" - threshold = 15 - divide_by_instance_count = false + # Decrease rules + + rule { + metric_trigger { + metric_name = "Requests" + metric_resource_id = module.appservice_app_backendl1.id + metric_namespace = "microsoft.web/sites" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT15M" + time_aggregation = "Average" + operator = "LessThan" + threshold = 1500 + divide_by_instance_count = false + } + + scale_action { + direction = "Decrease" + type = "ChangeCount" + value = "1" + cooldown = "PT30M" + } } - scale_action { - direction = "Decrease" - type = "ChangeCount" - value = "1" - cooldown = "PT30M" + rule { + metric_trigger { + metric_name = "CpuPercentage" + metric_resource_id = module.appservice_app_backendl1.plan_id + metric_namespace = "microsoft.web/serverfarms" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT15M" + time_aggregation = "Average" + operator = "LessThan" + threshold = 15 + divide_by_instance_count = false + } + + scale_action { + direction = "Decrease" + type = "ChangeCount" + value = "1" + cooldown = "PT30M" + } } } } @@ -967,105 +1055,134 @@ resource "azurerm_monitor_autoscale_setting" "appservice_app_backendl2" { location = azurerm_resource_group.rg_linux.location target_resource_id = module.appservice_app_backendl2.plan_id - profile { - name = "default" + # Scaling strategy + # 05 - 19,30 -> min 3 + # 19,30 - 23 -> min 4 + # 23 - 05 -> min 2 + dynamic "profile" { + for_each = local.autoscale_profiles + iterator = profile_info + + content { + name = profile_info.value.name + + dynamic "recurrence" { + for_each = profile_info.value.recurrence != null ? [profile_info.value.recurrence] : [] + iterator = recurrence_info + + content { + timezone = "W. Europe Standard Time" + hours = [recurrence_info.value.hours] + minutes = [recurrence_info.value.minutes] + days = [ + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday" + ] + } + } - capacity { - default = var.app_backend_autoscale_default - minimum = var.app_backend_autoscale_minimum - maximum = var.app_backend_autoscale_maximum - } + capacity { + default = profile_info.value.capacity.default + minimum = profile_info.value.capacity.minimum + maximum = profile_info.value.capacity.maximum + } - # Increase rules - - rule { - metric_trigger { - metric_name = "Requests" - metric_resource_id = module.appservice_app_backendl2.id - metric_namespace = "microsoft.web/sites" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT1M" - time_aggregation = "Average" - operator = "GreaterThan" - threshold = 4000 - divide_by_instance_count = false - } + # Increase rules + + rule { + metric_trigger { + metric_name = "Requests" + metric_resource_id = module.appservice_app_backendl2.id + metric_namespace = "microsoft.web/sites" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT1M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 4000 + divide_by_instance_count = false + } - scale_action { - direction = "Increase" - type = "ChangeCount" - value = "2" - cooldown = "PT1M" + scale_action { + direction = "Increase" + type = "ChangeCount" + value = "2" + cooldown = "PT1M" + } } - } - rule { - metric_trigger { - metric_name = "CpuPercentage" - metric_resource_id = module.appservice_app_backendl2.plan_id - metric_namespace = "microsoft.web/serverfarms" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT1M" - time_aggregation = "Average" - operator = "GreaterThan" - threshold = 40 - divide_by_instance_count = false - } + rule { + metric_trigger { + metric_name = "CpuPercentage" + metric_resource_id = module.appservice_app_backendl2.plan_id + metric_namespace = "microsoft.web/serverfarms" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT1M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 40 + divide_by_instance_count = false + } - scale_action { - direction = "Increase" - type = "ChangeCount" - value = "2" - cooldown = "PT1M" + scale_action { + direction = "Increase" + type = "ChangeCount" + value = "2" + cooldown = "PT1M" + } } - } - # Decrease rules - - rule { - metric_trigger { - metric_name = "Requests" - metric_resource_id = module.appservice_app_backendl2.id - metric_namespace = "microsoft.web/sites" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT15M" - time_aggregation = "Average" - operator = "LessThan" - threshold = 1500 - divide_by_instance_count = false - } + # Decrease rules + + rule { + metric_trigger { + metric_name = "Requests" + metric_resource_id = module.appservice_app_backendl2.id + metric_namespace = "microsoft.web/sites" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT15M" + time_aggregation = "Average" + operator = "LessThan" + threshold = 1500 + divide_by_instance_count = false + } - scale_action { - direction = "Decrease" - type = "ChangeCount" - value = "1" - cooldown = "PT30M" + scale_action { + direction = "Decrease" + type = "ChangeCount" + value = "1" + cooldown = "PT30M" + } } - } - rule { - metric_trigger { - metric_name = "CpuPercentage" - metric_resource_id = module.appservice_app_backendl2.plan_id - metric_namespace = "microsoft.web/serverfarms" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT15M" - time_aggregation = "Average" - operator = "LessThan" - threshold = 15 - divide_by_instance_count = false - } + rule { + metric_trigger { + metric_name = "CpuPercentage" + metric_resource_id = module.appservice_app_backendl2.plan_id + metric_namespace = "microsoft.web/serverfarms" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT15M" + time_aggregation = "Average" + operator = "LessThan" + threshold = 15 + divide_by_instance_count = false + } - scale_action { - direction = "Decrease" - type = "ChangeCount" - value = "1" - cooldown = "PT30M" + scale_action { + direction = "Decrease" + type = "ChangeCount" + value = "1" + cooldown = "PT30M" + } } } } @@ -1189,104 +1306,133 @@ resource "azurerm_monitor_autoscale_setting" "appservice_app_backendli" { location = azurerm_resource_group.rg_linux.location target_resource_id = module.appservice_app_backendli.plan_id - profile { - name = "default" - - capacity { - default = var.app_backend_autoscale_default - minimum = var.app_backend_autoscale_minimum - maximum = var.app_backend_autoscale_maximum - } - - # Increase rules - - rule { - metric_trigger { - metric_name = "Requests" - metric_resource_id = module.appservice_app_backendli.id - metric_namespace = "microsoft.web/sites" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT5M" - time_aggregation = "Average" - operator = "GreaterThan" - threshold = 4000 - divide_by_instance_count = false + # Scaling strategy + # 05 - 19,30 -> min 3 + # 19,30 - 23 -> min 4 + # 23 - 05 -> min 2 + dynamic "profile" { + for_each = local.autoscale_profiles + iterator = profile_info + + content { + name = profile_info.value.name + + dynamic "recurrence" { + for_each = profile_info.value.recurrence != null ? [profile_info.value.recurrence] : [] + iterator = recurrence_info + + content { + timezone = "W. Europe Standard Time" + hours = [recurrence_info.value.hours] + minutes = [recurrence_info.value.minutes] + days = [ + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday" + ] + } } - scale_action { - direction = "Increase" - type = "ChangeCount" - value = "2" - cooldown = "PT5M" + capacity { + default = profile_info.value.capacity.default + minimum = profile_info.value.capacity.minimum + maximum = profile_info.value.capacity.maximum } - } - rule { - metric_trigger { - metric_name = "CpuPercentage" - metric_resource_id = module.appservice_app_backendli.plan_id - metric_namespace = "microsoft.web/serverfarms" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT5M" - time_aggregation = "Average" - operator = "GreaterThan" - threshold = 40 - divide_by_instance_count = false - } + # Increase rules + + rule { + metric_trigger { + metric_name = "Requests" + metric_resource_id = module.appservice_app_backendli.id + metric_namespace = "microsoft.web/sites" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT5M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 4000 + divide_by_instance_count = false + } - scale_action { - direction = "Increase" - type = "ChangeCount" - value = "2" - cooldown = "PT5M" + scale_action { + direction = "Increase" + type = "ChangeCount" + value = "2" + cooldown = "PT5M" + } } - } - # Decrease rules - - rule { - metric_trigger { - metric_name = "Requests" - metric_resource_id = module.appservice_app_backendli.id - metric_namespace = "microsoft.web/sites" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT5M" - time_aggregation = "Average" - operator = "LessThan" - threshold = 1500 - divide_by_instance_count = false - } + rule { + metric_trigger { + metric_name = "CpuPercentage" + metric_resource_id = module.appservice_app_backendli.plan_id + metric_namespace = "microsoft.web/serverfarms" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT5M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 40 + divide_by_instance_count = false + } - scale_action { - direction = "Decrease" - type = "ChangeCount" - value = "1" - cooldown = "PT30M" + scale_action { + direction = "Increase" + type = "ChangeCount" + value = "2" + cooldown = "PT5M" + } } - } - rule { - metric_trigger { - metric_name = "CpuPercentage" - metric_resource_id = module.appservice_app_backendli.plan_id - metric_namespace = "microsoft.web/serverfarms" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT5M" - time_aggregation = "Average" - operator = "LessThan" - threshold = 15 - divide_by_instance_count = false + # Decrease rules + + rule { + metric_trigger { + metric_name = "Requests" + metric_resource_id = module.appservice_app_backendli.id + metric_namespace = "microsoft.web/sites" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT5M" + time_aggregation = "Average" + operator = "LessThan" + threshold = 1500 + divide_by_instance_count = false + } + + scale_action { + direction = "Decrease" + type = "ChangeCount" + value = "1" + cooldown = "PT30M" + } } - scale_action { - direction = "Decrease" - type = "ChangeCount" - value = "1" - cooldown = "PT30M" + rule { + metric_trigger { + metric_name = "CpuPercentage" + metric_resource_id = module.appservice_app_backendli.plan_id + metric_namespace = "microsoft.web/serverfarms" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT5M" + time_aggregation = "Average" + operator = "LessThan" + threshold = 15 + divide_by_instance_count = false + } + + scale_action { + direction = "Decrease" + type = "ChangeCount" + value = "1" + cooldown = "PT30M" + } } } } diff --git a/src/core/env/prod/terraform.tfvars b/src/core/env/prod/terraform.tfvars index 61d2d6e84..3801fbd15 100644 --- a/src/core/env/prod/terraform.tfvars +++ b/src/core/env/prod/terraform.tfvars @@ -198,7 +198,7 @@ ehns_metric_alerts = { function_app_kind = "Linux" function_app_sku_tier = "PremiumV3" function_app_sku_size = "P1v3" -function_app_autoscale_minimum = 3 +function_app_autoscale_minimum = 2 function_app_autoscale_maximum = 30 function_app_autoscale_default = 10 @@ -235,7 +235,7 @@ function_public_autoscale_maximum = 30 function_public_autoscale_default = 10 app_backend_autoscale_default = 10 -app_backend_autoscale_minimum = 4 +app_backend_autoscale_minimum = 2 app_backend_autoscale_maximum = 30 # Function CDN Assets diff --git a/src/core/function_app.tf b/src/core/function_app.tf index 04f3b89db..2d0fe0ab6 100644 --- a/src/core/function_app.tf +++ b/src/core/function_app.tf @@ -299,100 +299,186 @@ resource "azurerm_monitor_autoscale_setting" "function_app" { location = var.location target_resource_id = module.function_app[count.index].app_service_plan_id - profile { - name = "default" - - capacity { - default = var.function_app_autoscale_default - minimum = var.function_app_autoscale_minimum - maximum = var.function_app_autoscale_maximum - } - - rule { - metric_trigger { - metric_name = "Requests" - metric_resource_id = module.function_app[count.index].id - metric_namespace = "microsoft.web/sites" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT1M" - time_aggregation = "Average" - operator = "GreaterThan" - threshold = 3000 - divide_by_instance_count = false - } - - scale_action { - direction = "Increase" - type = "ChangeCount" - value = "2" - cooldown = "PT1M" + # Scaling strategy + # 05 - 19,30 -> min 3 + # 19,30 - 23 -> min 4 + # 23 - 05 -> min 2 + dynamic "profile" { + for_each = [ + { + name = "{\"name\":\"default\",\"for\":\"evening\"}", + + recurrence = { + hours = 22 + minutes = 59 + } + + capacity = { + default = var.function_app_autoscale_default + 1 + minimum = var.function_app_autoscale_minimum + 1 + maximum = var.function_app_autoscale_maximum + } + }, + { + name = "{\"name\":\"default\",\"for\":\"night\"}", + + recurrence = { + hours = 5 + minutes = 0 + } + + capacity = { + default = var.function_app_autoscale_default + 1 + minimum = var.function_app_autoscale_minimum + 1 + maximum = var.function_app_autoscale_maximum + } + }, + { + name = "evening" + + recurrence = { + hours = 19 + minutes = 30 + } + + capacity = { + default = var.function_app_autoscale_default + 2 + minimum = var.function_app_autoscale_minimum + 2 + maximum = var.function_app_autoscale_maximum + } + }, + { + name = "night" + + recurrence = { + hours = 23 + minutes = 0 + } + + capacity = { + default = var.function_app_autoscale_default + minimum = var.function_app_autoscale_minimum + maximum = var.function_app_autoscale_maximum + } } - } - - rule { - metric_trigger { - metric_name = "CpuPercentage" - metric_resource_id = module.function_app[count.index].app_service_plan_id - metric_namespace = "microsoft.web/serverfarms" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT5M" - time_aggregation = "Average" - operator = "GreaterThan" - threshold = 45 - divide_by_instance_count = false + ] + iterator = profile_info + + content { + name = profile_info.value.name + + dynamic "recurrence" { + for_each = profile_info.value.recurrence != null ? [profile_info.value.recurrence] : [] + iterator = recurrence_info + + content { + timezone = "W. Europe Standard Time" + hours = [recurrence_info.value.hours] + minutes = [recurrence_info.value.minutes] + days = [ + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday" + ] + } } - scale_action { - direction = "Increase" - type = "ChangeCount" - value = "2" - cooldown = "PT5M" + capacity { + default = profile_info.value.capacity.default + minimum = profile_info.value.capacity.minimum + maximum = profile_info.value.capacity.maximum } - } - rule { - metric_trigger { - metric_name = "Requests" - metric_resource_id = module.function_app[count.index].id - metric_namespace = "microsoft.web/sites" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT15M" - time_aggregation = "Average" - operator = "LessThan" - threshold = 2000 - divide_by_instance_count = false + rule { + metric_trigger { + metric_name = "Requests" + metric_resource_id = module.function_app[count.index].id + metric_namespace = "microsoft.web/sites" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT1M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 3000 + divide_by_instance_count = false + } + + scale_action { + direction = "Increase" + type = "ChangeCount" + value = "2" + cooldown = "PT1M" + } } - scale_action { - direction = "Decrease" - type = "ChangeCount" - value = "1" - cooldown = "PT10M" + rule { + metric_trigger { + metric_name = "CpuPercentage" + metric_resource_id = module.function_app[count.index].app_service_plan_id + metric_namespace = "microsoft.web/serverfarms" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT5M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 45 + divide_by_instance_count = false + } + + scale_action { + direction = "Increase" + type = "ChangeCount" + value = "2" + cooldown = "PT5M" + } } - } - rule { - metric_trigger { - metric_name = "CpuPercentage" - metric_resource_id = module.function_app[count.index].app_service_plan_id - metric_namespace = "microsoft.web/serverfarms" - time_grain = "PT1M" - statistic = "Average" - time_window = "PT15M" - time_aggregation = "Average" - operator = "LessThan" - threshold = 30 - divide_by_instance_count = false + rule { + metric_trigger { + metric_name = "Requests" + metric_resource_id = module.function_app[count.index].id + metric_namespace = "microsoft.web/sites" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT15M" + time_aggregation = "Average" + operator = "LessThan" + threshold = 2000 + divide_by_instance_count = false + } + + scale_action { + direction = "Decrease" + type = "ChangeCount" + value = "1" + cooldown = "PT10M" + } } - scale_action { - direction = "Decrease" - type = "ChangeCount" - value = "1" - cooldown = "PT10M" + rule { + metric_trigger { + metric_name = "CpuPercentage" + metric_resource_id = module.function_app[count.index].app_service_plan_id + metric_namespace = "microsoft.web/serverfarms" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT15M" + time_aggregation = "Average" + operator = "LessThan" + threshold = 30 + divide_by_instance_count = false + } + + scale_action { + direction = "Decrease" + type = "ChangeCount" + value = "1" + cooldown = "PT10M" + } } } }