forked from h2ouw8n4/wordpressOnAWS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fargate.tf
372 lines (328 loc) · 9.21 KB
/
fargate.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
# Ref - https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html
resource "aws_iam_role" "task_execution_role" {
name = "${var.prefix}-task-execution-role-${var.environment}"
tags = var.tags
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"ecs-tasks.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_policy" "task_execution_policy" {
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogStream",
"logs:PutLogEvents",
"ssm:GetParameters",
"kms:Decrypt"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "task_execution_policy_attach" {
role = aws_iam_role.task_execution_role.name
policy_arn = aws_iam_policy.task_execution_policy.arn
}
resource "aws_iam_role" "task_role" {
name = "${var.prefix}-task-role-${var.environment}"
tags = var.tags
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"ecs-tasks.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_policy" "task_policy" {
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"elasticfilesystem:ClientMount",
"elasticfilesystem:ClientWrite",
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "task_policy_attach" {
role = aws_iam_role.task_role.name
policy_arn = aws_iam_policy.task_policy.arn
}
resource "aws_ecs_cluster" "this" {
name = "${var.prefix}-${var.environment}"
}
resource "aws_ecs_cluster_capacity_providers" "cluster" {
cluster_name = "${var.prefix}-${var.environment}"
capacity_providers = ["FARGATE_SPOT"]
default_capacity_provider_strategy {
base = 0
weight = 10
capacity_provider = "FARGATE_SPOT"
}
}
resource "aws_security_group" "wordpress" {
name = "${var.prefix}-wordpress-${var.environment}"
description = "Fargate wordpress"
vpc_id = module.vpc.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 0
protocol = "tcp"
security_groups = [
aws_security_group.alb.id,
aws_security_group.efs.id,
aws_security_group.elasticache.id
]
}
tags = var.tags
}
resource "aws_ecs_service" "this" {
name = "${var.prefix}-${var.environment}"
cluster = aws_ecs_cluster.this.id
task_definition = aws_ecs_task_definition.this.arn
desired_count = var.desired_count
enable_execute_command = true
network_configuration {
security_groups = [
aws_security_group.alb.id,
aws_security_group.db.id,
aws_security_group.efs.id,
aws_security_group.elasticache.id
]
subnets = module.vpc.private_subnets
}
load_balancer {
target_group_arn = aws_lb_target_group.this.id
container_name = "wordpress"
container_port = 80
}
lifecycle {
ignore_changes = [
task_definition,
desired_count,
capacity_provider_strategy
]
}
}
resource "aws_ecs_task_definition" "this" {
family = "${var.prefix}-${var.environment}"
execution_role_arn = aws_iam_role.task_execution_role.arn
task_role_arn = aws_iam_role.task_role.arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.task_cpu
memory = var.task_memory
runtime_platform {
cpu_architecture = "ARM64" # Indicate Graviton2 processor architecture
operating_system_family = "LINUX"
}
container_definitions = <<CONTAINER_DEFINITION
[
{
"secrets": [
{
"name": "WORDPRESS_DB_USER",
"valueFROM": "${aws_ssm_parameter.db_master_user.arn}"
},
{
"name": "WORDPRESS_DB_PASSWORD",
"valueFROM": "${aws_ssm_parameter.db_master_password.arn}"
}
],
"environment": [
{
"name": "WORDPRESS_DB_HOST",
"value": "${aws_rds_cluster.this.endpoint}"
},
{
"name": "WORDPRESS_DB_NAME",
"value": "wordpress"
},
{
"name": "WORDPRESS_CONFIG_EXTRA",
"value": ""
},
{
"name": "WP_MEMCACHED_ENDPOINT",
"value": "${aws_elasticache_cluster.this.configuration_endpoint}"
}
],
"essential": true,
"image": "828936781852.dkr.ecr.ap-northeast-1.amazonaws.com/wordpress:latest",
"name": "wordpress",
"portMappings": [
{
"containerPort": 80
}
],
"mountPoints": [
{
"containerPath": "/var/www/html",
"sourceVolume": "efs"
}
],
"logConfiguration": {
"logDriver":"awslogs",
"options": {
"awslogs-group": "${aws_cloudwatch_log_group.wordpress.name}",
"awslogs-region": "${data.aws_region.current.name}",
"awslogs-stream-prefix": "app"
}
}
}
]
CONTAINER_DEFINITION
volume {
name = "efs"
efs_volume_configuration {
file_system_id = aws_efs_file_system.this.id
}
}
}
resource "aws_cloudwatch_log_group" "wordpress" {
name = "/${var.prefix}/${var.environment}/fg-task"
tags = var.tags
retention_in_days = var.log_retention_in_days
}
resource "aws_lb_target_group" "this" {
name = "${var.prefix}-${var.environment}"
port = 80
protocol = "HTTP"
target_type = "ip"
vpc_id = module.vpc.vpc_id
health_check {
path = "/"
matcher = "200,301,302"
}
}
resource "aws_lb_listener_rule" "wordpress" {
listener_arn = module.alb.https_listener_arns[0]
priority = 100
action {
type = "forward"
target_group_arn = aws_lb_target_group.this.arn
}
condition {
host_header {
values = [var.site_domain]
}
}
}
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_high" {
alarm_name = "${var.prefix}-high-CPU-utilization-ecs-${var.environment}"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = var.task_cpu_high_threshold
dimensions = {
ClusterName = aws_ecs_cluster.this.name
ServiceName = aws_ecs_service.this.name
}
alarm_actions = [aws_appautoscaling_policy.scale_up.arn]
}
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_low" {
alarm_name = "${var.prefix}-low-CPU-utilization-ecs-${var.environment}"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = var.task_cpu_low_threshold
dimensions = {
ClusterName = aws_ecs_cluster.this.name
ServiceName = aws_ecs_service.this.name
}
alarm_actions = [aws_appautoscaling_policy.scale_down.arn]
}
resource "aws_appautoscaling_target" "this" {
max_capacity = var.max_task
min_capacity = var.min_task
resource_id = "service/${aws_ecs_cluster.this.name}/${aws_ecs_service.this.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_appautoscaling_policy" "scale_up" {
name = "${var.prefix}-ecs-scale-up-${var.environment}"
policy_type = "StepScaling"
resource_id = aws_appautoscaling_target.this.resource_id
scalable_dimension = aws_appautoscaling_target.this.scalable_dimension
service_namespace = aws_appautoscaling_target.this.service_namespace
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = var.scaling_up_cooldown
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = var.scaling_up_adjustment
}
}
}
resource "aws_appautoscaling_policy" "scale_down" {
name = "${var.prefix}-ecs-scale-down-${var.environment}"
policy_type = "StepScaling"
resource_id = aws_appautoscaling_target.this.resource_id
scalable_dimension = aws_appautoscaling_target.this.scalable_dimension
service_namespace = aws_appautoscaling_target.this.service_namespace
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = var.scaling_down_cooldown
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = var.scaling_down_adjustment
}
}
}