Skip to content

Commit

Permalink
added custom scaling with advanced config
Browse files Browse the repository at this point in the history
  • Loading branch information
robertomoutinho committed Oct 28, 2024
1 parent 3ac468b commit 37f66ed
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 40 deletions.
6 changes: 1 addition & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ repos:
hooks:
- id: terraform_fmt
- id: terraform_docs
- id: terraform_tflint
- repo: https://github.com/bridgecrewio/checkov
rev: '2.0.638'
hooks:
- id: checkov
- id: terraform_tflint
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ This repository contains Terraform infrastructure code which creates AWS resourc
| Name | Type |
|------|------|
| [aws_appautoscaling_policy.auto_scaling_cpu](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_policy.auto_scaling_custom_cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_policy.auto_scaling_mem](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_policy.auto_scaling_request](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_target.target](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
| [aws_cloudwatch_log_group.app](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
| [aws_ecs_service.app](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
Expand Down Expand Up @@ -103,10 +103,7 @@ This repository contains Terraform infrastructure code which creates AWS resourc
| <a name="input_app_sg_extra_cidr"></a> [app\_sg\_extra\_cidr](#input\_app\_sg\_extra\_cidr) | A list of extra cidr blocks to allow ingress traffic to container | `list(string)` | `[]` | no |
| <a name="input_asg_cooldown_to_scale_down_again"></a> [asg\_cooldown\_to\_scale\_down\_again](#input\_asg\_cooldown\_to\_scale\_down\_again) | The amount of time, in seconds, after a scaling activity completes and before the next scaling down activity can start. | `number` | `300` | no |
| <a name="input_asg_cooldown_to_scale_up_again"></a> [asg\_cooldown\_to\_scale\_up\_again](#input\_asg\_cooldown\_to\_scale\_up\_again) | The amount of time, in seconds, after a scaling activity completes and before the next scaling up activity can start. | `number` | `60` | no |
| <a name="input_asg_custom_scaling_metric_name"></a> [asg\_custom\_scaling\_metric\_name](#input\_asg\_custom\_scaling\_metric\_name) | Name of the metric | `string` | `null` | no |
| <a name="input_asg_custom_scaling_namespace"></a> [asg\_custom\_scaling\_namespace](#input\_asg\_custom\_scaling\_namespace) | Namespace of the metric | `string` | `null` | no |
| <a name="input_asg_custom_scaling_statistic"></a> [asg\_custom\_scaling\_statistic](#input\_asg\_custom\_scaling\_statistic) | Statistic of the metric | `string` | `null` | no |
| <a name="input_asg_custom_scaling_target_value"></a> [asg\_custom\_scaling\_target\_value](#input\_asg\_custom\_scaling\_target\_value) | Target value for the metric | `number` | `100` | no |
| <a name="input_asg_custom_policies"></a> [asg\_custom\_policies](#input\_asg\_custom\_policies) | Map of autoscaling policies to create for the service | `any` | <pre>{<br> "cpu": {<br> "target_tracking_scaling_policy_configuration": {<br> "predefined_metric_specification": {<br> "predefined_metric_type": "ECSServiceAverageCPUUtilization"<br> }<br> }<br> },<br> "memory": {<br> "target_tracking_scaling_policy_configuration": {<br> "predefined_metric_specification": {<br> "predefined_metric_type": "ECSServiceAverageMemoryUtilization"<br> }<br> }<br> }<br>}</pre> | no |
| <a name="input_asg_evaluation_periods"></a> [asg\_evaluation\_periods](#input\_asg\_evaluation\_periods) | The number of periods over which data is compared to the specified threshold. | `number` | `5` | no |
| <a name="input_asg_max_tasks"></a> [asg\_max\_tasks](#input\_asg\_max\_tasks) | The amount of maximum tasks | `number` | `3` | no |
| <a name="input_asg_min_tasks"></a> [asg\_min\_tasks](#input\_asg\_min\_tasks) | The amount of minimum tasks | `number` | `1` | no |
Expand Down
49 changes: 41 additions & 8 deletions autoscaling.tf
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,54 @@ resource "aws_appautoscaling_policy" "auto_scaling_request" {
## Custom Cloudwatch ##
#######################

resource "aws_appautoscaling_policy" "auto_scaling_custom_cloudwatch" {
resource "aws_appautoscaling_policy" "this" {

for_each = { for k, v in var.asg_custom_policies : k => v if var.enable_custom_scaling }

count = (var.enable_asg && var.enable_custom_scaling) ? 1 : 0
name = "${var.environment}-${var.name}-custom-scale"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.target[0].resource_id
service_namespace = aws_appautoscaling_target.target[0].service_namespace
scalable_dimension = aws_appautoscaling_target.target[0].scalable_dimension

target_tracking_scaling_policy_configuration {
target_value = var.asg_custom_scaling_target_value
customized_metric_specification {
metric_name = var.asg_custom_scaling_metric_name
namespace = var.asg_custom_scaling_namespace
statistic = var.asg_custom_scaling_statistic
dynamic "target_tracking_scaling_policy_configuration" {
for_each = each.value.target_tracking_scaling_policy_configuration

content {
dynamic "customized_metric_specification" {
for_each = try([target_tracking_scaling_policy_configuration.value.customized_metric_specification], [])

content {
dynamic "dimensions" {
for_each = try(customized_metric_specification.value.dimensions, [])

content {
name = dimensions.value.name
value = dimensions.value.value
}
}

metric_name = customized_metric_specification.value.metric_name
namespace = customized_metric_specification.value.namespace
statistic = customized_metric_specification.value.statistic
unit = try(customized_metric_specification.value.unit, null)
}
}

disable_scale_in = try(target_tracking_scaling_policy_configuration.value.disable_scale_in, null)

dynamic "predefined_metric_specification" {
for_each = try([target_tracking_scaling_policy_configuration.value.predefined_metric_specification], [])

content {
predefined_metric_type = predefined_metric_specification.value.predefined_metric_type
resource_label = try(predefined_metric_specification.value.resource_label, null)
}
}

scale_in_cooldown = try(target_tracking_scaling_policy_configuration.value.scale_in_cooldown, 300)
scale_out_cooldown = try(target_tracking_scaling_policy_configuration.value.scale_out_cooldown, 60)
target_value = try(target_tracking_scaling_policy_configuration.value.target_value, 75)
}
}

Expand Down
41 changes: 19 additions & 22 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -241,28 +241,25 @@ variable "enable_custom_scaling" {
default = false
}

variable "asg_custom_scaling_target_value" {
description = "Target value for the metric"
type = number
default = 100
}

variable "asg_custom_scaling_metric_name" {
description = "Name of the metric"
type = string
default = null
}

variable "asg_custom_scaling_namespace" {
description = "Namespace of the metric"
type = string
default = null
}

variable "asg_custom_scaling_statistic" {
description = "Statistic of the metric"
type = string
default = null
variable "asg_custom_policies" {
description = "Map of autoscaling policies to create for the service"
type = any
default = {
cpu = {
target_tracking_scaling_policy_configuration = {
predefined_metric_specification = {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
}
}
memory = {
target_tracking_scaling_policy_configuration = {
predefined_metric_specification = {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
}
}
}
}
}

# ACM
Expand Down

0 comments on commit 37f66ed

Please sign in to comment.