This repository was archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathmain.tf
292 lines (238 loc) · 7.24 KB
/
main.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
*/
variable "name" {
description = "The cluster name, e.g cdn"
type = "string"
}
variable "environment" {
description = "Environment tag, e.g prod"
type = "string"
}
variable "vpc_id" {
description = "VPC ID"
type = "string"
}
variable "image_id" {
description = "AMI Image ID"
type = "string"
}
variable "subnet_ids" {
description = "list of subnet IDs"
type = "list"
}
variable "key_name" {
description = "SSH key name to use"
type = "string"
}
variable "ingress_cidr" {
description = "Comma separated list of ingress cidrs"
type = "string"
}
variable "iam_instance_profile" {
description = "Instance profile ARN to use in the launch configuration"
type = "string"
}
variable "region" {
description = "AWS Region"
type = "string"
}
variable "availability_zones" {
description = "list of AZs"
type = "list"
}
variable "instance_type" {
description = "The instance type to use, e.g t2.small"
type = "string"
}
variable "instance_ebs_optimized" {
description = "When set to true the instance will be launched with EBS optimized turned on"
default = true
}
variable "min_size" {
description = "Minimum instance count"
default = 3
}
variable "max_size" {
description = "Maxmimum instance count"
default = 100
}
variable "desired_capacity" {
description = "Desired instance count"
default = 3
}
variable "associate_public_ip_address" {
description = "Should created instances be publicly accessible (if the SG allows)"
default = false
}
variable "custom_script" {
description = "Custom instance bootupt script"
default = ""
type = "string"
}
variable "load_balancers" {
description = "ASG ELBs"
default = []
}
variable "health_check_grace_period" {
description = "Time (in seconds) after instance comes into service before checking health"
default = 300
}
variable "target_group_arns" {
description = "Application load balancer target group ARN(s) if any"
default = []
}
variable "termination_policies" {
description = "OldestInstance, NewestInstance, OldestLaunchConfiguration, ClosestToNextInstanceHour, Default"
default = ["OldestLaunchConfiguration", "Default"]
}
variable "wait_for_capacity_timeout" {
description = "A maximum duration that Terraform should wait for ASG instances to be healthy before timing out"
default = "10m"
}
resource "aws_security_group" "asg" {
name = "${var.name}-asg"
vpc_id = "${var.vpc_id}"
description = "Allows traffic from and to the EC2 instances of the ${var.name} ASG"
ingress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["${split(",", var.ingress_cidr)}"]
}
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = "ASG (${var.name})"
Environment = "${var.environment}"
}
lifecycle {
create_before_destroy = true
}
}
data "template_file" "instance_config" {
template = "${file("${path.module}/files/instance-config.yml.tpl")}"
vars {
custom_script = "${var.custom_script}"
}
}
resource "aws_launch_configuration" "main" {
name_prefix = "${format("%s-", var.name)}"
image_id = "${var.image_id}"
instance_type = "${var.instance_type}"
ebs_optimized = "${var.instance_ebs_optimized}"
iam_instance_profile = "${var.iam_instance_profile}"
key_name = "${var.key_name}"
security_groups = ["${aws_security_group.asg.id}"]
user_data = "${data.template_file.instance_config.rendered}"
associate_public_ip_address = "${var.associate_public_ip_address}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "main" {
name = "${var.name}"
availability_zones = ["${var.availability_zones}"]
vpc_zone_identifier = ["${var.subnet_ids}"]
launch_configuration = "${aws_launch_configuration.main.id}"
min_size = "${var.min_size}"
max_size = "${var.max_size}"
desired_capacity = "${var.desired_capacity}"
health_check_grace_period = "${var.health_check_grace_period}"
health_check_type = "ELB"
load_balancers = ["${var.load_balancers}"]
target_group_arns = ["${var.target_group_arns}"]
termination_policies = "${var.termination_policies}"
wait_for_capacity_timeout = "${var.wait_for_capacity_timeout}"
tag {
key = "Name"
value = "${var.name}"
propagate_at_launch = true
}
tag {
key = "Cluster"
value = "${var.name}"
propagate_at_launch = true
}
tag {
key = "Environment"
value = "${var.environment}"
propagate_at_launch = true
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_policy" "scale_up" {
name = "${var.name}-scaleup"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = "${aws_autoscaling_group.main.name}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_policy" "scale_down" {
name = "${var.name}-scaledown"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = "${aws_autoscaling_group.main.name}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
alarm_name = "${var.name}-cpureservation-high"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "300"
statistic = "Maximum"
threshold = "60"
dimensions {
AutoScalingGroupName = "${aws_autoscaling_group.main.name}"
}
alarm_description = "Scale up if the cpu reservation is above 60% for 10 minutes"
alarm_actions = ["${aws_autoscaling_policy.scale_up.arn}"]
lifecycle {
create_before_destroy = true
}
# This is required to make cloudwatch alarms creation sequential, AWS doesn't
# support modifying alarms concurrently.
depends_on = ["aws_autoscaling_group.main"]
}
resource "aws_cloudwatch_metric_alarm" "cpu_low" {
alarm_name = "${var.name}-cpureservation-low"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "300"
statistic = "Maximum"
threshold = "10"
dimensions {
AutoScalingGroupName = "${aws_autoscaling_group.main.name}"
}
alarm_description = "Scale down if the cpu reservation is below 10% for 10 minutes"
alarm_actions = ["${aws_autoscaling_policy.scale_down.arn}"]
lifecycle {
create_before_destroy = true
}
# This is required to make cloudwatch alarms creation sequential, AWS doesn't
# support modifying alarms concurrently.
depends_on = ["aws_cloudwatch_metric_alarm.cpu_high"]
}
// The asg name, e.g cdn
output "name" {
value = "${var.name}"
}
// The asg security group ID.
output "security_group_id" {
value = "${aws_security_group.asg.id}"
}