diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2483e69..41e9bc5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 \ No newline at end of file + - id: terraform_tflint \ No newline at end of file diff --git a/README.md b/README.md index ce479ad..8ac822b 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -103,10 +103,7 @@ This repository contains Terraform infrastructure code which creates AWS resourc | [app\_sg\_extra\_cidr](#input\_app\_sg\_extra\_cidr) | A list of extra cidr blocks to allow ingress traffic to container | `list(string)` | `[]` | no | | [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 | | [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 | -| [asg\_custom\_scaling\_metric\_name](#input\_asg\_custom\_scaling\_metric\_name) | Name of the metric | `string` | `null` | no | -| [asg\_custom\_scaling\_namespace](#input\_asg\_custom\_scaling\_namespace) | Namespace of the metric | `string` | `null` | no | -| [asg\_custom\_scaling\_statistic](#input\_asg\_custom\_scaling\_statistic) | Statistic of the metric | `string` | `null` | no | -| [asg\_custom\_scaling\_target\_value](#input\_asg\_custom\_scaling\_target\_value) | Target value for the metric | `number` | `100` | no | +| [asg\_custom\_policies](#input\_asg\_custom\_policies) | Map of autoscaling policies to create for the service | `any` |
{| no | | [asg\_evaluation\_periods](#input\_asg\_evaluation\_periods) | The number of periods over which data is compared to the specified threshold. | `number` | `5` | no | | [asg\_max\_tasks](#input\_asg\_max\_tasks) | The amount of maximum tasks | `number` | `3` | no | | [asg\_min\_tasks](#input\_asg\_min\_tasks) | The amount of minimum tasks | `number` | `1` | no | diff --git a/autoscaling.tf b/autoscaling.tf index 3f57cd8..b392542 100644 --- a/autoscaling.tf +++ b/autoscaling.tf @@ -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) } } diff --git a/variables.tf b/variables.tf index 8b28d8b..ced64c3 100644 --- a/variables.tf +++ b/variables.tf @@ -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
"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"
}
}
}
}