forked from Rishang/terraform-aws-fargate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
302 lines (229 loc) · 8.07 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
locals {
point_to_lb = var.point_to_lb == true ? 1 : 0
point_to_r53 = var.subdomain != "" && var.point_to_r53 == true && var.point_to_lb == true ? 1 : 0
fargate_version = "LATEST"
failure_threshold = 2
enable_discovery = var.enable_discovery == true ? 1 : 0
container_name = var.container_name == "" ? var.service : var.container_name
# convert : demo.example.com to example.com
root_domain = join(".", slice(split(".", var.subdomain), 1, length(split(".", var.subdomain))))
}
# ------------------------ DATA ------------------------------
data "aws_lb_listener" "https" {
count = local.point_to_lb
arn = var.listener_arn_https
}
data "aws_lb" "lb" {
count = local.point_to_lb
arn = data.aws_lb_listener.https[0].load_balancer_arn
}
# ----------------- ALB TARGET GROUP ------------------------------
resource "aws_lb_target_group" "lb_tg" {
count = local.point_to_lb
lifecycle {
create_before_destroy = true
}
name = "${var.cluster}-${var.service}"
port = var.container_port
protocol = "HTTP"
target_type = "ip"
vpc_id = var.vpc_id
health_check {
interval = var.health_check_interval
path = var.health_check_path
protocol = "HTTP"
matcher = var.health_check_matcher
healthy_threshold = 3
unhealthy_threshold = local.failure_threshold
timeout = 10
}
tags = {
Environment = var.EnvironmentName
Service = var.service
}
}
# ----------------- ALB RULES ------------------------------
resource "aws_lb_listener_rule" "https" {
count = local.point_to_lb
listener_arn = var.listener_arn_https
lifecycle {
create_before_destroy = true
}
action {
type = "forward"
target_group_arn = aws_lb_target_group.lb_tg[0].arn
}
condition {
path_pattern {
values = var.path_pattern
}
}
dynamic "condition" {
for_each = toset(var.subdomain != "" ? ["create"] : [])
content {
host_header {
values = [var.subdomain]
}
}
}
}
# ----------------- SERVICE DISCOVERY ---------------------
resource "aws_service_discovery_service" "fargate" {
count = local.enable_discovery
description = "discovery for service: ${var.service} for cluster: ${var.cluster}"
name = var.service
force_destroy = true
dns_config {
namespace_id = var.namespace_id
dns_records {
ttl = 10
type = "A"
}
}
health_check_custom_config {
failure_threshold = local.failure_threshold
}
tags = var.tags
}
# ----------------- ECS SERVICE ------------------------------
resource "aws_ecs_service" "fargate" {
lifecycle {
ignore_changes = [desired_count]
}
name = var.service
cluster = var.cluster
task_definition = var.task_definition_arn
desired_count = var.min_count
platform_version = local.fargate_version
force_new_deployment = var.force_new_deployment
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
deployment_maximum_percent = var.deployment_maximum_percent
health_check_grace_period_seconds = 0
enable_ecs_managed_tags = var.enable_ecs_managed_tags
# for stability a 1 dedicated fargate instance and rest spot
# 1 dedicated per 5 spot
dynamic "capacity_provider_strategy" {
for_each = toset(var.fargate_spot == true && var.force_spot == false ? ["create"] : [])
content {
base = 1
capacity_provider = "FARGATE"
weight = 1
}
}
dynamic "capacity_provider_strategy" {
for_each = toset(var.fargate_spot == true ? ["create"] : [])
content {
base = var.fargate_spot == true && var.force_spot == true ? 1 : 0
capacity_provider = "FARGATE_SPOT"
weight = 5
}
}
dynamic "service_registries" {
for_each = toset(var.enable_discovery == true ? ["create"] : [])
content {
registry_arn = aws_service_discovery_service.fargate[0].arn
container_name = local.container_name
}
}
network_configuration {
subnets = var.subnets
security_groups = var.security_groups
assign_public_ip = var.assign_public_ip
}
dynamic "load_balancer" {
for_each = toset(local.point_to_lb == 1 ? ["true"] : [])
content {
target_group_arn = aws_lb_target_group.lb_tg[0].arn
container_name = local.container_name
container_port = var.container_port
}
}
tags = merge(
var.tags,
{
Name = var.service,
Environment = var.EnvironmentName
}
)
}
# ----------------- DOMAIN ------------------------------
data "aws_route53_zone" "web" {
count = local.point_to_r53
name = local.root_domain
}
resource "aws_route53_record" "web" {
count = local.point_to_r53
depends_on = [aws_ecs_service.fargate]
zone_id = data.aws_route53_zone.web[0].id
name = var.subdomain
type = "A"
alias {
name = data.aws_lb.lb[0].dns_name
zone_id = data.aws_lb.lb[0].zone_id
evaluate_target_health = true
}
}
# ----------------- AUTOSCALING ------------------------------
# ab [options] [http[s]://]hostname[:port]/path
# ab -n 100000 -c 1000 "http://api.output.com:80/"
# cpu base scale
resource "aws_appautoscaling_target" "scaling" {
count = (var.cpu_scale_target > 0 || var.memory_scale_target > 0) ? 1 : 0
min_capacity = var.min_count
max_capacity = var.scale_max_capacity
resource_id = "service/${var.cluster}/${aws_ecs_service.fargate.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_appautoscaling_policy" "cpu_scale_up_policy" {
count = var.cpu_scale_target > 0 ? 1 : 0
name = "ECSServiceAverageCPUUtilization:${aws_appautoscaling_target.scaling[count.index].resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = "service/${var.cluster}/${var.service}"
scalable_dimension = aws_appautoscaling_target.scaling[count.index].scalable_dimension
service_namespace = "ecs"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = var.cpu_scale_target
scale_in_cooldown = var.scale_in_cooldown
scale_out_cooldown = var.scale_out_cooldown
}
}
# memory base scale
resource "aws_appautoscaling_policy" "memory_scale_up_policy" {
count = var.memory_scale_target > 0 ? 1 : 0
name = "ECSServiceAverageMemoryUtilization:${aws_appautoscaling_target.scaling[count.index].resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = "service/${var.cluster}/${var.service}"
scalable_dimension = aws_appautoscaling_target.scaling[count.index].scalable_dimension
service_namespace = "ecs"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
}
target_value = var.memory_scale_target
scale_in_cooldown = var.scale_in_cooldown
scale_out_cooldown = var.scale_out_cooldown
}
}
# alb based auto scaling
resource "aws_appautoscaling_policy" "scale_up_policy" {
count = var.lb_scale_target > 0 ? 1 : 0
name = "ALBRequestCountPerTarget:${aws_appautoscaling_target.scaling[count.index].resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = "service/${var.cluster}/${var.service}"
scalable_dimension = aws_appautoscaling_target.scaling[count.index].scalable_dimension
service_namespace = "ecs"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ALBRequestCountPerTarget"
# app/my-alb/778d41231b141a0f/targetgroup/my-alb-target-group/943f017f100becff
resource_label = "${data.aws_lb.lb[0].arn_suffix}/${aws_lb_target_group.lb_tg[0].arn_suffix}"
}
target_value = var.lb_scale_target
scale_in_cooldown = var.scale_in_cooldown
scale_out_cooldown = var.scale_out_cooldown
}
}