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

Scaling and instance sizing #47

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
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
44 changes: 0 additions & 44 deletions .terraform.lock.hcl

This file was deleted.

117 changes: 12 additions & 105 deletions README.md

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions examples/ci/ci.tf
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ module "fargate_api" {
}

autoscaling_config = null
task_memory = 256
task_cpu = 512
codedeploy_test_listener_port = 8443
codedeploy_lifecycle_hooks = {
BeforeInstall = null
Expand Down Expand Up @@ -131,12 +133,8 @@ output "cloudwatch_log_group" {
value = module.fargate_api.cloudwatch_log_group.arn
}

output "autoscaling_step_up_policy" {
value = module.fargate_api.autoscaling_step_up_policy
}

output "autoscaling_step_down_policy" {
value = module.fargate_api.autoscaling_step_down_policy
output "autoscaling_policy" {
value = module.fargate_api.autoscaling_policy
}

output "task_role" {
Expand Down
2 changes: 1 addition & 1 deletion examples/logging/logging.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data "aws_elb_service_account" "main" {}
// name = "fake-example-cluster"
//}
module "fargate_api" {
source = "github.com/byu-oit/terraform-aws-fargate-api?ref=v6.0.1"
source = "github.com/byu-oit/terraform-aws-fargate-api?ref=v7.0.0"
// source = "../../" // for local testing
app_name = "example-api"
//ecs_cluster_name = aws_ecs_cluster.existing.name
Expand Down
71 changes: 10 additions & 61 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -578,76 +578,25 @@ resource "aws_appautoscaling_target" "default" {
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_appautoscaling_policy" "up" {
count = var.autoscaling_config != null ? 1 : 0
name = "${var.app_name}-autoscale-up"
resource_id = aws_appautoscaling_target.default[0].resource_id
scalable_dimension = aws_appautoscaling_target.default[0].scalable_dimension
service_namespace = aws_appautoscaling_target.default[0].service_namespace

step_scaling_policy_configuration {
adjustment_type = var.scaling_up_policy_config.adjustment_type
metric_aggregation_type = var.scaling_up_policy_config.metric_aggregation_type
cooldown = var.scaling_up_policy_config.cooldown

step_adjustment {
scaling_adjustment = var.scaling_up_policy_config.scaling_adjustment
metric_interval_lower_bound = var.scaling_up_policy_config.metric_interval_lower_bound
}
}
}
resource "aws_cloudwatch_metric_alarm" "up" {
count = var.autoscaling_config != null ? 1 : 0
alarm_name = "${var.app_name}-alarm-up"
namespace = "AWS/ECS"
dimensions = {
ClusterName = local.cluster_name
ServiceName = aws_ecs_service.service.name
}
statistic = var.scaling_up_metric_alarm_config.statistic
metric_name = var.scaling_up_metric_alarm_config.metric_name
comparison_operator = var.scaling_up_metric_alarm_config.comparison_operator
threshold = var.scaling_up_metric_alarm_config.threshold
period = var.scaling_up_metric_alarm_config.period
evaluation_periods = var.scaling_up_metric_alarm_config.evaluation_periods
alarm_actions = [aws_appautoscaling_policy.up[0].arn]
tags = var.tags
}
resource "aws_appautoscaling_policy" "down" {
resource "aws_appautoscaling_policy" "default" {
count = var.autoscaling_config != null ? 1 : 0
name = "${var.app_name}-autoscale-down"
name = "${var.app_name}-tracking-autoscale"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.default[0].resource_id
scalable_dimension = aws_appautoscaling_target.default[0].scalable_dimension
service_namespace = aws_appautoscaling_target.default[0].service_namespace

step_scaling_policy_configuration {
adjustment_type = var.scaling_down_policy_config.adjustment_type
metric_aggregation_type = var.scaling_down_policy_config.metric_aggregation_type
cooldown = var.scaling_down_policy_config.cooldown

step_adjustment {
scaling_adjustment = var.scaling_down_policy_config.scaling_adjustment
metric_interval_upper_bound = var.scaling_down_policy_config.metric_interval_upper_bound
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = var.autoscaling_config.target_metric
}
target_value = var.autoscaling_config.target_value
scale_in_cooldown = var.autoscaling_config.scale_in_cooldown
scale_out_cooldown = var.autoscaling_config.scale_out_cooldown
}
}
resource "aws_cloudwatch_metric_alarm" "down" {
count = var.autoscaling_config != null ? 1 : 0
alarm_name = "${var.app_name}-alarm-down"
namespace = "AWS/ECS"
dimensions = {
ClusterName = local.cluster_name
ServiceName = aws_ecs_service.service.name
}
statistic = var.scaling_down_metric_alarm_config.statistic
metric_name = var.scaling_down_metric_alarm_config.metric_name
comparison_operator = var.scaling_down_metric_alarm_config.comparison_operator
threshold = var.scaling_down_metric_alarm_config.threshold
period = var.scaling_down_metric_alarm_config.period
evaluation_periods = var.scaling_down_metric_alarm_config.evaluation_periods
alarm_actions = [aws_appautoscaling_policy.down[0].arn]
tags = var.tags
}


# ==================== AppSpec file ====================
resource "local_file" "appspec_json" {
Expand Down
8 changes: 2 additions & 6 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ output "cloudwatch_log_group" {
value = aws_cloudwatch_log_group.container_log_group
}

output "autoscaling_step_up_policy" {
value = var.autoscaling_config != null ? aws_appautoscaling_policy.up : null
}

output "autoscaling_step_down_policy" {
value = var.autoscaling_config != null ? aws_appautoscaling_policy.down : null
output "autoscaling_policy" {
value = var.autoscaling_config != null ? aws_appautoscaling_policy.default[0] : null
}

output "task_role" {
Expand Down
87 changes: 9 additions & 78 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,11 @@ variable "task_policies" {
}
variable "task_cpu" {
type = number
description = "CPU for the task definition. Defaults to 256."
default = 256
description = "CPU for the task definition."
}
variable "task_memory" {
type = number
description = "Memory for the task definition. Defaults to 512."
default = 512
description = "Memory for the task definition."
}
variable "cpu_architecture" {
type = string
Expand Down Expand Up @@ -215,84 +213,17 @@ variable "https_certificate_arn" {
}
variable "autoscaling_config" {
type = object({
min_capacity = number
max_capacity = number
min_capacity = number
max_capacity = number
target_metric = string
target_value = number
scale_in_cooldown = optional(number, 300) # 5 minutes between scale down actions
scale_out_cooldown = optional(number, 60) # 1 minute between scale up actions
})
description = "Configuration for default autoscaling policies and alarms. Set to null if you want to set up your own autoscaling policies and alarms."
default = null
}
variable "scaling_up_policy_config" {
type = object({
adjustment_type = string
metric_aggregation_type = string
cooldown = number
scaling_adjustment = number
metric_interval_lower_bound = number
})
description = "Advanced configuration for the scaling up policy if 'autoscaling_config' is in use."
default = {
adjustment_type = "ChangeInCapacity"
metric_aggregation_type = "Average"
cooldown = 300
scaling_adjustment = 1
metric_interval_lower_bound = 0
}
}
variable "scaling_up_metric_alarm_config" {
type = object({
statistic = string
metric_name = string
comparison_operator = string
threshold = number
period = number
evaluation_periods = number
})
description = "Advanced configuration for the scaling up metric alarm if 'autoscaling_config' is in use."
default = {
statistic = "Average"
metric_name = "CPUUtilization"
comparison_operator = "GreaterThanThreshold"
threshold = 75
period = 300
evaluation_periods = 5
}
}
variable "scaling_down_policy_config" {
type = object({
adjustment_type = string
metric_aggregation_type = string
cooldown = number
scaling_adjustment = number
metric_interval_upper_bound = number
})
description = "Advanced configuration for the scaling down policy if 'autoscaling_config' is in use."
default = {
adjustment_type = "ChangeInCapacity"
metric_aggregation_type = "Average"
cooldown = 300
scaling_adjustment = -1
metric_interval_upper_bound = 0
}
}
variable "scaling_down_metric_alarm_config" {
type = object({
statistic = string
metric_name = string
comparison_operator = string
threshold = number
period = number
evaluation_periods = number
})
description = "Advanced configuration for scaling the down metric alarm if 'autoscaling_config' is in use."
default = {
statistic = "Average"
metric_name = "CPUUtilization"
comparison_operator = "LessThanThreshold"
threshold = 25
period = 300
evaluation_periods = 5
}
}

variable "log_group_name" {
type = string
description = "CloudWatch log group name."
Expand Down
Loading