This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ecs.tf
126 lines (109 loc) · 3.43 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
# ===========================
# CLUSTER
# ===========================
resource "aws_ecs_cluster" "cluster" {
name = "${var.app}-${var.env}"
}
#=========================
# TASK DEFINITION
#=========================
resource "aws_ecs_task_definition" "task" {
family = "${var.app}-${var.env}"
container_definitions = jsonencode([
module.ui_container.json_map_object,
module.api_container.json_map_object,
module.tasks_container.json_map_object,
])
cpu = var.cpu
execution_role_arn = aws_iam_role.ecs_execution.arn
memory = var.memory
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
task_role_arn = aws_iam_role.ecs_task.arn
}
# ===========================
# SECURITY GROUP
# ===========================
resource "aws_security_group" "service" {
name = "${var.app}-${var.env}-service"
description = "Allow traffic to service from ALB"
vpc_id = var.vpc_id
ingress {
description = "Allow container port from ALB"
from_port = local.api_container_port
to_port = local.api_container_port
protocol = "tcp"
security_groups = [aws_security_group.alb.id]
self = true
}
ingress {
description = "Allow container port from ALB"
from_port = local.ui_container_port
to_port = local.ui_container_port
protocol = "tcp"
security_groups = [aws_security_group.alb.id]
self = true
}
ingress {
description = "Allow container port from ALB"
from_port = local.tasks_container_port
to_port = local.tasks_container_port
protocol = "tcp"
security_groups = [aws_security_group.alb.id]
self = true
}
ingress {
description = "Allow traffic to containers"
from_port = local.landing_container_port
to_port = local.landing_container_port
protocol = "tcp"
security_groups = [aws_security_group.alb.id]
self = true
}
egress {
description = "Allow outbound traffic"
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
}
#=========================
# SERVICE
#=========================
resource "aws_ecs_service" "service" {
name = "${var.app}-${var.env}"
cluster = aws_ecs_cluster.cluster.id
task_definition = aws_ecs_task_definition.task.arn
desired_count = var.desired_count
force_new_deployment = true
launch_type = "FARGATE"
load_balancer {
target_group_arn = aws_lb_target_group.api.arn
container_name = local.api_container_name
container_port = local.api_container_port
}
load_balancer {
target_group_arn = aws_lb_target_group.tasks.arn
container_name = local.tasks_container_name
container_port = local.tasks_container_port
}
load_balancer {
target_group_arn = aws_lb_target_group.ui.arn
container_name = local.ui_container_name
container_port = local.ui_container_port
}
load_balancer {
target_group_arn = aws_lb_target_group.landing.arn
container_name = local.api_container_name
container_port = local.landing_container_port
}
network_configuration {
subnets = var.private_subnet_ids
security_groups = [aws_security_group.service.id]
assign_public_ip = false
}
lifecycle {
ignore_changes = [desired_count]
}
}