forked from larribas/terraform-aws-mlflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.tf
307 lines (276 loc) · 8.7 KB
/
server.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
data "aws_region" "current" {}
resource "aws_iam_role" "ecs_task" {
name = "${var.unique_name}-ecs-task"
tags = local.tags
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
Effect = "Allow"
},
]
})
inline_policy {
name = "ssm-execute-command"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
],
"Resource" : "*"
}
]
})
}
dynamic "inline_policy" {
for_each = local.create_dedicated_bucket ? ["1"] : []
content {
name = "access_to_default_buckets"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"s3:ListBucket",
"s3:GetBucketLocation",
]
Resource = concat(
aws_s3_bucket.default.*.arn,
var.artifact_buckets_mlflow_will_read,
)
},
{
Effect = "Allow"
Action = [
"s3:GetObjectVersionTagging",
"s3:PutObjectVersionTagging",
"s3:ListMultipartUploadParts",
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject",
"s3:GetObjectAcl",
"s3:AbortMultipartUpload",
"s3:GetObjectTagging",
"s3:PutObjectTagging",
"s3:GetObjectVersionAcl",
"s3:GetObjectVersion",
]
Resource = [
for bucket in concat(aws_s3_bucket.default.*.arn, var.artifact_buckets_mlflow_will_read) :
"${bucket}/*"
]
},
]
})
}
}
}
resource "aws_iam_role" "ecs_execution" {
name = "${var.unique_name}-ecs-execution"
tags = local.tags
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
Effect = "Allow"
},
]
})
}
resource "aws_iam_role_policy_attachment" "ecs_execution" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
role = aws_iam_role.ecs_execution.name
}
resource "aws_security_group" "ecs_service" {
name = "${var.unique_name}-ecs-service"
tags = local.tags
vpc_id = var.vpc_id
ingress {
from_port = local.service_port
to_port = local.service_port
protocol = "tcp"
security_groups = [aws_security_group.lb.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_cloudwatch_log_group" "mlflow" {
name = "/aws/ecs/${var.unique_name}"
retention_in_days = var.service_log_retention_in_days
tags = local.tags
}
resource "aws_ecs_cluster" "mlflow" {
name = var.unique_name
tags = local.tags
}
resource "aws_ecs_task_definition" "mlflow" {
family = var.unique_name
tags = local.tags
container_definitions = jsonencode(concat([
{
name = "mlflow"
image = "${var.service_image}:${var.service_image_tag}"
essential = true
# As of version 1.9.1, MLflow doesn't support specifying the backend store uri as an environment variable. ECS doesn't allow evaluating secret environment variables from within the command. Therefore, we are forced to override the entrypoint and assume the docker image has a shell we can use to interpolate the secret at runtime.
entryPoint = ["sh", "-c"]
command = [
"/bin/sh -c \"${var.mlflow_command} server --backend-store-uri=mysql+pymysql://${aws_rds_cluster.backend_store.master_username}:`echo -n $DB_PASSWORD`@${aws_rds_cluster.backend_store.endpoint}:${aws_rds_cluster.backend_store.port}/${aws_rds_cluster.backend_store.database_name}\""
]
portMappings = [{ containerPort = local.service_port }]
environment = concat(
[
{
name = "MLFLOW_HOST"
value = "0.0.0.0"
},
{
name = "MLFLOW_PORT"
value = tostring(local.service_port)
},
{
name = "MLFLOW_GUNICORN_OPTS"
value = var.gunicorn_opts
},
],
var.proxy_artifact_storage_requests ? [
{
name = "MLFLOW_ARTIFACTS_DESTINATION"
value = "s3://${local.artifact_bucket_id}${var.artifact_bucket_path}"
},
{
name = "MLFLOW_SERVE_ARTIFACTS"
value = "True"
},
] : [
{
name = "MLFLOW_DEFAULT_ARTIFACT_ROOT"
value = "s3://${local.artifact_bucket_id}${var.artifact_bucket_path}"
},
]
)
secrets = [
{
name = "DB_PASSWORD"
valueFrom = data.aws_secretsmanager_secret.db_password.arn
},
]
logConfiguration = {
logDriver = "awslogs"
secretOptions = null
options = {
"awslogs-group" = aws_cloudwatch_log_group.mlflow.name
"awslogs-region" = data.aws_region.current.name
"awslogs-stream-prefix" = "cis"
}
}
},
], var.service_sidecar_container_definitions))
network_mode = "awsvpc"
task_role_arn = aws_iam_role.ecs_task.arn
execution_role_arn = aws_iam_role.ecs_execution.arn
requires_compatibilities = ["FARGATE"]
cpu = var.service_cpu
memory = var.service_memory
}
resource "aws_ecs_service" "mlflow" {
name = var.unique_name
cluster = aws_ecs_cluster.mlflow.id
task_definition = aws_ecs_task_definition.mlflow.arn
desired_count = 2
launch_type = "FARGATE"
platform_version = "1.4.0"
enable_execute_command = var.enable_execute_command
network_configuration {
subnets = var.service_subnet_ids
security_groups = [aws_security_group.ecs_service.id]
}
load_balancer {
target_group_arn = aws_lb_target_group.mlflow.arn
container_name = "mlflow"
container_port = local.service_port
}
lifecycle {
ignore_changes = [desired_count]
}
depends_on = [
aws_lb.mlflow,
]
}
resource "aws_appautoscaling_target" "mlflow" {
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.mlflow.name}/${aws_ecs_service.mlflow.name}"
scalable_dimension = "ecs:service:DesiredCount"
max_capacity = var.service_max_capacity
min_capacity = var.service_min_capacity
}
resource "aws_security_group" "lb" {
name = "${var.unique_name}-lb"
tags = local.tags
vpc_id = var.vpc_id
}
resource "aws_security_group_rule" "lb_ingress_http" {
description = "Only allow load balancer to reach the ECS service on the right port"
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = var.load_balancer_ingress_cidr_blocks
security_group_id = aws_security_group.lb.id
}
resource "aws_security_group_rule" "lb_ingress_https" {
description = "Only allow load balancer to reach the ECS service on the right port"
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.load_balancer_ingress_cidr_blocks
security_group_id = aws_security_group.lb.id
}
resource "aws_security_group_rule" "lb_egress" {
description = "Only allow load balancer to reach the ECS service on the right port"
type = "egress"
from_port = local.service_port
to_port = local.service_port
protocol = "tcp"
source_security_group_id = aws_security_group.ecs_service.id
security_group_id = aws_security_group.lb.id
}
resource "aws_lb" "mlflow" {
name = var.unique_name
tags = local.tags
internal = var.load_balancer_is_internal ? true : false
load_balancer_type = "application"
security_groups = [aws_security_group.lb.id]
subnets = var.load_balancer_subnet_ids
}
resource "aws_lb_target_group" "mlflow" {
name = var.unique_name
port = local.service_port
protocol = "HTTP"
vpc_id = var.vpc_id
target_type = "ip"
health_check {
protocol = "HTTP"
matcher = "200-202"
path = "/health"
}
}