Skip to content

Commit

Permalink
Adding alarms and scaling policies to enable autoscaling on the group
Browse files Browse the repository at this point in the history
  • Loading branch information
jnonino committed Jul 10, 2019
1 parent 93d101c commit 57a9ae5
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
64 changes: 64 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,67 @@ output "asg_target_group_arns" {
description = "(Optional) list of Target Group ARNs that apply to this AutoScaling Group."
value = aws_autoscaling_group.asg.target_group_arns
}

# ---------------------------------------------------------------------------------------------------------------------
# AUTOSCALING POLICIES
# ---------------------------------------------------------------------------------------------------------------------
output "cpu_high_name" {
description = "The scaling policy's name."
value = aws_autoscaling_policy.cpu_high.name
}
output "cpu_high_arn" {
description = "The ARN assigned by AWS to the scaling policy."
value = aws_autoscaling_policy.cpu_high.arn
}
output "cpu_high_autoscaling_group_name" {
description = "The scaling policy's assigned autoscaling group"
value = aws_autoscaling_policy.cpu_high.autoscaling_group_name
}
output "cpu_high_adjustment_type" {
description = "The scaling policy's adjustment type."
value = aws_autoscaling_policy.cpu_high.adjustment_type
}
output "cpu_high_policy_type" {
description = "The scaling policy's type."
value = aws_autoscaling_policy.cpu_high.policy_type
}
output "cpu_low_name" {
description = "The scaling policy's name."
value = aws_autoscaling_policy.cpu_low.name
}
output "cpu_low_arn" {
description = "The ARN assigned by AWS to the scaling policy."
value = aws_autoscaling_policy.cpu_low.arn
}
output "cpu_low_autoscaling_group_name" {
description = "The scaling policy's assigned autoscaling group"
value = aws_autoscaling_policy.cpu_low.autoscaling_group_name
}
output "cpu_low_adjustment_type" {
description = "The scaling policy's adjustment type."
value = aws_autoscaling_policy.cpu_low.adjustment_type
}
output "cpu_low_policy_type" {
description = "The scaling policy's type."
value = aws_autoscaling_policy.cpu_low.policy_type
}

# ---------------------------------------------------------------------------------------------------------------------
# CLOUDWATCH ALARMS
# ---------------------------------------------------------------------------------------------------------------------
output "cpu_high_alarm_id" {
description = "The ID of the health check."
value = aws_cloudwatch_metric_alarm.cpu_high_alarm.id
}
output "cpu_high_alarm_arn" {
description = "(Optional) list of Target Group ARNs that apply to this AutoScaling Group."
value = aws_cloudwatch_metric_alarm.cpu_high_alarm.arn
}
output "cpu_low_alarm_id" {
description = "The ID of the health check."
value = aws_cloudwatch_metric_alarm.cpu_low_alarm.id
}
output "cpu_low_alarm_arn" {
description = "(Optional) list of Target Group ARNs that apply to this AutoScaling Group."
value = aws_cloudwatch_metric_alarm.cpu_low_alarm.arn
}
55 changes: 55 additions & 0 deletions scaling_policies.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# ---------------------------------------------------------------------------------------------------------------------
# AUTOSCALING POLICIES
# ---------------------------------------------------------------------------------------------------------------------
# Scaling UP - CPU High
resource "aws_autoscaling_policy" "cpu_high" {
name = "${var.name_preffix}-cpu-high"
autoscaling_group_name = "${aws_autoscaling_group.asg.name}"
adjustment_type = "ChangeInCapacity"
policy_type = "SimpleScaling"
scaling_adjustment = "1"
cooldown = "300"
}
# Scaling DOWN - CPU Low
resource "aws_autoscaling_policy" "cpu_low" {
name = "${var.name_preffix}-cpu-high"
autoscaling_group_name = "${aws_autoscaling_group.asg.name}"
adjustment_type = "ChangeInCapacity"
policy_type = "SimpleScaling"
scaling_adjustment = "-1"
cooldown = "300"
}

# ---------------------------------------------------------------------------------------------------------------------
# CLOUDWATCH METRIC ALARMS
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_cloudwatch_metric_alarm" "cpu_high_alarm" {
alarm_name = "${var.name_preffix}-cpu-high-alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "60"
statistic = "Average"
threshold = "80"
actions_enabled = true
alarm_actions = ["${aws_autoscaling_policy.cpu_high.arn}"]
dimensions = {
"AutoScalingGroupName" = "${aws_autoscaling_group.asg.name}"
}
}
resource "aws_cloudwatch_metric_alarm" "cpu_low_alarm" {
alarm_name = "${var.name_preffix}-cpu-low-alarm"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "60"
statistic = "Average"
threshold = "10"
actions_enabled = true
alarm_actions = ["${aws_autoscaling_policy.cpu_low.arn}"]
dimensions = {
"AutoScalingGroupName" = "${aws_autoscaling_group.asg.name}"
}
}

0 comments on commit 57a9ae5

Please sign in to comment.