forked from ExpediaGroup/apiary-federation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs.tf
130 lines (110 loc) · 3.98 KB
/
ecs.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
/**
* Copyright (C) 2018-2019 Expedia Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
*/
resource "aws_ecs_cluster" "waggledance" {
count = var.wd_instance_type == "ecs" ? 1 : 0
name = local.instance_alias
tags = var.tags
}
resource "aws_ecs_service" "waggledance_service" {
count = var.wd_instance_type == "ecs" ? 1 : 0
name = "${local.instance_alias}-service"
launch_type = "FARGATE"
cluster = aws_ecs_cluster.waggledance[0].id
task_definition = aws_ecs_task_definition.waggledance[0].arn
desired_count = var.wd_ecs_task_count
network_configuration {
security_groups = [aws_security_group.wd_sg.id]
subnets = var.subnets
}
dynamic "service_registries" {
for_each = var.enable_autoscaling ? [] : ["1"]
content {
registry_arn = aws_service_discovery_service.metastore_proxy[0].arn
}
}
dynamic "load_balancer" {
for_each = var.enable_autoscaling ? ["1"] : []
content {
target_group_arn = aws_lb_target_group.waggledance[0].arn
container_name = "waggledance"
container_port = local.wd_port
}
}
}
resource "aws_ecs_task_definition" "waggledance" {
count = var.wd_instance_type == "ecs" ? 1 : 0
family = local.instance_alias
task_role_arn = aws_iam_role.waggledance_task[0].arn
execution_role_arn = aws_iam_role.waggledance_task_exec[0].arn
network_mode = "awsvpc"
memory = var.memory
cpu = var.cpu
requires_compatibilities = ["EC2", "FARGATE"]
container_definitions = data.template_file.waggledance.rendered
tags = var.tags
}
resource "aws_appautoscaling_target" "waggledance" {
count = var.wd_instance_type == "ecs" && var.enable_autoscaling ? 1 : 0
max_capacity = var.wd_ecs_max_task_count
min_capacity = var.wd_ecs_task_count
resource_id = "service/${local.instance_alias}/${local.instance_alias}-service"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
depends_on = [
aws_ecs_service.waggledance_service,
]
}
resource "aws_appautoscaling_policy" "waggledance" {
count = var.wd_instance_type == "ecs" && var.enable_autoscaling ? 1 : 0
name = "cpu"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.waggledance[0].resource_id
scalable_dimension = aws_appautoscaling_target.waggledance[0].scalable_dimension
service_namespace = aws_appautoscaling_target.waggledance[0].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = var.wd_target_cpu_percentage
scale_in_cooldown = var.cpu_scale_in_cooldown
scale_out_cooldown = var.cpu_scale_out_cooldown
}
depends_on = [aws_appautoscaling_target.waggledance]
}
resource "aws_lb" "waggledance" {
count = var.wd_instance_type == "ecs" && var.enable_autoscaling ? 1 : 0
name = local.instance_alias
internal = true
load_balancer_type = "network"
subnets = var.subnets
tags = var.tags
}
resource "aws_lb_target_group" "waggledance" {
count = var.wd_instance_type == "ecs" && var.enable_autoscaling ? 1 : 0
port = local.wd_port
protocol = "TCP"
target_type = "ip"
vpc_id = var.vpc_id
tags = var.tags
health_check {
protocol = "HTTP"
port = 18000
path = "/actuator/health"
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_lb_listener" "waggledance" {
count = var.wd_instance_type == "ecs" && var.enable_autoscaling ? 1 : 0
load_balancer_arn = aws_lb.waggledance[0].arn
protocol = "TCP"
port = local.wd_port
default_action {
target_group_arn = aws_lb_target_group.waggledance[0].arn
type = "forward"
}
}