Skip to content

Commit bcfe9ae

Browse files
authored
Fix S3 creation and access from MLflow (#68)
* fix s3 impl for mlflow * allow mlflow sa to access s3 * fmt files
1 parent 32e2dc2 commit bcfe9ae

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

aws-modular/eks.tf

+13
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ resource "aws_iam_role" "ng" {
9595
Principal = {
9696
Service = "ec2.amazonaws.com"
9797
}
98+
},
99+
{
100+
Action = "sts:AssumeRoleWithWebIdentity"
101+
Effect = "Allow"
102+
Principal = {
103+
Federated = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/${aws_eks_cluster.cluster[0].identity[0].oidc[0].issuer}"
104+
}
105+
Condition = {
106+
StringLike = {
107+
"${aws_eks_cluster.cluster[0].identity[0].oidc[0].issuer}:aud" = "sts.amazonaws.com"
108+
"${aws_eks_cluster.cluster[0].identity[0].oidc[0].issuer}:sub" = "system:serviceaccount:mlflow:*"
109+
}
110+
}
98111
}]
99112
Version = "2012-10-17"
100113
})

aws-modular/mlflow.tf

+41-8
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,50 @@ resource "aws_s3_bucket" "mlflow-bucket" {
4040
tags = local.tags
4141
}
4242

43-
resource "aws_s3_bucket_acl" "mlflow" {
44-
count = length(aws_s3_bucket.mlflow-bucket) > 0 ? 1 : 0
43+
# block public access to the bucket
44+
resource "aws_s3_bucket_public_access_block" "mlflow" {
45+
count = (var.enable_experiment_tracker_mlflow && var.mlflow_bucket == "") ? 1 : 0
4546
bucket = aws_s3_bucket.mlflow-bucket[0].id
46-
acl = "private"
47+
48+
block_public_acls = true
49+
block_public_policy = true
50+
ignore_public_acls = false
51+
restrict_public_buckets = false
4752
}
4853

49-
# block public access to the bucket
50-
resource "aws_s3_bucket_public_access_block" "mlflow" {
51-
count = length(aws_s3_bucket.mlflow-bucket) > 0 ? 1 : 0
54+
resource "aws_s3_bucket_policy" "allow_access_from_another_account_mlflow" {
55+
count = (var.enable_experiment_tracker_mlflow && var.mlflow_bucket == "") ? 1 : 0
5256
bucket = aws_s3_bucket.mlflow-bucket[0].id
57+
policy = data.aws_iam_policy_document.allow_access_from_another_account_mlflow[0].json
58+
}
59+
60+
data "aws_iam_policy_document" "allow_access_from_another_account_mlflow" {
61+
count = (var.enable_experiment_tracker_mlflow && var.mlflow_bucket == "") ? 1 : 0
62+
statement {
63+
principals {
64+
type = "AWS"
65+
identifiers = [aws_iam_role.ng[0].arn]
66+
}
67+
actions = [
68+
"s3:*",
69+
]
70+
resources = [
71+
aws_s3_bucket.mlflow-bucket[0].arn,
72+
"${aws_s3_bucket.mlflow-bucket[0].arn}/*",
73+
]
74+
}
75+
}
76+
77+
# allow the mlflow kubernetes SA to assume the IAM role
78+
resource "null_resource" "mlflow-iam-access" {
79+
80+
count = var.enable_experiment_tracker_mlflow ? 1 : 0
5381

54-
block_public_acls = true
55-
block_public_policy = true
82+
provisioner "local-exec" {
83+
command = "kubectl annotate serviceaccount -n mlflow mlflow-tracking eks.amazonaws.com/role-arn=${aws_iam_role.ng[0].arn}"
84+
}
85+
86+
depends_on = [
87+
module.mlflow,
88+
]
5689
}

0 commit comments

Comments
 (0)