diff --git a/services/terraform/modules/fargate-autoscaling/main.tf b/services/terraform/modules/fargate-autoscaling/main.tf new file mode 100644 --- /dev/null +++ b/services/terraform/modules/fargate-autoscaling/main.tf @@ -0,0 +1,46 @@ +resource "aws_appautoscaling_target" "service" { + count = var.create_resources ? 1 : 0 + max_capacity = var.max_capacity + min_capacity = var.min_capacity + resource_id = "service/${var.cluster_name}/${var.service_name}" + scalable_dimension = "ecs:service:DesiredCount" + service_namespace = "ecs" +} + +# Target tracking scaling based on CPU utilization +resource "aws_appautoscaling_policy" "cpu_target_tracking" { + count = var.create_resources ? 1 : 0 + name = "${var.service_name}-cpu-target-tracking" + policy_type = "TargetTrackingScaling" + resource_id = aws_appautoscaling_target.service[0].resource_id + scalable_dimension = aws_appautoscaling_target.service[0].scalable_dimension + service_namespace = aws_appautoscaling_target.service[0].service_namespace + + target_tracking_scaling_policy_configuration { + predefined_metric_specification { + predefined_metric_type = "ECSServiceAverageCPUUtilization" + } + target_value = var.cpu_target + scale_in_cooldown = var.scale_in_cooldown + scale_out_cooldown = var.scale_out_cooldown + } +} + +# Target tracking scaling based on memory utilization +resource "aws_appautoscaling_policy" "memory_target_tracking" { + count = var.create_resources ? 1 : 0 + name = "${var.service_name}-memory-target-tracking" + policy_type = "TargetTrackingScaling" + resource_id = aws_appautoscaling_target.service[0].resource_id + scalable_dimension = aws_appautoscaling_target.service[0].scalable_dimension + service_namespace = aws_appautoscaling_target.service[0].service_namespace + + target_tracking_scaling_policy_configuration { + predefined_metric_specification { + predefined_metric_type = "ECSServiceAverageMemoryUtilization" + } + target_value = var.memory_target + scale_in_cooldown = var.scale_in_cooldown + scale_out_cooldown = var.scale_out_cooldown + } +} \ No newline at end of file diff --git a/services/terraform/modules/fargate-autoscaling/outputs.tf b/services/terraform/modules/fargate-autoscaling/outputs.tf new file mode 100644 --- /dev/null +++ b/services/terraform/modules/fargate-autoscaling/outputs.tf @@ -0,0 +1,25 @@ +output "autoscaling_target_arn" { + description = "ARN of the auto-scaling target" + value = var.create_resources ? aws_appautoscaling_target.service[0].arn : null +} + +output "cpu_policy_arn" { + description = "ARN of the CPU scaling policy" + value = var.create_resources ? aws_appautoscaling_policy.cpu_target_tracking[0].arn : null +} + +output "memory_policy_arn" { + description = "ARN of the memory scaling policy" + value = var.create_resources ? aws_appautoscaling_policy.memory_target_tracking[0].arn : null +} + +output "scaling_configuration" { + description = "Summary of scaling configuration" + value = var.create_resources ? { + service_name = var.service_name + min_capacity = var.min_capacity + max_capacity = var.max_capacity + cpu_target = var.cpu_target + memory_target = var.memory_target + } : null +} \ No newline at end of file diff --git a/services/terraform/modules/fargate-autoscaling/variables.tf b/services/terraform/modules/fargate-autoscaling/variables.tf new file mode 100644 --- /dev/null +++ b/services/terraform/modules/fargate-autoscaling/variables.tf @@ -0,0 +1,61 @@ +variable "create_resources" { + description = "Whether to create auto-scaling resources" + type = bool + default = true +} + +variable "service_name" { + description = "Name of the ECS service" + type = string +} + +variable "cluster_name" { + description = "Name of the ECS cluster" + type = string +} + +variable "min_capacity" { + description = "Minimum number of tasks" + type = number + default = 1 +} + +variable "max_capacity" { + description = "Maximum number of tasks" + type = number + default = 8 +} + +variable "cpu_target" { + description = "Target CPU utilization percentage" + type = number + default = 30.0 + + validation { + condition = var.cpu_target > 0 && var.cpu_target <= 100 + error_message = "CPU target must be between 0 and 100." + } +} + +variable "memory_target" { + description = "Target memory utilization percentage" + type = number + default = 40.0 + + validation { + condition = var.memory_target > 0 && var.memory_target <= 100 + error_message = "Memory target must be between 0 and 100." + } +} + +variable "scale_in_cooldown" { + description = "Cooldown period (in seconds) before scaling down" + type = number + default = 300 +} + +variable "scale_out_cooldown" { + description = "Cooldown period (in seconds) before scaling up" + type = number + default = 60 +} \ No newline at end of file