-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2.tf
93 lines (82 loc) · 3.18 KB
/
ec2.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
resource "aws_key_pair" "key_pair" {
key_name = "am-kp"
public_key = file("../am-kp.pub")
}
resource "aws_launch_template" "launch_template" {
name_prefix = "andrem-launch-template"
image_id = var.ami_id
instance_type = var.instance_type
key_name = aws_key_pair.key_pair.key_name
user_data = base64encode(templatefile("user_data.tftpl", {db_host=aws_db_instance.rds_instance.address, db_name = var.db_name, db_username = var.db_username, db_password = var.db_password}))
tag_specifications {
resource_type = "instance"
tags = {
Name = "andrem-launch-template"
}
}
network_interfaces {
associate_public_ip_address = true
subnet_id = module.vpc.public_subnets[0]
security_groups = [aws_security_group.ec2_sg.id]
}
}
resource "aws_autoscaling_group" "asg" {
name = "andrem-asg"
desired_capacity = 2
min_size = 2
max_size = 6
vpc_zone_identifier = module.vpc.public_subnets
target_group_arns = [aws_alb_target_group.alb_tg.arn]
launch_template {
id = aws_launch_template.launch_template.id
version = "$Latest"
}
}
resource "aws_autoscaling_policy" "scale_up_policy" {
name = "andrem-scale-up-policy"
scaling_adjustment = 1
adjustment_type = var.adjustment_type # ChangeInCapacity
cooldown = var.cooldown # 5 minutes
autoscaling_group_name = aws_autoscaling_group.asg.name
}
resource "aws_autoscaling_policy" "scale_down_policy" {
name = "andrem-scale-down-policy"
scaling_adjustment = -1
adjustment_type = var.adjustment_type # ChangeInCapacity
cooldown = var.cooldown # 5 minutes
autoscaling_group_name = aws_autoscaling_group.asg.name
}
resource "aws_cloudwatch_metric_alarm" "high_cpu_alarm" {
alarm_name = "andrem-high-cpu-alarm"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.evaluation_periods
metric_name = var.metric_name
namespace = var.namespace
period = var.period
statistic = var.statistic
threshold = 70
alarm_description = "This metric monitors ec2 high cpu utilization"
alarm_actions = [aws_autoscaling_policy.scale_up_policy.arn]
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.asg.name
}
}
resource "aws_cloudwatch_metric_alarm" "low_cpu_alarm" {
alarm_name = "andrem-low-cpu-alarm"
comparison_operator = "LessThanThreshold"
evaluation_periods = var.evaluation_periods
metric_name = var.metric_name
namespace = var.namespace
period = var.period
statistic = var.statistic
threshold = 20
alarm_description = "This metric monitors ec2 low cpu utilization"
alarm_actions = [aws_autoscaling_policy.scale_down_policy.arn]
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.asg.name
}
}
resource "aws_autoscaling_attachment" "asg_attachment" {
autoscaling_group_name = aws_autoscaling_group.asg.name
lb_target_group_arn = aws_alb_target_group.alb_tg.arn
}