-
Notifications
You must be signed in to change notification settings - Fork 0
/
asg.tf
187 lines (153 loc) · 5.31 KB
/
asg.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Create launch configuration for Auto scaling group
data "aws_ami" "launch_configuration_ami" {
most_recent = true
owners = var.owners
filter {
name = "root-device-type"
values = ["ebs"]
}
}
# Create private launch configuration for Auto scaling group
resource "aws_launch_configuration" "o4bproject_ec2_private_launch_configuration" {
image_id = data.aws_ami.launch_configuration_ami.id
instance_type = var.ec2_instance_type["magento"]
key_name = var.key_name
associate_public_ip_address = false
iam_instance_profile = aws_iam_instance_profile.o4bproject_ec2_iam_instance_profile.name
security_groups = [aws_security_group.o4bproject_dev_ec2_private_sg.id]
root_block_device {
volume_type = "gp2"
volume_size = 50
encrypted = true
}
ebs_block_device {
device_name = "/dev/sdf"
volume_type = "gp2"
volume_size = 10
encrypted = true
}
lifecycle {
create_before_destroy = true
}
user_data = <<EOF
EOF
}
# Create a launch template for the Auto scaling group
resource "aws_launch_template" "o4bproject_ec2_launch_template" {
name = "ampdev-ec2-launch-template"
block_device_mappings {
device_name = "/dev/sda1"
ebs {
volume_size = 20
}
}
cpu_options {
core_count = 4
threads_per_core = 2
}
disable_api_termination = true
ebs_optimized = true
iam_instance_profile {
name = aws_iam_instance_profile.o4bproject_ec2_iam_instance_profile.name
}
/*
image_id = aws_ami.varnish_ami.id
*/
instance_initiated_shutdown_behavior = "terminate"
instance_type = var.ec2_instance_type["magento"]
key_name = var.key_name
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 1
instance_metadata_tags = "enabled"
}
monitoring {
enabled = true
}
network_interfaces {
associate_public_ip_address = true
}
placement {
availability_zone = [var.private_subnet_availability_zone]
}
vpc_security_group_ids = [aws_security_group.o4bproject_dev_ec2_private_sg.id]
tag_specifications {
resource_type = "instance"
tags = {
Name = "ampdev-ec2-launch-template"
Environment = "dev"
}
}
user_data = file("install_varnish.sh")
}
# Create Auto scaling group for private subnet
resource "aws_autoscaling_group" "o4bproject_private_asg" {
name = "AmpDevO4b-private-asg"
vpc_zone_identifier = [aws_subnet.o4bproject-private[0].id, aws_subnet.o4bproject-private[1].id]
desired_capacity = var.desired_capacity
min_size = var.min_size
max_size = var.max_size
launch_configuration = aws_launch_configuration.o4bproject_ec2_private_launch_configuration.name
health_check_grace_period = var.health_check_grace_period
health_check_type = var.health_check_type
load_balancers = [aws_lb.o4bproject_inner_alb.arn]
lifecycle {
create_before_destroy = true
}
tag {
key = "Name"
propagate_at_launch = false
value = "AmpDevO4b-private-asg"
}
}
# Create autoscaling policy for scaleout
resource "aws_autoscaling_policy" "o4bproject_asg_scaleout_policy" {
name = "AmpDevO4b-asg-scaleout-policy"
autoscaling_group_name = aws_autoscaling_group.o4bproject_private_asg.name
policy_type = "SimpleScaling"
adjustment_type = "ChangeInCapacity"
scaling_adjustment = 1
cooldown = 300
}
# Create autoscaling policy for scalein
resource "aws_autoscaling_policy" "o4bproject_asg_scalein_policy" {
name = "AmpDevO4b-asg-scalein-policy"
autoscaling_group_name = aws_autoscaling_group.o4bproject_private_asg.name
policy_type = "SimpleScaling"
adjustment_type = "ChangeInCapacity"
scaling_adjustment = -1
cooldown = 300
}
# Create cloudwatch alarm metric to execute ASG policy for scaleout
resource "aws_cloudwatch_metric_alarm" "o4bproject_asg_scaleout_alarm" {
alarm_name = "AmpDevO4b-asg-scaleout-alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "80"
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.o4bproject_private_asg.name
}
alarm_description = "This metric monitors ec2 cpu utilization"
alarm_actions = [aws_autoscaling_policy.o4bproject_asg_scaleout_policy.arn]
}
# Create cloudwatch alarm metric to execute ASG policy for scalein
resource "aws_cloudwatch_metric_alarm" "o4bproject_asg_scalein_alarm" {
alarm_name = "AmpDevO4b-asg-scalein-alarm"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "20"
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.o4bproject_private_asg.name
}
alarm_description = "This metric monitors ec2 cpu utilization"
alarm_actions = [aws_autoscaling_policy.o4bproject_asg_scalein_policy.arn]
}