Skip to content

Commit f63b4eb

Browse files
committed
Set nullable inputs for k8s and helm providers and set conditional provisioning of s3 permissions for ec2 instance
1 parent 2608d8f commit f63b4eb

File tree

9 files changed

+1213
-604
lines changed

9 files changed

+1213
-604
lines changed

.terraform.lock.hcl

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.tf

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

3-
/*
3+
44
data "aws_eks_cluster_auth" "this" {
5+
count = var.enable_eks ? 1 : 0
56
name = module.comet_eks[0].cluster_name
67
}
7-
*/
8+
89

910
locals {
1011
resource_name = "comet-${var.environment}"
@@ -52,10 +53,12 @@ module "vpc" {
5253
module "comet_ec2" {
5354
source = "./modules/comet_ec2"
5455
count = var.enable_ec2 ? 1 : 0
56+
57+
s3_enabled = var.enable_s3
5558

5659
vpc_id = module.vpc.vpc_id
57-
allinone_ami = "ami-05842f1afbf311a43"
58-
allinone_subnet = module.vpc.public_subnets[count.index % length(module.vpc.public_subnets)]
60+
comet_ec2_ami = "ami-05842f1afbf311a43"
61+
comet_ec2_subnet = module.vpc.public_subnets[count.index % length(module.vpc.public_subnets)]
5962

6063
comet_ml_s3_bucket = var.s3_bucket_name
6164
}
@@ -81,7 +84,7 @@ module "comet_elasticache" {
8184
vpc_private_subnets = module.vpc.private_subnets
8285

8386
# index is used on the module refs becuase of the count usage in the toggle: "After the count apply the resource becomes a group, so later in the reference use 0-index of the group"
84-
elasticache_allow_ec2_sg = var.enable_ec2 ? module.comet_ec2[0].allinone_sg_id : null
87+
elasticache_allow_ec2_sg = var.enable_ec2 ? module.comet_ec2[0].comet_ec2_sg_id : null
8588
elasticache_allow_eks_sg = var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : null
8689
}
8790

@@ -97,7 +100,7 @@ module "comet_rds" {
97100
vpc_private_subnets = module.vpc.private_subnets
98101

99102
# index is used on the module refs becuase of the count usage in the toggle: "After the count apply the resource becomes a group, so later in the reference use 0-index of the group"
100-
rds_allow_ec2_sg = var.enable_ec2 ? module.comet_ec2[0].allinone_sg_id : null
103+
rds_allow_ec2_sg = var.enable_ec2 ? module.comet_ec2[0].comet_ec2_sg_id : null
101104
rds_allow_eks_sg = var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : null
102105

103106
}

modules/comet_ec2/main.tf

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@ locals {
1010
}
1111
}
1212

13-
resource "aws_instance" "allinone" {
14-
ami = var.allinone_ami
15-
instance_type = var.allinone_instance_type
13+
resource "aws_instance" "comet_ec2" {
14+
ami = var.comet_ec2_ami
15+
instance_type = var.comet_ec2_instance_type
1616
key_name = var.key_name
17-
count = var.allinone_instance_count
18-
iam_instance_profile = aws_iam_instance_profile.comet-ml-s3-access-profile.name
19-
# Recommended place it in a private subnet along with a bastion host
20-
#subnet_id = module.vpc.private_subnets[count.index % length(module.vpc.private_subnets)]
21-
subnet_id = var.allinone_subnet
17+
count = var.comet_ec2_instance_count
18+
iam_instance_profile = aws_iam_instance_profile.comet-ec2-instance-profile.name
19+
subnet_id = var.comet_ec2_subnet
2220

2321
# need enable multiple SGs
24-
vpc_security_group_ids = [aws_security_group.allinone_sg.id]
22+
vpc_security_group_ids = [aws_security_group.comet_ec2_sg.id]
2523

2624
root_block_device {
27-
volume_type = var.allinone_volume_type
28-
volume_size = var.allinone_volume_size
25+
volume_type = var.comet_ec2_volume_type
26+
volume_size = var.comet_ec2_volume_size
2927
}
3028

3129
tags = merge(local.tags, {
@@ -37,23 +35,23 @@ resource "aws_instance" "allinone" {
3735
}
3836
}
3937

40-
resource "aws_security_group" "allinone_sg" {
38+
resource "aws_security_group" "comet_ec2_sg" {
4139
name = "comet_${var.environment}_ec2_sg"
4240
description = "Comet EC2 instance security group"
4341
vpc_id = var.vpc_id
4442
}
4543

46-
resource "aws_vpc_security_group_ingress_rule" "allinone_ingress_ssh" {
47-
security_group_id = aws_security_group.allinone_sg.id
44+
resource "aws_vpc_security_group_ingress_rule" "comet_ec2_ingress_ssh" {
45+
security_group_id = aws_security_group.comet_ec2_sg.id
4846

4947
from_port = local.ssh_port
5048
to_port = local.ssh_port
5149
ip_protocol = "tcp"
5250
cidr_ipv4 = local.cidr_anywhere
5351
}
5452

55-
resource "aws_vpc_security_group_ingress_rule" "allinone_ingress_http" {
56-
security_group_id = aws_security_group.allinone_sg.id
53+
resource "aws_vpc_security_group_ingress_rule" "comet_ec2_ingress_http" {
54+
security_group_id = aws_security_group.comet_ec2_sg.id
5755

5856
from_port = local.http_port
5957
to_port = local.http_port
@@ -64,8 +62,8 @@ resource "aws_vpc_security_group_ingress_rule" "allinone_ingress_http" {
6462
}
6563

6664
/* SG rule to allow ingress from LB SG; add later
67-
resource "aws_vpc_security_group_ingress_rule" "allinone_ingress_http" {
68-
security_group_id = aws_security_group.allinone_sg.id
65+
resource "aws_vpc_security_group_ingress_rule" "comet_ec2_ingress_http" {
66+
security_group_id = aws_security_group.comet_ec2_sg.id
6967
7068
from_port = local.http_port
7169
to_port = local.http_port
@@ -74,25 +72,38 @@ resource "aws_vpc_security_group_ingress_rule" "allinone_ingress_http" {
7472
}
7573
*/
7674

77-
resource "aws_vpc_security_group_egress_rule" "allinone_egress_any" {
78-
security_group_id = aws_security_group.allinone_sg.id
75+
resource "aws_vpc_security_group_egress_rule" "comet_ec2_egress_any" {
76+
security_group_id = aws_security_group.comet_ec2_sg.id
7977
ip_protocol = "-1"
8078
cidr_ipv4 = local.cidr_anywhere
8179
}
8280

83-
resource "aws_iam_role" "comet-ml-allinone-s3-access-role" {
81+
resource "aws_iam_role" "comet-ec2-s3-access-role" {
8482
name = "comet-ml-s3-role"
85-
assume_role_policy = file("${path.module}/templates/assume-role.json")
83+
assume_role_policy = jsonencode({
84+
"Version": "2012-10-17",
85+
"Statement": [
86+
{
87+
"Action": "sts:AssumeRole",
88+
"Principal": {
89+
"Service": "ec2.amazonaws.com"
90+
},
91+
"Effect": "Allow",
92+
"Sid": ""
93+
}
94+
]
95+
})
8696
}
8797

88-
resource "aws_iam_instance_profile" "comet-ml-s3-access-profile" {
89-
name = "${var.environment}-comet-ml-s3-access-profile"
90-
role = aws_iam_role.comet-ml-allinone-s3-access-role.name
98+
resource "aws_iam_instance_profile" "comet-ec2-instance-profile" {
99+
name = "${var.environment}-comet-ec2-instance-profile"
100+
role = aws_iam_role.comet-ec2-s3-access-role.name
91101
}
92102

93103
resource "aws_iam_policy" "comet-ml-s3-policy" {
94-
name = "comet-ml-s3-access-policy"
95-
description = "comet-ml-s3-access-policy"
104+
count = var.s3_enabled ? 1 : 0
105+
name = "comet-s3-access-policy"
106+
description = "comet-s3-access-policy"
96107
policy = jsonencode({
97108
"Version": "2012-10-17",
98109
"Statement": [
@@ -109,6 +120,7 @@ resource "aws_iam_policy" "comet-ml-s3-policy" {
109120
}
110121

111122
resource "aws_iam_role_policy_attachment" "comet-ml-s3-access-attachment" {
112-
role = aws_iam_role.comet-ml-allinone-s3-access-role.name
113-
policy_arn = aws_iam_policy.comet-ml-s3-policy.arn
123+
count = var.s3_enabled ? 1 : 0
124+
role = aws_iam_role.comet-ec2-s3-access-role.name
125+
policy_arn = aws_iam_policy.comet-ml-s3-policy[0].arn
114126
}

modules/comet_ec2/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
output "allinone_sg_id" {
2-
value = aws_security_group.allinone_sg.id
3-
description = "ID of the security group associated with the allinone instance"
1+
output "comet_ec2_sg_id" {
2+
value = aws_security_group.comet_ec2_sg.id
3+
description = "ID of the security group associated with the comet_ec2 instance"
44
}

modules/comet_ec2/variables.tf

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ variable "vpc_id" {
1010
#default = ""
1111
}
1212

13-
variable "allinone_ami" {
13+
variable "comet_ec2_ami" {
1414
description = "AMI for the EC2 instance"
1515
type = string
1616
default = ""
1717
}
1818

19-
variable "allinone_instance_type" {
19+
variable "comet_ec2_instance_type" {
2020
description = "Instance type for the EC2 instance"
2121
type = string
2222
default = "m5.4xlarge"
@@ -28,30 +28,36 @@ variable "key_name" {
2828
default = ""
2929
}
3030

31-
variable "allinone_instance_count" {
31+
variable "comet_ec2_instance_count" {
3232
description = "Number of EC2 instances to provision"
3333
type = number
3434
default = 1
3535
}
3636

37-
variable "allinone_volume_type" {
37+
variable "comet_ec2_volume_type" {
3838
description = "EBS volume type for the EC2 instance root volume"
3939
type = string
4040
default = "gp2"
4141
}
4242

43-
variable "allinone_volume_size" {
43+
variable "comet_ec2_volume_size" {
4444
description = "Size, in gibibytes (GiB), for the EC2 instance root volume"
4545
type = number
4646
default = 1024
4747
}
4848

49-
variable "allinone_subnet" {
49+
variable "comet_ec2_subnet" {
5050
description = "ID of VPC subnet to launch EC2 instance in"
5151
type = string
5252
default = ""
5353
}
5454

55+
variable "s3_enabled" {
56+
description = "Indicates if S3 bucket is being provisioned for Comet"
57+
type = bool
58+
default = null
59+
}
60+
5561
variable "comet_ml_s3_bucket" {
5662
description = "Name of the S3 bucket provisioned for Comet"
5763
type = string

providers.tf

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ provider "aws" {
22
region = var.region
33
}
44

5-
/*
65
provider "kubernetes" {
7-
host = module.eks_deployment[0].cluster_endpoint
8-
cluster_ca_certificate = base64decode(module.eks_deployment[0].cluster_certificate_authority_data)
9-
token = data.aws_eks_cluster_auth.this.token
6+
host = var.enable_eks ? module.comet_eks[0].cluster_endpoint : null
7+
cluster_ca_certificate = var.enable_eks ? base64decode(module.comet_eks[0].cluster_certificate_authority_data) : null
8+
token = var.enable_eks ? data.aws_eks_cluster_auth.this[0].token : null
109
}
1110

1211
provider "helm" {
1312
kubernetes {
14-
host = module.eks_deployment[0].cluster_endpoint
15-
cluster_ca_certificate = base64decode(module.eks_deployment[0].cluster_certificate_authority_data)
16-
token = data.aws_eks_cluster_auth.this.token
13+
host = var.enable_eks ? module.comet_eks[0].cluster_endpoint : null
14+
cluster_ca_certificate = var.enable_eks ? base64decode(module.comet_eks[0].cluster_certificate_authority_data) : null
15+
token = var.enable_eks ? data.aws_eks_cluster_auth.this[0].token : null
1716
}
18-
}
19-
*/
17+
}

0 commit comments

Comments
 (0)