-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs.tf
148 lines (137 loc) · 4 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
resource "aws_ecs_cluster" "studyoracle" {
name = "studyoracle"
}
# feed the credentials to the docker provider
# container definition
resource "aws_ecs_task_definition" "studyoracle" {
family = "studyoracle"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = 512
memory = 2048
execution_role_arn = data.aws_iam_role.lab.arn
container_definitions = <<DEFINITION
[
{
"image": "${local.server_image}",
"cpu": 256,
"memory": 1024,
"name": "studyoracle_server",
"networkMode": "awsvpc",
"portMappings": [
{
"containerPort": 8080,
"hostPort": 8080
}
],
"environment": [
{
"name": "OPENAI_API_KEY",
"value": "${local.openai_api_key}"
},
{
"name": "SQLALCHEMY_DATABASE_URI",
"value": "postgresql://${local.database_username}:${local.database_password}@${aws_db_instance.database.address}:${aws_db_instance.database.port}/${aws_db_instance.database.db_name}"
},
{
"name": "CELERY_BROKER_URL",
"value": "redis://${local.redis_address}:${local.redis_port}"
},
{
"name": "CELERY_RESULT_BACKEND",
"value": "db+postgresql://${local.database_username}:${local.database_password}@${aws_db_instance.database.address}:${aws_db_instance.database.port}/${aws_db_instance.database.db_name}"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/studyoracle/server",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs",
"awslogs-create-group": "true"
}
}
},
{
"image": "${local.worker_image}",
"cpu": 256,
"memory": 1024,
"name": "studyoracle_worker",
"networkMode": "awsvpc",
"environment": [
{
"name": "OPENAI_API_KEY",
"value": "${local.openai_api_key}"
},
{
"name": "SQLALCHEMY_DATABASE_URI",
"value": "postgresql://${local.database_username}:${local.database_password}@${aws_db_instance.database.address}:${aws_db_instance.database.port}/${aws_db_instance.database.db_name}"
},
{
"name": "CELERY_BROKER_URL",
"value": "redis://${local.redis_address}:${local.redis_port}"
},
{
"name": "CELERY_RESULT_BACKEND",
"value": "db+postgresql://${local.database_username}:${local.database_password}@${aws_db_instance.database.address}:${aws_db_instance.database.port}/${aws_db_instance.database.db_name}"
},
{
"name": "C_FORCE_ROOT",
"value": "true"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/studyoracle/worker",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs",
"awslogs-create-group": "true"
}
}
}
]
DEFINITION
}
# scaling
resource "aws_ecs_service" "studyoracle" {
name = "studyoracle"
cluster = aws_ecs_cluster.studyoracle.id
task_definition = aws_ecs_task_definition.studyoracle.arn
desired_count = 1
launch_type = "FARGATE"
network_configuration {
subnets = data.aws_subnets.private.ids
security_groups = [aws_security_group.studyoracle.id]
assign_public_ip = true
}
# Connect the internal side of the load balancer
# to the auto-scaling service
load_balancer {
target_group_arn = aws_lb_target_group.studyoracle_server.arn
container_name = "studyoracle_server"
container_port = 8080
}
}
resource "aws_security_group" "studyoracle" {
name = "studyoracle"
description = "studyoracle Security Group"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}