Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(infra): update asg in api cluster #1682

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,6 +20,8 @@
{
"name": "${task_name}",
"image": "${ecr_uri}",
"cpu": 924,
"memoryReservation": 512,
"essential": true,
"portMappings": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,6 +20,8 @@
{
"name": "${task_name}",
"image": "${ecr_uri}",
"cpu": 924,
"memoryReservation": 512,
"essential": true,
"portMappings": [
{
Expand Down
83 changes: 83 additions & 0 deletions apps/infra/modules/codedang-infra/ecs-api-admin-service-asg.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
}
3 changes: 1 addition & 2 deletions apps/infra/modules/codedang-infra/ecs-api-admin.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apps/infra/modules/codedang-infra/ecs-api-asg.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
83 changes: 83 additions & 0 deletions apps/infra/modules/codedang-infra/ecs-api-client-service-asg.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
}
3 changes: 1 addition & 2 deletions apps/infra/modules/codedang-infra/ecs-api-client.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions apps/infra/modules/codedang-infra/ecs-iris-service-asg.tf
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Loading