-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecs-service.tf
217 lines (181 loc) · 5.52 KB
/
ecs-service.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
resource "aws_security_group" "loadbalancer_sg" {
vpc_id = module.main-vpc.vpc_id
name = local.loadbalancer_sg_name
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Project = var.project
Environment = var.stage
}
}
#
# ALB resources
#
resource "aws_alb" "loadbalancer" {
security_groups = [aws_security_group.loadbalancer_sg.id]
subnets = module.main-vpc.public_subnets
name = local.loadbalancer_name
tags = {
Name = local.loadbalancer_name
Project = var.project
Environment = var.stage
}
}
resource "aws_alb_target_group" "loadbalancer" {
name = local.target_group_name
health_check {
healthy_threshold = "3"
interval = "30"
protocol = var.abl_protocol
matcher = "200"
timeout = "3"
path = var.health_check_path
unhealthy_threshold = "2"
}
port = var.container_port
protocol = var.abl_protocol
vpc_id = module.main-vpc.vpc_id
tags = {
Name = local.target_group_name
Project = var.project
Environment = var.stage
}
}
resource "aws_alb_listener" "https" {
load_balancer_arn = aws_alb.loadbalancer.id
port = var.container_port
protocol = var.abl_protocol
# certificate_arn = var.ssl_certificate_arn
default_action {
target_group_arn = aws_alb_target_group.loadbalancer.id
type = "forward"
}
}
data "template_file" "task-definitions" {
template = file("${path.module}/task-definitions/service.json")
vars = {
ecs_image_name = var.ecr_image_name
image_tag = "latest"
name = var.project
port = var.container_port
region = var.region
}
}
resource "aws_ecs_task_definition" "ecs_task" {
family = local.task_name
container_definitions = data.template_file.task-definitions.rendered
requires_compatibilities = [var.service_launch_type]
}
resource "aws_ecs_service" "ecs_service" {
lifecycle {
create_before_destroy = true
}
name = local.service_name
cluster = aws_ecs_cluster.container_instance.id
task_definition = aws_ecs_task_definition.ecs_task.arn
desired_count = var.desired_capacity
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
load_balancer {
target_group_arn = aws_alb_target_group.loadbalancer.id
container_name = var.project
container_port = var.container_port
}
depends_on = [
aws_alb.loadbalancer,
aws_alb_target_group.loadbalancer
]
}
#
# CloudWatch Matric resources
#
resource "aws_cloudwatch_metric_alarm" "cloudwatch_metric_high" {
alarm_name = local.alarm_name_hight
alarm_actions = [aws_appautoscaling_policy.up.arn]
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "RequestCountPerTarget"
namespace = "AWS/ApplicationELB"
period = "300"
statistic = "Sum"
threshold = "100000"
dimensions = {
LoadBalancer = aws_alb.loadbalancer.id
TargetGroup = aws_alb_target_group.loadbalancer.arn_suffix
}
}
resource "aws_cloudwatch_metric_alarm" "cloudwatch_metric_low" {
alarm_name = local.alarm_name_low
alarm_actions = [aws_appautoscaling_policy.down.arn]
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "RequestCountPerTarget"
namespace = "AWS/ApplicationELB"
period = "300"
statistic = "Sum"
threshold = "0"
dimensions = {
LoadBalancer = aws_alb.loadbalancer.id
TargetGroup = aws_alb_target_group.loadbalancer.arn_suffix
}
}
#
# Application AutoScaling resources
#
resource "aws_appautoscaling_target" "main" {
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.container_instance.name}/${aws_ecs_service.ecs_service.name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = var.min_capacity
max_capacity = var.max_capacity
depends_on = [
aws_ecs_service.ecs_service,
]
}
resource "aws_appautoscaling_policy" "up" {
name = local.autoscaling_policy_up
resource_id = "service/${aws_ecs_cluster.container_instance.name}/${aws_ecs_service.ecs_service.name}"
service_namespace = "ecs"
scalable_dimension = "ecs:service:DesiredCount"
policy_type = "StepScaling"
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = "300"
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_lower_bound = 5
scaling_adjustment = 1
}
}
depends_on = [
aws_appautoscaling_target.main,
]
}
resource "aws_appautoscaling_policy" "down" {
name = local.autoscaling_policy_down
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.container_instance.name}/${aws_ecs_service.ecs_service.name}"
scalable_dimension = "ecs:service:DesiredCount"
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = "300"
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
}
depends_on = [
aws_appautoscaling_target.main,
]
}