Skip to content

Commit 9c8ae59

Browse files
committed
Provide conditional S3 permissions policy to EKS worker node group role
1 parent 9695ea9 commit 9c8ae59

File tree

5 files changed

+23
-30
lines changed

5 files changed

+23
-30
lines changed

main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
data "aws_availability_zones" "available" {}
22

3-
43
data "aws_eks_cluster_auth" "this" {
54
count = var.enable_eks ? 1 : 0
65
name = module.comet_eks[0].cluster_name
76
}
87

9-
108
locals {
119
resource_name = "comet-${var.environment}"
1210
vpc_cidr = "10.0.0.0/16"
@@ -60,7 +58,7 @@ module "comet_ec2" {
6058

6159
s3_enabled = var.enable_s3
6260
comet_ml_s3_bucket = var.s3_bucket_name
63-
comet_ec2_s3_iam_policy = module.comet_s3[0].comet_s3_iam_policy_arn
61+
comet_ec2_s3_iam_policy = var.enable_s3 ? module.comet_s3[0].comet_s3_iam_policy_arn : null
6462
}
6563

6664
module "comet_eks" {
@@ -71,6 +69,9 @@ module "comet_eks" {
7169
vpc_private_subnets = module.vpc.private_subnets
7270
cluster_name = var.eks_cluster_name
7371
cluster_version = var.eks_cluster_version
72+
73+
s3_enabled = var.enable_s3
74+
comet_ec2_s3_iam_policy = var.enable_s3 ? module.comet_s3[0].comet_s3_iam_policy_arn : null
7475
}
7576

7677
module "comet_elasticache" {

modules/comet_ec2/main.tf

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ resource "aws_vpc_security_group_ingress_rule" "comet_ec2_ingress_ssh" {
4747
from_port = local.ssh_port
4848
to_port = local.ssh_port
4949
ip_protocol = "tcp"
50+
# make more restrictive
5051
cidr_ipv4 = local.cidr_anywhere
5152
}
5253

@@ -56,8 +57,7 @@ resource "aws_vpc_security_group_ingress_rule" "comet_ec2_ingress_http" {
5657
from_port = local.http_port
5758
to_port = local.http_port
5859
ip_protocol = "tcp"
59-
# We recommend restricting that to your company IP or by Using a bastion host
60-
#security_groups = [aws_security_group.bastion_inbound_sg.id]
60+
# make more restrictive
6161
cidr_ipv4 = local.cidr_anywhere
6262
}
6363

@@ -100,30 +100,8 @@ resource "aws_iam_instance_profile" "comet-ec2-instance-profile" {
100100
role = aws_iam_role.comet-ec2-s3-access-role.name
101101
}
102102

103-
/*
104-
resource "aws_iam_policy" "comet-ml-s3-policy" {
105-
count = var.s3_enabled ? 1 : 0
106-
name = "comet-s3-access-policy"
107-
description = "comet-s3-access-policy"
108-
policy = jsonencode({
109-
"Version": "2012-10-17",
110-
"Statement": [
111-
{
112-
"Effect": "Allow",
113-
"Action": "s3:*",
114-
"Resource": [
115-
"arn:aws:s3:::${var.comet_ml_s3_bucket}",
116-
"arn:aws:s3:::${var.comet_ml_s3_bucket}/*"
117-
]
118-
}
119-
]
120-
})
121-
}
122-
*/
123-
124103
resource "aws_iam_role_policy_attachment" "comet-ml-s3-access-attachment" {
125104
count = var.s3_enabled ? 1 : 0
126105
role = aws_iam_role.comet-ec2-s3-access-role.name
127-
#policy_arn = aws_iam_policy.comet-ml-s3-policy[0].arn
128106
policy_arn = var.comet_ec2_s3_iam_policy
129107
}

modules/comet_eks/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ module "eks" {
5252
min_size = 3
5353
max_size = 6
5454
desired_size = 3
55+
56+
iam_role_additional_policies = var.s3_enabled ? {comet_s3_access = var.comet_ec2_s3_iam_policy} : null
5557
}
5658
}
5759

modules/comet_eks/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,16 @@ variable "vpc_private_subnets" {
2323
description = "IDs of private subnets within the VPC"
2424
type = list(string)
2525
default = []
26+
}
27+
28+
variable "s3_enabled" {
29+
description = "Indicates if S3 bucket is being provisioned for Comet"
30+
type = bool
31+
default = null
32+
}
33+
34+
variable "comet_ec2_s3_iam_policy" {
35+
description = "Policy with access to S3 to associate with EKS worker nodes"
36+
type = string
37+
default = null
2638
}

terraform.tfvars

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
enable_ec2 = true
1+
enable_ec2 = false
22

3-
enable_eks = false
3+
enable_eks = true
44

55
enable_elasticache = true
66

77
enable_rds = false
88

99
enable_s3 = true
1010

11-
s3_bucket_name = "cometeoteoitheoihiahg"
11+
s3_bucket_name = "comet-use2-from-eks"

0 commit comments

Comments
 (0)