From f448447dae1ea08b32ee572cee50d2b042a6b1d4 Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 2 May 2024 10:34:57 +0100 Subject: [PATCH 01/11] added spot capacity --- main.tf | 14 +++++++++++--- variables.tf | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/main.tf b/main.tf index 25e05e3..d86b6cd 100644 --- a/main.tf +++ b/main.tf @@ -18,13 +18,21 @@ locals { capacity_providers = var.image_build_details["buildx"] == "true" && can(regexall("^arm64", var.image_build_details["platforms"])) ? [ { capacity_provider = "${var.ecs_cluster}-native-scaling" - weight = 1 + weight = 2 }, + { + capacity_provider = "${var.ecs_cluster}-native-spot-scaling" + weight = var.capacity_provider_spot_tasks_weight + } ] : [ { capacity_provider = "${var.ecs_cluster}-native-scaling" - weight = 1 - }, + weight = 2 + }, + { + capacity_provider = "${var.ecs_cluster}-native-spot-scaling" + weight = var.capacity_provider_spot_tasks_weight + } ] } diff --git a/variables.tf b/variables.tf index ce01e28..5d9695b 100644 --- a/variables.tf +++ b/variables.tf @@ -269,4 +269,10 @@ variable "image_build_details" { "buildx" = "false", "platforms" = "" } +} + +variable "capacity_provider_spot_tasks_weight" { + default = 1 + type = number + description = "Weight " } \ No newline at end of file From f4d43e17158ea82a91633ce6a999efa0dc70740d Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 2 May 2024 11:57:00 +0100 Subject: [PATCH 02/11] improved weight calculation --- main.tf | 16 ++++++++++------ variables.tf | 6 +++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/main.tf b/main.tf index d86b6cd..cb7401f 100644 --- a/main.tf +++ b/main.tf @@ -1,6 +1,10 @@ locals { service_name = "${var.env}-${var.release["component"]}" full_service_name = "${local.service_name}${var.name_suffix}" + lower_weight = ceil((var.spot_capacity_percentage / 100)) + high_weight = floor(local.lower_weight / (var.spot_capacity_percentage / 100)) + spot_weight = var.spot_capacity_percentage <= 50 ? local.lower_weight : local.high_weight + ondemand_weight = var.spot_capacity_percentage <= 50 ? local.high_weight : local.lower_weight } module "ecs_update_monitor" { @@ -18,20 +22,20 @@ locals { capacity_providers = var.image_build_details["buildx"] == "true" && can(regexall("^arm64", var.image_build_details["platforms"])) ? [ { capacity_provider = "${var.ecs_cluster}-native-scaling" - weight = 2 + weight = local.ondemand_weight }, { - capacity_provider = "${var.ecs_cluster}-native-spot-scaling" - weight = var.capacity_provider_spot_tasks_weight + capacity_provider = "${var.ecs_cluster}-native-scaling-spot" + weight = local.spot_weight } ] : [ { capacity_provider = "${var.ecs_cluster}-native-scaling" - weight = 2 + weight = local.ondemand_weight }, { - capacity_provider = "${var.ecs_cluster}-native-spot-scaling" - weight = var.capacity_provider_spot_tasks_weight + capacity_provider = "${var.ecs_cluster}-native-scaling-spot" + weight = local.spot_weight } ] } diff --git a/variables.tf b/variables.tf index 5d9695b..d701fda 100644 --- a/variables.tf +++ b/variables.tf @@ -271,8 +271,8 @@ variable "image_build_details" { } } -variable "capacity_provider_spot_tasks_weight" { - default = 1 +variable "spot_capacity_percentage" { + default = 20 type = number - description = "Weight " + description = "Percentage of tasks to run on spot instances" } \ No newline at end of file From 3b3538386cc9c39027387d4bdb62fe09dcef617d Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 2 May 2024 12:31:49 +0100 Subject: [PATCH 03/11] fix devide by 0 --- main.tf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.tf b/main.tf index cb7401f..4465b65 100644 --- a/main.tf +++ b/main.tf @@ -2,9 +2,9 @@ locals { service_name = "${var.env}-${var.release["component"]}" full_service_name = "${local.service_name}${var.name_suffix}" lower_weight = ceil((var.spot_capacity_percentage / 100)) - high_weight = floor(local.lower_weight / (var.spot_capacity_percentage / 100)) - spot_weight = var.spot_capacity_percentage <= 50 ? local.lower_weight : local.high_weight - ondemand_weight = var.spot_capacity_percentage <= 50 ? local.high_weight : local.lower_weight + higher_weight = local.lower_weight == 0 ? 1 : ceil(local.lower_weight / (var.spot_capacity_percentage / 100)) - 1 + spot_weight = var.spot_capacity_percentage <= 50 ? local.lower_weight : local.higher_weight + ondemand_weight = var.spot_capacity_percentage <= 50 ? local.higher_weight : local.lower_weight } module "ecs_update_monitor" { From 03b1cd2d818bb17e3167e06f92c2debd0fdd33e5 Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 2 May 2024 12:45:31 +0100 Subject: [PATCH 04/11] fix 100% issue --- main.tf | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/main.tf b/main.tf index 4465b65..465f524 100644 --- a/main.tf +++ b/main.tf @@ -1,10 +1,11 @@ locals { service_name = "${var.env}-${var.release["component"]}" full_service_name = "${local.service_name}${var.name_suffix}" - lower_weight = ceil((var.spot_capacity_percentage / 100)) - higher_weight = local.lower_weight == 0 ? 1 : ceil(local.lower_weight / (var.spot_capacity_percentage / 100)) - 1 - spot_weight = var.spot_capacity_percentage <= 50 ? local.lower_weight : local.higher_weight - ondemand_weight = var.spot_capacity_percentage <= 50 ? local.higher_weight : local.lower_weight + p = var.spot_capacity_percentage + lower_weight = ceil((local.p / 100)) + higher_weight = local.lower_weight == 0 ? 1 : ceil(local.lower_weight / (local.p / 100)) - 1 + spot_weight = local.p <= 50 || local.p == 100 ? local.lower_weight : local.higher_weight + ondemand_weight = local.p <= 50 || local.p == 100 ? local.higher_weight : local.lower_weight } module "ecs_update_monitor" { From 6a406d5a419535acf67564e60710e08c506ca36a Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 2 May 2024 13:00:09 +0100 Subject: [PATCH 05/11] fix calculation --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 465f524..1edfbfa 100644 --- a/main.tf +++ b/main.tf @@ -1,7 +1,7 @@ locals { service_name = "${var.env}-${var.release["component"]}" full_service_name = "${local.service_name}${var.name_suffix}" - p = var.spot_capacity_percentage + p = var.spot_capacity_percentage <= 50 ? var.spot_capacity_percentage : 100 - var.spot_capacity_percentage lower_weight = ceil((local.p / 100)) higher_weight = local.lower_weight == 0 ? 1 : ceil(local.lower_weight / (local.p / 100)) - 1 spot_weight = local.p <= 50 || local.p == 100 ? local.lower_weight : local.higher_weight From 6bcb53e547910d8959988408a02bcf6d60736f7e Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 2 May 2024 13:09:53 +0100 Subject: [PATCH 06/11] fix logic in calc --- main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index 1edfbfa..36aa034 100644 --- a/main.tf +++ b/main.tf @@ -4,8 +4,8 @@ locals { p = var.spot_capacity_percentage <= 50 ? var.spot_capacity_percentage : 100 - var.spot_capacity_percentage lower_weight = ceil((local.p / 100)) higher_weight = local.lower_weight == 0 ? 1 : ceil(local.lower_weight / (local.p / 100)) - 1 - spot_weight = local.p <= 50 || local.p == 100 ? local.lower_weight : local.higher_weight - ondemand_weight = local.p <= 50 || local.p == 100 ? local.higher_weight : local.lower_weight + spot_weight = var.spot_capacity_percentage <= 50 ? local.lower_weight : local.higher_weight + ondemand_weight = var.spot_capacity_percentage <= 50 ? local.higher_weight : local.lower_weight } module "ecs_update_monitor" { From 8347cfa48b533031d9ca726275949d81d140fc7f Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 2 May 2024 13:20:26 +0100 Subject: [PATCH 07/11] moved locals --- main.tf | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/main.tf b/main.tf index 36aa034..4940b23 100644 --- a/main.tf +++ b/main.tf @@ -1,11 +1,6 @@ locals { service_name = "${var.env}-${var.release["component"]}" full_service_name = "${local.service_name}${var.name_suffix}" - p = var.spot_capacity_percentage <= 50 ? var.spot_capacity_percentage : 100 - var.spot_capacity_percentage - lower_weight = ceil((local.p / 100)) - higher_weight = local.lower_weight == 0 ? 1 : ceil(local.lower_weight / (local.p / 100)) - 1 - spot_weight = var.spot_capacity_percentage <= 50 ? local.lower_weight : local.higher_weight - ondemand_weight = var.spot_capacity_percentage <= 50 ? local.higher_weight : local.lower_weight } module "ecs_update_monitor" { @@ -20,6 +15,12 @@ module "ecs_update_monitor" { } locals { + p = var.spot_capacity_percentage <= 50 ? var.spot_capacity_percentage : 100 - var.spot_capacity_percentage + lower_weight = ceil((local.p / 100)) + higher_weight = local.lower_weight == 0 ? 1 : ceil(local.lower_weight / (local.p / 100)) - 1 + spot_weight = var.spot_capacity_percentage <= 50 ? local.lower_weight : local.higher_weight + ondemand_weight = var.spot_capacity_percentage <= 50 ? local.higher_weight : local.lower_weight + capacity_providers = var.image_build_details["buildx"] == "true" && can(regexall("^arm64", var.image_build_details["platforms"])) ? [ { capacity_provider = "${var.ecs_cluster}-native-scaling" From 0ac1bfce690f76a4eb3547a21e0ef4739e373ad0 Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 2 May 2024 13:46:13 +0100 Subject: [PATCH 08/11] fix calculation --- main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index 4940b23..6a6272a 100644 --- a/main.tf +++ b/main.tf @@ -17,10 +17,10 @@ module "ecs_update_monitor" { locals { p = var.spot_capacity_percentage <= 50 ? var.spot_capacity_percentage : 100 - var.spot_capacity_percentage lower_weight = ceil((local.p / 100)) - higher_weight = local.lower_weight == 0 ? 1 : ceil(local.lower_weight / (local.p / 100)) - 1 + higher_weight = local.lower_weight == 0 ? 1 : floor(local.lower_weight / (local.p / 100)) - 1 spot_weight = var.spot_capacity_percentage <= 50 ? local.lower_weight : local.higher_weight ondemand_weight = var.spot_capacity_percentage <= 50 ? local.higher_weight : local.lower_weight - + capacity_providers = var.image_build_details["buildx"] == "true" && can(regexall("^arm64", var.image_build_details["platforms"])) ? [ { capacity_provider = "${var.ecs_cluster}-native-scaling" From 6ecb41c568537e87ba16c92bdc64936b8b9195f1 Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 2 May 2024 15:19:34 +0100 Subject: [PATCH 09/11] fix calc --- main.tf | 4 ++-- variables.tf | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main.tf b/main.tf index 6a6272a..6639a93 100644 --- a/main.tf +++ b/main.tf @@ -16,8 +16,8 @@ module "ecs_update_monitor" { locals { p = var.spot_capacity_percentage <= 50 ? var.spot_capacity_percentage : 100 - var.spot_capacity_percentage - lower_weight = ceil((local.p / 100)) - higher_weight = local.lower_weight == 0 ? 1 : floor(local.lower_weight / (local.p / 100)) - 1 + lower_weight = ceil(local.p / 100) + higher_weight = local.lower_weight == 0 ? 1 : (floor(local.lower_weight / (local.p / 100)) - 1) spot_weight = var.spot_capacity_percentage <= 50 ? local.lower_weight : local.higher_weight ondemand_weight = var.spot_capacity_percentage <= 50 ? local.higher_weight : local.lower_weight diff --git a/variables.tf b/variables.tf index d701fda..8e07194 100644 --- a/variables.tf +++ b/variables.tf @@ -272,7 +272,7 @@ variable "image_build_details" { } variable "spot_capacity_percentage" { - default = 20 + default = 33 type = number description = "Percentage of tasks to run on spot instances" } \ No newline at end of file From 263f8e374e681bd51fae8ab751988886bdf713b9 Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Fri, 3 May 2024 09:49:53 +0100 Subject: [PATCH 10/11] fix cal --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 6639a93..aba8926 100644 --- a/main.tf +++ b/main.tf @@ -17,7 +17,7 @@ module "ecs_update_monitor" { locals { p = var.spot_capacity_percentage <= 50 ? var.spot_capacity_percentage : 100 - var.spot_capacity_percentage lower_weight = ceil(local.p / 100) - higher_weight = local.lower_weight == 0 ? 1 : (floor(local.lower_weight / (local.p / 100)) - 1) + higher_weight = local.lower_weight == 0 ? 1 : (floor(local.lower_weight / (local.p / 100)) - local.lower_weight) spot_weight = var.spot_capacity_percentage <= 50 ? local.lower_weight : local.higher_weight ondemand_weight = var.spot_capacity_percentage <= 50 ? local.higher_weight : local.lower_weight From 8358afd0334ca9b8fb53aa52eebe7295051c015f Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Fri, 3 May 2024 11:46:42 +0100 Subject: [PATCH 11/11] add graviton to capacity providers --- main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index aba8926..c8b9153 100644 --- a/main.tf +++ b/main.tf @@ -23,11 +23,11 @@ locals { capacity_providers = var.image_build_details["buildx"] == "true" && can(regexall("^arm64", var.image_build_details["platforms"])) ? [ { - capacity_provider = "${var.ecs_cluster}-native-scaling" + capacity_provider = "${var.ecs_cluster}-native-scaling-graviton" weight = local.ondemand_weight }, { - capacity_provider = "${var.ecs_cluster}-native-scaling-spot" + capacity_provider = "${var.ecs_cluster}-native-scaling-graviton-spot" weight = local.spot_weight } ] : [