Skip to content

Instantly share code, notes, and snippets.

@xueshanf
Created October 27, 2015 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xueshanf/01c11d9727d4ed5f9fef to your computer and use it in GitHub Desktop.
Save xueshanf/01c11d9727d4ed5f9fef to your computer and use it in GitHub Desktop.
resource "aws_autoscaling_group" "hosting" {
name = "hosting"
availability_zones = [ "us-west-2a", "us-west-2b", "us-west-2c"]
max_size = 9
min_size = 3
desired_capacity = 3
health_check_type = "EC2"
force_delete = true
launch_configuration = "${aws_launch_configuration.hosting.name}"
vpc_zone_identifier = ["${var.subnet_hosting-us-west-2a}","${var.subnet_hosting-us-west-2b}","${var.subnet_hosting-us-west-2c}"]
# Name tag
tag {
key = "Name"
value = "hosting"
propagate_at_launch = true
}
resource "aws_autoscaling_policy" "hosting" {
name = "${var.env}-hosting"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = "${aws_autoscaling_group.hosting.name}"
}
resource "aws_cloudwatch_metric_alarm" "hosting" {
alarm_name = "${var.env}-hosting-cpu"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "60"
statistic = "Average"
threshold = "60"
alarm_description = "This metric monitor sites EC2 cpu utilization"
alarm_actions = ["${aws_autoscaling_policy.hosting.arn}"]
}
resource "aws_sns_topic" "scaling_hosting" {
name = "${var.env}-scaling-hosting"
# Workaround until Terraform support Email protocol/confirmation
provisioner "local-exec" {
command = "aws --profile ${var.env} sns subscribe --topic-arn ${aws_sns_topic.scaling_hosting.arn} --protocol email --notification-endpoint ${var.env}-alerts@example.com"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment