-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathasg.tf
201 lines (193 loc) · 7.68 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
188
189
190
191
192
193
194
195
196
197
198
199
/////////////////////////////////////////////////////[ AUTOSCALING CONFIGURATION ]////////////////////////////////////////
# # ---------------------------------------------------------------------------------------------------------------------#
# Create Launch Template for Autoscaling Groups
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_launch_template" "this" {
for_each = var.ec2
name = "${local.project}-${each.key}-ltpl"
iam_instance_profile { name = aws_iam_instance_profile.ec2[each.key].name }
image_id = data.aws_ami.distro.id
instance_type = each.value.instance_type
monitoring { enabled = true }
network_interfaces {
associate_public_ip_address = true
security_groups = [aws_security_group.ec2[each.key].id]
}
block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_size = each.value.volume_size
volume_type = "gp3"
encrypted = true
delete_on_termination = true
}
}
tag_specifications {
resource_type = "instance"
tags = merge(
local.default_tags,
{
Name = "${local.project}-${each.key}-ec2"
Instance_name = each.key
Hostname = "${each.key}.${var.brand}.internal"
}
)
}
tag_specifications {
resource_type = "volume"
tags = merge(
local.default_tags,
{
Name = "${local.project}-${each.key}-volume"
Instance_name = each.key
}
)
}
user_data = base64encode(<<EOF
#!/bin/bash
# update and install
apt -qq update
apt -qqy remove --purge awscli
apt -qqy install unzip
# install ssm manager
mkdir /tmp/ssm
cd /tmp/ssm
wget https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_arm64/amazon-ssm-agent.deb
dpkg -i amazon-ssm-agent.deb
systemctl enable amazon-ssm-agent
systemctl start amazon-ssm-agent
# install aws cli v2
mkdir /tmp/awscli
cd /tmp/awscli
curl "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
bash ./aws/install
EOF
)
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 1
instance_metadata_tags = "enabled"
}
update_default_version = true
lifecycle {
create_before_destroy = true
}
tags = {
Name = "${local.project}-${each.key}-ltpl"
}
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create Autoscaling Groups
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_autoscaling_group" "this" {
for_each = var.ec2
name = "${local.project}-${each.key}-asg"
vpc_zone_identifier = slice(keys(data.aws_availability_zone.all), 0, 2)
desired_capacity = each.value.desired_capacity
min_size = each.value.min_size
max_size = each.value.max_size
health_check_grace_period = var.asg["health_check_grace_period"]
health_check_type = var.asg["health_check_type"]
target_group_arns = each.key == "varnish" ? [aws_lb_target_group.this.arn] : []
launch_template {
name = aws_launch_template.this[each.key].name
version = "$Latest"
}
instance_refresh {
strategy = "Rolling"
preferences {
min_healthy_percentage = 50
skip_matching = false
scale_in_protected_instances = "Refresh"
}
}
lifecycle {
create_before_destroy = true
}
dynamic "tag" {
for_each = merge(local.default_tags,{Name="${local.project}-${each.key}-asg"})
content {
key = tag.key
value = tag.value
propagate_at_launch = false
}
}
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create Autoscaling groups actions for SNS topic email alerts
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_autoscaling_notification" "this" {
for_each = aws_autoscaling_group.this
group_names = [
aws_autoscaling_group.this[each.key].name
]
notifications = [
"autoscaling:EC2_INSTANCE_LAUNCH",
"autoscaling:EC2_INSTANCE_TERMINATE",
"autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
"autoscaling:EC2_INSTANCE_TERMINATE_ERROR",
]
topic_arn = aws_sns_topic.default.arn
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create Autoscaling policy for scale-out
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_autoscaling_policy" "scaleout" {
for_each = var.ec2
name = "${local.project}-${each.key}-asp-out"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.this[each.key].name
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create CloudWatch alarm metric to execute Autoscaling policy for scale-out
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_cloudwatch_metric_alarm" "scaleout" {
for_each = var.ec2
alarm_name = "${local.project}-${each.key} scale-out alarm"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.asp["evaluation_periods_out"]
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = var.asp["period"]
statistic = "Average"
threshold = var.asp["out_threshold"]
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.this[each.key].name
}
alarm_description = "${each.key} scale-out alarm - CPU exceeds ${var.asp["out_threshold"]} percent"
alarm_actions = [aws_autoscaling_policy.scaleout[each.key].arn]
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create Autoscaling policy for scale-in
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_autoscaling_policy" "scalein" {
for_each = var.ec2
name = "${local.project}-${each.key}-asp-in"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.this[each.key].name
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create CloudWatch alarm metric to execute Autoscaling policy for scale-in
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_cloudwatch_metric_alarm" "scalein" {
for_each = var.ec2
alarm_name = "${local.project}-${each.key} scale-in alarm"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = var.asp["evaluation_periods_in"]
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = var.asp["period"]
statistic = "Average"
threshold = var.asp["in_threshold"]
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.this[each.key].name
}
alarm_description = "${each.key} scale-in alarm - CPU less than ${var.asp["in_threshold"]} percent"
alarm_actions = [aws_autoscaling_policy.scalein[each.key].arn]
}