Skip to content

Commit 62f8299

Browse files
author
Luca Venturelli
authored
moves cpu-specific autoscaling to a more general autoscaling approach (#21)
1 parent e83b218 commit 62f8299

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

README.md

+26-4
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,43 @@ module "bluegreen" {
3434
}
3535
```
3636

37-
## cpu-scaling
37+
## scaling
3838
Terraform module to setup alarms and autoscaling triggers for autoscaling
3939

4040
### Variables
4141

42-
See the [cpu-scaling/variables.tf](cpu-scaling/variables.tf) file.
42+
| Name | Description | Type | Default | Required |
43+
|------|-------------|:----:|:-----:|:-----:|
44+
| adjustment\_down | The number of instances to remove when the alarm is triggered (the value has to be negative) | string | `"-1"` | no |
45+
| adjustment\_type | What typ of adjustment needs to happen | string | `"ChangeInCapacity"` | no |
46+
| adjustment\_up | The number of instances to add when the alarm is triggered | string | `"1"` | no |
47+
| autoscaling\_group\_name | The name of the AS group this config needs to be applied | list | n/a | yes |
48+
| cooldown\_down | The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start. | string | `"600"` | no |
49+
| cooldown\_up | The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start. | string | `"300"` | no |
50+
| dimension\_name | | string | `"AutoScalingGroupName"` | no |
51+
| dimension\_value | | string | `"false"` | no |
52+
| environment | Environment to deploy on | string | n/a | yes |
53+
| evaluation\_periods | the number of samples to evaluate | string | `"4"` | no |
54+
| metric\_name | The metric the scaling is based upon | string | `"CPUUtilization"` | no |
55+
| name | Name of the stack | string | n/a | yes |
56+
| namespace | the namespace of the cloudwatch metric | string | `"AWS/EC2"` | no |
57+
| num\_asg | the number of autoscaling groups passed | string | `"2"` | no |
58+
| period\_down | he period in seconds over which the selected metric statistic is applied. | string | `"120"` | no |
59+
| period\_up | he period in seconds over which the selected metric statistic is applied. | string | `"60"` | no |
60+
| policy\_type | The policy type, either SimpleScaling or StepScaling | string | `"SimpleScaling"` | no |
61+
| project | Project name to use | string | n/a | yes |
62+
| statistic | The statistic to apply to the alarm's associated metric. Either of the following is supported: | string | `"Average"` | no |
63+
| threshold\_down | The metric value to scale down | string | `"30"` | no |
64+
| threshold\_up | The metric value to scale up | string | `"80"` | no |
4365

4466
### Outputs
4567
/
4668

4769
### Example
4870

4971
```
50-
module "cpu-scaling" {
51-
source = "github.com/skyscrapers/terraform-bluegreen//cpu-scaling"
72+
module "scaling" {
73+
source = "github.com/skyscrapers/terraform-bluegreen//scaling"
5274
project = "example"
5375
name = "app"
5476
environment = "production"

cpu-scaling/main.tf scaling/main.tf

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
resource "aws_cloudwatch_metric_alarm" "alarm-cpu-down" {
1+
resource "aws_cloudwatch_metric_alarm" "alarm_down" {
22
count = "${var.num_asg}"
3-
alarm_name = "${var.environment}-${var.project}-${var.name}-cpu-down${count.index}"
3+
alarm_name = "${var.environment}-${var.project}-${var.name}-down${count.index}"
44
comparison_operator = "LessThanOrEqualToThreshold"
55
evaluation_periods = "${var.evaluation_periods}"
66
metric_name = "${var.metric_name}"
@@ -11,12 +11,12 @@ resource "aws_cloudwatch_metric_alarm" "alarm-cpu-down" {
1111
dimensions = "${map(var.dimension_name, var.dimension_value == "false" ? var.autoscaling_group_name[count.index] : var.dimension_value)}"
1212

1313
alarm_description = "This metric monitors CPU utilization down"
14-
alarm_actions = ["${aws_autoscaling_policy.down-cpu.*.arn}"]
14+
alarm_actions = ["${aws_autoscaling_policy.down.*.arn}"]
1515
}
1616

17-
resource "aws_cloudwatch_metric_alarm" "alarm-cpu-up" {
17+
resource "aws_cloudwatch_metric_alarm" "alarm_up" {
1818
count = "${var.num_asg}"
19-
alarm_name = "${var.environment}-${var.project}-${var.name}-cpu-up${count.index}"
19+
alarm_name = "${var.environment}-${var.project}-${var.name}-up${count.index}"
2020
comparison_operator = "GreaterThanOrEqualToThreshold"
2121
evaluation_periods = "${var.evaluation_periods}"
2222
metric_name = "${var.metric_name}"
@@ -27,22 +27,22 @@ resource "aws_cloudwatch_metric_alarm" "alarm-cpu-up" {
2727
dimensions = "${map(var.dimension_name, var.dimension_value == "false" ? var.autoscaling_group_name[count.index] : var.dimension_value)}"
2828

2929
alarm_description = "This metric monitors CPU utilization up"
30-
alarm_actions = ["${aws_autoscaling_policy.up-cpu.*.arn}"]
30+
alarm_actions = ["${aws_autoscaling_policy.up.*.arn}"]
3131
}
3232

33-
resource "aws_autoscaling_policy" "up-cpu" {
33+
resource "aws_autoscaling_policy" "up" {
3434
count = "${var.num_asg}"
35-
name = "${var.environment}-${var.project}-${var.name}-cpu-up${count.index}"
35+
name = "${var.environment}-${var.project}-${var.name}-up${count.index}"
3636
autoscaling_group_name = "${var.autoscaling_group_name[count.index]}"
3737
adjustment_type = "${var.adjustment_type}"
3838
policy_type = "${var.policy_type}"
3939
cooldown = "${var.cooldown_up}"
4040
scaling_adjustment = "${var.adjustment_up}"
4141
}
4242

43-
resource "aws_autoscaling_policy" "down-cpu" {
43+
resource "aws_autoscaling_policy" "down" {
4444
count = "${var.num_asg}"
45-
name = "${var.environment}-${var.project}-${var.name}-cpu-down${count.index}"
45+
name = "${var.environment}-${var.project}-${var.name}-down${count.index}"
4646
autoscaling_group_name = "${var.autoscaling_group_name[count.index]}"
4747
adjustment_type = "${var.adjustment_type}"
4848
policy_type = "${var.policy_type}"

cpu-scaling/variables.tf scaling/variables.tf

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ variable "evaluation_periods" {
55

66
variable "namespace" {
77
default = "AWS/EC2"
8-
description = "the namespace of the cpu cloudwatch metric"
8+
description = "the namespace of the cloudwatch metric"
99
}
1010

1111
variable "period_down" {
1212
default = "120"
13-
description = "he period in seconds over which the CPUUtilization statistic is applied."
13+
description = "he period in seconds over which the selected metric statistic is applied."
1414
}
1515

1616
variable "period_up" {
1717
default = "60"
18-
description = "he period in seconds over which the CPUUtilization statistic is applied."
18+
description = "he period in seconds over which the selected metric statistic is applied."
1919
}
2020

2121
variable "statistic" {
@@ -25,12 +25,12 @@ variable "statistic" {
2525

2626
variable "threshold_up" {
2727
default = "80"
28-
description = "The cpu value to scale up"
28+
description = "The metric value to scale up"
2929
}
3030

3131
variable "threshold_down" {
3232
default = "30"
33-
description = "The cpu value to scale down"
33+
description = "The metric value to scale down"
3434
}
3535

3636
variable "policy_type" {

0 commit comments

Comments
 (0)