This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ecs.tf
166 lines (145 loc) · 4.02 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
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
resource "aws_security_group" "ecs_task" {
name = "${local.name}-task-sg"
vpc_id = aws_vpc.this.id
ingress {
protocol = "tcp"
from_port = var.port
to_port = var.port
security_groups = [aws_security_group.lb.id]
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = local.tags
}
resource "aws_ecs_cluster" "this" {
name = local.name
tags = local.tags
}
resource "aws_ecs_task_definition" "run" {
family = "run"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.task_cpu
memory = var.task_memory
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
container_definitions = jsonencode([
{
name = local.name
image = var.openfga_container_image
command = ["run"]
networkMode = "awsvpc"
essential = true
portMappings = [
{
containerPort = var.port
hostPort = var.port
}
],
environment = [
{
name = "OPENFGA_PLAYGROUND_ENABLED"
value = "false"
},
{
name = "OPENFGA_LOG_FORMAT"
value = "json"
},
{
name = "OPENFGA_DATASTORE_ENGINE"
value = var.db_type
},
{
name = "OPENFGA_DATASTORE_URI"
value = local.db_conn_string
}
],
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = aws_cloudwatch_log_group.this.id
awslogs-region = var.region
awslogs-stream-prefix = "ecs"
}
},
}
])
tags = local.tags
}
resource "aws_ecs_service" "run" {
name = "${local.name}-run"
cluster = aws_ecs_cluster.this.id
task_definition = aws_ecs_task_definition.run.arn
launch_type = "FARGATE"
scheduling_strategy = "REPLICA"
desired_count = var.service_count
network_configuration {
subnets = aws_subnet.public.*.id
assign_public_ip = true # needed to pull from docker hub
security_groups = [aws_security_group.ecs_task.id]
}
load_balancer {
target_group_arn = aws_lb_target_group.this.arn
container_name = local.name
container_port = var.port
}
depends_on = [
aws_lb_listener.this,
aws_rds_cluster_instance.this,
aws_iam_role.ecs_task_execution_role,
aws_ecs_service.migrate,
]
tags = local.tags
}
resource "aws_ecs_task_definition" "migrate" {
count = var.migrate ? 1 : 0
family = "migrate"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.task_cpu
memory = var.task_memory
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
container_definitions = jsonencode([
{
name = "${local.name}-migrate"
image = var.openfga_container_image
command = ["migrate"]
networkMode = "awsvpc"
essential = true
environment = [
{
name = "OPENFGA_DATASTORE_ENGINE"
value = var.db_type
},
{
name = "OPENFGA_DATASTORE_URI"
value = local.db_conn_string
}
],
}
])
tags = local.tags
}
resource "aws_ecs_service" "migrate" {
count = var.migrate ? 1 : 0
name = "${local.name}-migrate"
cluster = aws_ecs_cluster.this.id
task_definition = aws_ecs_task_definition.migrate[0].arn
launch_type = "FARGATE"
scheduling_strategy = "REPLICA"
desired_count = 1
network_configuration {
subnets = aws_subnet.public.*.id
assign_public_ip = true # needed to pull from docker hub
security_groups = [aws_security_group.ecs_task.id]
}
depends_on = [
aws_lb_listener.this,
aws_rds_cluster_instance.this,
aws_iam_role.ecs_task_execution_role,
]
tags = local.tags
}