From 89420c773cf00383918dad380fec79f5961376be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=9D=BC=EA=B1=B4?= <100272259+k1g99@users.noreply.github.com> Date: Fri, 10 May 2024 17:42:59 +0900 Subject: [PATCH] feat(infra): update asg in api cluster (#1682) * feat: update asg in api cluster * chore: change dash to underscore --- .../backend/admin-task-definition.tftpl | 4 +- .../backend/client-task-definition.tftpl | 4 +- .../ecs-api-admin-service-asg.tf | 83 +++++++++++++++++++ .../modules/codedang-infra/ecs-api-admin.tf | 3 +- .../modules/codedang-infra/ecs-api-asg.tf | 2 +- .../ecs-api-client-service-asg.tf | 83 +++++++++++++++++++ .../modules/codedang-infra/ecs-api-client.tf | 3 +- .../codedang-infra/ecs-iris-service-asg.tf | 4 +- 8 files changed, 177 insertions(+), 9 deletions(-) create mode 100644 apps/infra/modules/codedang-infra/ecs-api-admin-service-asg.tf create mode 100644 apps/infra/modules/codedang-infra/ecs-api-client-service-asg.tf diff --git a/apps/infra/modules/codedang-infra/backend/admin-task-definition.tftpl b/apps/infra/modules/codedang-infra/backend/admin-task-definition.tftpl index 5cef4e4616..b84c1326c4 100644 --- a/apps/infra/modules/codedang-infra/backend/admin-task-definition.tftpl +++ b/apps/infra/modules/codedang-infra/backend/admin-task-definition.tftpl @@ -2,7 +2,7 @@ { "name": "log_router", "image": "grafana/fluent-bit-plugin-loki:2.0.0-amd64", - "cpu": 0, + "cpu": 100, "memoryReservation": 50, "portMappings": [], "essential": true, @@ -20,6 +20,8 @@ { "name": "${task_name}", "image": "${ecr_uri}", + "cpu": 924, + "memoryReservation": 512, "essential": true, "portMappings": [ { diff --git a/apps/infra/modules/codedang-infra/backend/client-task-definition.tftpl b/apps/infra/modules/codedang-infra/backend/client-task-definition.tftpl index 176dea7381..d48d2b4253 100644 --- a/apps/infra/modules/codedang-infra/backend/client-task-definition.tftpl +++ b/apps/infra/modules/codedang-infra/backend/client-task-definition.tftpl @@ -2,7 +2,7 @@ { "name": "log_router", "image": "grafana/fluent-bit-plugin-loki:2.0.0-amd64", - "cpu": 0, + "cpu": 100, "memoryReservation": 50, "portMappings": [], "essential": true, @@ -20,6 +20,8 @@ { "name": "${task_name}", "image": "${ecr_uri}", + "cpu": 924, + "memoryReservation": 512, "essential": true, "portMappings": [ { diff --git a/apps/infra/modules/codedang-infra/ecs-api-admin-service-asg.tf b/apps/infra/modules/codedang-infra/ecs-api-admin-service-asg.tf new file mode 100644 index 0000000000..12a8880aed --- /dev/null +++ b/apps/infra/modules/codedang-infra/ecs-api-admin-service-asg.tf @@ -0,0 +1,83 @@ +###################### cloudwatch alert ###################### +resource "aws_cloudwatch_metric_alarm" "ecs_admin_api_scale_down" { + alarm_name = "Codedang-Admin-Api-Service-Scale-Down-Alert" + comparison_operator = "LessThanThreshold" + datapoints_to_alarm = 10 + evaluation_periods = 10 + metric_name = "CPUUtilization" + namespace = "AWS/ECS" + period = 60 + statistic = "Average" + threshold = 50 + alarm_description = "This metric monitors task cpu utilization and scale down ecs service" + alarm_actions = [aws_appautoscaling_policy.service_asp_admin_api_scale_down.arn] + + dimensions = { + ClusterName = aws_ecs_cluster.api.name + ServiceName = aws_ecs_service.admin_api.name + } +} + +resource "aws_cloudwatch_metric_alarm" "ecs_admin_api_scale_up" { + alarm_name = "Codedang-Admin-Api-Service-Scale-Up-Alert" + comparison_operator = "GreaterThanThreshold" + datapoints_to_alarm = 1 + evaluation_periods = 1 + metric_name = "CPUUtilization" + namespace = "AWS/ECS" + period = 60 + statistic = "Maximum" + threshold = 120 + alarm_description = "This metric monitors task cpu utilization and scale up ecs service" + alarm_actions = [aws_appautoscaling_policy.service_asp_admin_api_scale_up.arn] + + dimensions = { + ClusterName = aws_ecs_cluster.api.name + ServiceName = aws_ecs_service.admin_api.name + } +} + +###################### Service Auto Scaling ##################### +resource "aws_appautoscaling_target" "service_asg_admin_api" { + max_capacity = 8 + min_capacity = 1 + resource_id = "service/${aws_ecs_cluster.api.name}/${aws_ecs_service.admin_api.name}" + scalable_dimension = "ecs:service:DesiredCount" + service_namespace = "ecs" +} + +resource "aws_appautoscaling_policy" "service_asp_admin_api_scale_up" { + name = "scale-up" + policy_type = "StepScaling" + resource_id = aws_appautoscaling_target.service_asg_admin_api.resource_id + scalable_dimension = aws_appautoscaling_target.service_asg_admin_api.scalable_dimension + service_namespace = aws_appautoscaling_target.service_asg_admin_api.service_namespace + + step_scaling_policy_configuration { + adjustment_type = "ChangeInCapacity" + cooldown = 60 + + step_adjustment { + metric_interval_lower_bound = 0 + scaling_adjustment = 1 + } + } +} + +resource "aws_appautoscaling_policy" "service_asp_admin_api_scale_down" { + name = "scale-down" + policy_type = "StepScaling" + resource_id = aws_appautoscaling_target.service_asg_admin_api.resource_id + scalable_dimension = aws_appautoscaling_target.service_asg_admin_api.scalable_dimension + service_namespace = aws_appautoscaling_target.service_asg_admin_api.service_namespace + + step_scaling_policy_configuration { + adjustment_type = "ChangeInCapacity" + cooldown = 30 + + step_adjustment { + metric_interval_upper_bound = -30 + scaling_adjustment = -1 + } + } +} diff --git a/apps/infra/modules/codedang-infra/ecs-api-admin.tf b/apps/infra/modules/codedang-infra/ecs-api-admin.tf index 5f167e00d7..5b5b0203a5 100644 --- a/apps/infra/modules/codedang-infra/ecs-api-admin.tf +++ b/apps/infra/modules/codedang-infra/ecs-api-admin.tf @@ -94,8 +94,7 @@ resource "aws_ecs_task_definition" "admin_api" { family = "Codedang-Admin-Api" requires_compatibilities = ["EC2"] network_mode = "bridge" - cpu = 512 - memory = 512 + memory = 950 container_definitions = templatefile("${path.module}/backend/admin-task-definition.tftpl", { task_name = "Codedang-Admin-Api", # aurora-posrgresql diff --git a/apps/infra/modules/codedang-infra/ecs-api-asg.tf b/apps/infra/modules/codedang-infra/ecs-api-asg.tf index 8996d33d82..e72b324985 100644 --- a/apps/infra/modules/codedang-infra/ecs-api-asg.tf +++ b/apps/infra/modules/codedang-infra/ecs-api-asg.tf @@ -51,7 +51,7 @@ resource "aws_autoscaling_policy" "asp_api" { estimated_instance_warmup = 300 target_tracking_configuration { - target_value = 90 + target_value = 70 predefined_metric_specification { predefined_metric_type = "ASGAverageCPUUtilization" } diff --git a/apps/infra/modules/codedang-infra/ecs-api-client-service-asg.tf b/apps/infra/modules/codedang-infra/ecs-api-client-service-asg.tf new file mode 100644 index 0000000000..c21d8c93ce --- /dev/null +++ b/apps/infra/modules/codedang-infra/ecs-api-client-service-asg.tf @@ -0,0 +1,83 @@ +###################### cloudwatch alert ###################### +resource "aws_cloudwatch_metric_alarm" "ecs_client_api_scale_down" { + alarm_name = "Codedang-Client-Api-Service-Scale-Down-Alert" + comparison_operator = "LessThanThreshold" + datapoints_to_alarm = 10 + evaluation_periods = 10 + metric_name = "CPUUtilization" + namespace = "AWS/ECS" + period = 60 + statistic = "Average" + threshold = 50 + alarm_description = "This metric monitors task cpu utilization and scale down ecs service" + alarm_actions = [aws_appautoscaling_policy.service_asp_client_api_scale_down.arn] + + dimensions = { + ClusterName = aws_ecs_cluster.api.name + ServiceName = aws_ecs_service.client_api.name + } +} + +resource "aws_cloudwatch_metric_alarm" "ecs_client_api_scale_up" { + alarm_name = "Codedang-Client-Api-Service-Scale-Up-Alert" + comparison_operator = "GreaterThanThreshold" + datapoints_to_alarm = 1 + evaluation_periods = 1 + metric_name = "CPUUtilization" + namespace = "AWS/ECS" + period = 60 + statistic = "Maximum" + threshold = 120 + alarm_description = "This metric monitors task cpu utilization and scale up ecs service" + alarm_actions = [aws_appautoscaling_policy.service_asp_client_api_scale_up.arn] + + dimensions = { + ClusterName = aws_ecs_cluster.api.name + ServiceName = aws_ecs_service.client_api.name + } +} + +###################### Service Auto Scaling ##################### +resource "aws_appautoscaling_target" "service_asg_client_api" { + max_capacity = 8 + min_capacity = 1 + resource_id = "service/${aws_ecs_cluster.api.name}/${aws_ecs_service.client_api.name}" + scalable_dimension = "ecs:service:DesiredCount" + service_namespace = "ecs" +} + +resource "aws_appautoscaling_policy" "service_asp_client_api_scale_up" { + name = "scale-up" + policy_type = "StepScaling" + resource_id = aws_appautoscaling_target.service_asg_client_api.resource_id + scalable_dimension = aws_appautoscaling_target.service_asg_client_api.scalable_dimension + service_namespace = aws_appautoscaling_target.service_asg_client_api.service_namespace + + step_scaling_policy_configuration { + adjustment_type = "ChangeInCapacity" + cooldown = 60 + + step_adjustment { + metric_interval_lower_bound = 0 + scaling_adjustment = 1 + } + } +} + +resource "aws_appautoscaling_policy" "service_asp_client_api_scale_down" { + name = "scale-down" + policy_type = "StepScaling" + resource_id = aws_appautoscaling_target.service_asg_client_api.resource_id + scalable_dimension = aws_appautoscaling_target.service_asg_client_api.scalable_dimension + service_namespace = aws_appautoscaling_target.service_asg_client_api.service_namespace + + step_scaling_policy_configuration { + adjustment_type = "ChangeInCapacity" + cooldown = 30 + + step_adjustment { + metric_interval_upper_bound = -30 + scaling_adjustment = -1 + } + } +} diff --git a/apps/infra/modules/codedang-infra/ecs-api-client.tf b/apps/infra/modules/codedang-infra/ecs-api-client.tf index c81b2af3d3..0acc9a37f6 100644 --- a/apps/infra/modules/codedang-infra/ecs-api-client.tf +++ b/apps/infra/modules/codedang-infra/ecs-api-client.tf @@ -94,8 +94,7 @@ resource "aws_ecs_task_definition" "client_api" { family = "Codedang-Client-Api" requires_compatibilities = ["EC2"] network_mode = "bridge" - cpu = 512 - memory = 512 + memory = 950 container_definitions = templatefile("${path.module}/backend/client-task-definition.tftpl", { task_name = "Codedang-Client-Api", # aurora-posrgresql diff --git a/apps/infra/modules/codedang-infra/ecs-iris-service-asg.tf b/apps/infra/modules/codedang-infra/ecs-iris-service-asg.tf index 96f6756b2e..6314787fe9 100644 --- a/apps/infra/modules/codedang-infra/ecs-iris-service-asg.tf +++ b/apps/infra/modules/codedang-infra/ecs-iris-service-asg.tf @@ -1,5 +1,5 @@ ###################### cloudwatch alert ###################### -resource "aws_cloudwatch_metric_alarm" "ecs_iris_scale-down" { +resource "aws_cloudwatch_metric_alarm" "ecs_iris_scale_down" { alarm_name = "Codedang-Iris-Service-Scale-Down-Alert" comparison_operator = "LessThanThreshold" datapoints_to_alarm = 15 @@ -18,7 +18,7 @@ resource "aws_cloudwatch_metric_alarm" "ecs_iris_scale-down" { } } -resource "aws_cloudwatch_metric_alarm" "ecs_iris_scale-up" { +resource "aws_cloudwatch_metric_alarm" "ecs_iris_scale_up" { alarm_name = "Codedang-Iris-Service-Scale-Up-Alert" comparison_operator = "GreaterThanThreshold" datapoints_to_alarm = 1