-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
ec2_autoscaling.tf
265 lines (255 loc) · 10.9 KB
/
ec2_autoscaling.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/////////////////////////////////////////////////////[ AUTOSCALING CONFIGURATION ]////////////////////////////////////////
# # ---------------------------------------------------------------------------------------------------------------------#
# Create EC2 service role
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_iam_role" "ec2" {
for_each = var.ec2
name = "${local.project}-EC2InstanceRole-${each.key}"
description = "Allows EC2 instances to call AWS services on your behalf"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Attach policies to EC2 service role
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_iam_role_policy_attachment" "ec2" {
for_each = { for policy in [ for role,policy in setproduct(keys(var.ec2),var.ec2_instance_profile_policy): { role = policy[0] , policy = policy[1]} ] : "${policy.role}-${policy.policy}" => policy }
role = aws_iam_role.ec2[each.value.role].name
policy_arn = each.value.policy
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create inline policy for EC2 service role to publish sns message
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_iam_role_policy" "sns_publish" {
for_each = var.ec2
name = "${local.project}EC2ProfileSNS${title(each.key)}"
role = aws_iam_role.ec2[each.key].id
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Sid = "EC2ProfileSNSPublishPolicy${each.key}",
Effect = "Allow",
Action = [
"sns:Publish"
],
Resource = aws_sns_topic.default.arn
}]
})
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create inline policy for EC2 service role to limit CodeCommit access
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_iam_role_policy" "codecommit_access" {
for_each = var.ec2
name = "${local.project}PolicyCodeCommitAccess${title(each.key)}"
role = aws_iam_role.ec2[each.key].id
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Sid = "codecommitaccessapp${each.key}",
Effect = "Allow",
Action = [
"codecommit:Get*",
"codecommit:List*",
"codecommit:GitPull"
],
Resource = aws_codecommit_repository.app.arn
Condition = {
StringEqualsIfExists = {
"codecommit:References" = ["refs/heads/main"]
}
}
},
{
Sid = "codecommitaccessservices${each.key}",
Effect = "Allow",
Action = [
"codecommit:Get*",
"codecommit:List*",
"codecommit:GitPull"
],
Resource = aws_codecommit_repository.services.arn
}]
})
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create EC2 Instance Profile
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_iam_instance_profile" "ec2" {
for_each = var.ec2
name = "${local.project}-EC2InstanceProfile-${each.key}"
role = aws_iam_role.ec2[each.key].name
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create EC2 ebs default encryption
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_ebs_encryption_by_default" "this" {
enabled = true
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create Launch Template for Autoscaling Groups - user_data converted
# # ---------------------------------------------------------------------------------------------------------------------#
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 = element(values(data.external.packer[each.key].result), 0)
instance_type = each.value
monitoring { enabled = var.asg["monitoring"] }
network_interfaces {
associate_public_ip_address = true
security_groups = [aws_security_group.ec2.id]
}
dynamic "tag_specifications" {
for_each = toset(["instance","volume"])
content {
resource_type = tag_specifications.key
tags = merge(var.default_tags,{Name = "${local.project}-${each.key}-ec2", Project = "${local.project}"})
}
}
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 1
instance_metadata_tags = "enabled"
}
user_data = filebase64("${abspath(path.root)}/user_data/${each.key}")
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 = values(aws_subnet.this).*.id
desired_capacity = var.asg["desired_capacity"]
min_size = var.asg["min_size"]
max_size = var.asg["max_size"]
health_check_grace_period = var.asg["health_check_grace_period"]
health_check_type = var.asg["health_check_type"]
target_group_arns = [aws_lb_target_group.this[each.key].arn]
dynamic "warm_pool" {
for_each = var.asg["warm_pool"] == "enabled" ? [var.ec2] : []
content {
pool_state = "Stopped"
min_size = var.asg["min_size"]
max_group_prepared_capacity = var.asg["max_size"]
}
}
launch_template {
name = aws_launch_template.this[each.key].name
version = "$Latest"
}
instance_refresh {
strategy = "Rolling"
}
lifecycle {
create_before_destroy = true
}
dynamic "tag" {
for_each = merge(var.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]
}