Created
March 19, 2022 23:53
-
-
Save psujit775/1885d085d2363c0bef8e928878d8217f to your computer and use it in GitHub Desktop.
Setup Auto Scaling along with scale up and down policy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Setup Auto Scaling along with scale up and down policy | |
1. Build AMI | |
we need to first build AMI by installing Amazon monitoring scripts to monitor memory utilization and send to cloudwathc and also install other required dependencies to run application. | |
2. Create launch configuration | |
we create launch configuration with options like availability zone, security groups, vpc, etc. | |
3. Create autoscaling group and autoscaling policy | |
This points at our earlier launch configuration and it lauch instances based on launch configuration. The tag,vpc security groups is propogated to any launched instance at launch. Also we define min and max number of instances. | |
In autoscaling policy we define metric type like Average CPU and Memory utilization based on clodwatch metrics. | |
In autoscaling group we also define load balacer and target group to attach launched instances. | |
Below is ths sample Terraform code for Auto Scaling Groups | |
resource "aws_launch_configuration" "demo-lc" { | |
name = "demo-lc" | |
image_id = <AMI ID> | |
instance_type = <instance type> | |
user_data = file("init-script.sh") | |
root_block_device { | |
volume_type = "gp2" | |
volume_size = "50" | |
} | |
} | |
resource "aws_autoscaling_group" "demo-asg" { | |
availability_zones = ["ap-south-1"] | |
name = "demo-asg" | |
max_size = "20" | |
min_size = "1" | |
health_check_grace_period = 300 | |
health_check_type = "EC2" | |
desired_capacity = 2 | |
force_delete = true | |
launch_configuration = "${aws_launch_configuration.demo-lc.name}" | |
} | |
resource "aws_autoscaling_policy" "agents-scale-up" { | |
name = "agents-scale-up" | |
scaling_adjustment = 1 | |
adjustment_type = "ChangeInCapacity" | |
cooldown = 300 | |
autoscaling_group_name = "${aws_autoscaling_group.demo-asg.name}" | |
} | |
resource "aws_autoscaling_policy" "agents-scale-down" { | |
name = "agents-scale-down" | |
scaling_adjustment = -1 | |
adjustment_type = "ChangeInCapacity" | |
cooldown = 300 | |
autoscaling_group_name = "${aws_autoscaling_group.demo-asg.name}" | |
} | |
resource "aws_cloudwatch_metric_alarm" "memory-high" { | |
alarm_name = "mem-util-high-agents" | |
comparison_operator = "GreaterThanOrEqualToThreshold" | |
evaluation_periods = "2" | |
metric_name = "MemoryUtilization" | |
namespace = "System/Linux" | |
period = "300" | |
statistic = "Average" | |
threshold = "80" | |
alarm_description = "This metric monitors ec2 memory for high utilization on agent hosts" | |
alarm_actions = [ | |
"${aws_autoscaling_policy.agents-scale-up.arn}" | |
] | |
dimensions = { | |
AutoScalingGroupName = "${aws_autoscaling_group.demo-asg.name}" | |
} | |
} | |
resource "aws_cloudwatch_metric_alarm" "memory-low" { | |
alarm_name = "mem-util-low-agents" | |
comparison_operator = "LessThanOrEqualToThreshold" | |
evaluation_periods = "2" | |
metric_name = "MemoryUtilization" | |
namespace = "System/Linux" | |
period = "300" | |
statistic = "Average" | |
threshold = "40" | |
alarm_description = "This metric monitors ec2 memory for low utilization on agent hosts" | |
alarm_actions = [ | |
"${aws_autoscaling_policy.agents-scale-down.arn}" | |
] | |
dimensions = { | |
AutoScalingGroupName = "${aws_autoscaling_group.demo-asg.name}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment