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
390 lines (320 loc) · 9.85 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* ECS Cluster creates a cluster with the following features:
*
* - Autoscaling groups
* - Instance tags for filtering
* - EBS volume for docker resources
*
*
* Usage:
*
* module "cdn" {
* source = "github.com/segmentio/stack/ecs-cluster"
* environment = "prod"
* name = "cdn"
* vpc_id = "vpc-id"
* image_id = "ami-id"
* subnet_ids = ["1" ,"2"]
* key_name = "ssh-key"
* security_groups = "1,2"
* iam_instance_profile = "id"
* region = "us-west-2"
* availability_zones = ["a", "b"]
* instance_type = "t2.small"
* }
*
*/
variable "name" {
description = "The cluster name, e.g cdn"
}
variable "environment" {
description = "Environment tag, e.g prod"
}
variable "vpc_id" {
description = "VPC ID"
}
variable "image_id" {
description = "AMI Image ID"
}
variable "subnet_ids" {
description = "List of subnet IDs"
type = "list"
}
variable "key_name" {
description = "SSH key name to use"
}
variable "security_groups" {
description = "List of security groups"
type = "list"
}
variable "iam_instance_profile" {
description = "Instance profile ARN to use in the launch configuration"
}
variable "region" {
description = "AWS Region"
}
variable "availability_zones" {
description = "List of AZs"
type = "list"
}
variable "instance_type" {
description = "The instance type to use, e.g t2.small"
}
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 "root_volume_size" {
description = "Root volume size in GB"
default = 25
}
variable "docker_volume_size" {
description = "Attached EBS volume size in GB"
default = 25
}
variable "docker_auth_type" {
description = "The docker auth type, see https://godoc.org/github.com/aws/amazon-ecs-agent/agent/engine/dockerauth for the possible values"
default = ""
}
variable "docker_auth_data" {
description = "A JSON object providing the docker auth data, see https://godoc.org/github.com/aws/amazon-ecs-agent/agent/engine/dockerauth for the supported formats"
default = ""
}
variable "extra_cloud_config_type" {
description = "Extra cloud config type"
default = "text/cloud-config"
}
variable "extra_cloud_config_content" {
description = "Extra cloud config content"
default = ""
}
resource "aws_security_group" "cluster" {
name = "${var.name}-ecs-cluster"
vpc_id = "${var.vpc_id}"
description = "Allows traffic from and to the EC2 instances of the ${var.name} ECS cluster"
ingress {
from_port = 0
to_port = 0
protocol = -1
security_groups = ["${var.security_groups}"]
}
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = "ECS cluster (${var.name})"
Environment = "${var.environment}"
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_ecs_cluster" "main" {
name = "${var.name}"
lifecycle {
create_before_destroy = true
}
}
data "template_file" "ecs_cloud_config" {
template = "${file("${path.module}/files/cloud-config.yml.tpl")}"
vars {
environment = "${var.environment}"
name = "${var.name}"
region = "${var.region}"
docker_auth_type = "${var.docker_auth_type}"
docker_auth_data = "${var.docker_auth_data}"
}
}
data "template_cloudinit_config" "cloud_config" {
gzip = false
base64_encode = false
part {
content_type = "text/cloud-config"
content = "${data.template_file.ecs_cloud_config.rendered}"
}
part {
content_type = "${var.extra_cloud_config_type}"
content = "${var.extra_cloud_config_content}"
}
}
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.cluster.id}"]
user_data = "${data.template_cloudinit_config.cloud_config.rendered}"
associate_public_ip_address = "${var.associate_public_ip_address}"
# root
root_block_device {
volume_type = "gp2"
volume_size = "${var.root_volume_size}"
}
# docker
ebs_block_device {
device_name = "/dev/xvdcz"
volume_type = "gp2"
volume_size = "${var.docker_volume_size}"
}
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}"
termination_policies = ["OldestLaunchConfiguration", "Default"]
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 = "CPUReservation"
namespace = "AWS/ECS"
period = "300"
statistic = "Maximum"
threshold = "90"
dimensions {
ClusterName = "${aws_ecs_cluster.main.name}"
}
alarm_description = "Scale up if the cpu reservation is above 90% for 10 minutes"
alarm_actions = ["${aws_autoscaling_policy.scale_up.arn}"]
lifecycle {
create_before_destroy = true
}
}
resource "aws_cloudwatch_metric_alarm" "memory_high" {
alarm_name = "${var.name}-memoryreservation-high"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "MemoryReservation"
namespace = "AWS/ECS"
period = "300"
statistic = "Maximum"
threshold = "90"
dimensions {
ClusterName = "${aws_ecs_cluster.main.name}"
}
alarm_description = "Scale up if the memory reservation is above 90% 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_cloudwatch_metric_alarm.cpu_high"]
}
resource "aws_cloudwatch_metric_alarm" "cpu_low" {
alarm_name = "${var.name}-cpureservation-low"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUReservation"
namespace = "AWS/ECS"
period = "300"
statistic = "Maximum"
threshold = "10"
dimensions {
ClusterName = "${aws_ecs_cluster.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.memory_high"]
}
resource "aws_cloudwatch_metric_alarm" "memory_low" {
alarm_name = "${var.name}-memoryreservation-low"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "MemoryReservation"
namespace = "AWS/ECS"
period = "300"
statistic = "Maximum"
threshold = "10"
dimensions {
ClusterName = "${aws_ecs_cluster.main.name}"
}
alarm_description = "Scale down if the memory 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_low"]
}
// The cluster name, e.g cdn
output "name" {
value = "${var.name}"
}
// The cluster security group ID.
output "security_group_id" {
value = "${aws_security_group.cluster.id}"
}