Skip to content

Commit 2608d8f

Browse files
committed
Configured dynamic ingress rule creation for mysql and redis SGs
1 parent f069d20 commit 2608d8f

File tree

15 files changed

+221
-3897
lines changed

15 files changed

+221
-3897
lines changed

.terraform/modules/comet_eks.eks

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit b6fa04f65bdcb26869215fb840f5ee088a096bc8

.terraform/modules/comet_eks.eks.kms

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit b026981f6b80240f8d22ca7ebcee61d67d1e9256
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit cab883e39115c1e740bddeb8021c1a8c6f73ef52

.terraform/modules/modules.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

main.tf

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module "vpc" {
5151

5252
module "comet_ec2" {
5353
source = "./modules/comet_ec2"
54-
count = var.enable_comet_ec2 ? 1 : 0
54+
count = var.enable_ec2 ? 1 : 0
5555

5656
vpc_id = module.vpc.vpc_id
5757
allinone_ami = "ami-05842f1afbf311a43"
@@ -74,25 +74,31 @@ module "comet_elasticache" {
7474
source = "./modules/comet_elasticache"
7575
count = var.enable_elasticache ? 1 : 0
7676

77+
ec2_enabled = var.enable_ec2
78+
eks_enabled = var.enable_eks
79+
7780
vpc_id = module.vpc.vpc_id
7881
vpc_private_subnets = module.vpc.private_subnets
7982

80-
# need to get SGs from comet_ec2 or comet_eks, depending on which is used
81-
# index is used on the comet_ec2 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"
82-
elasticache_rds_allowfrom_sg = module.comet_ec2[0].allinone_sg_id
83+
# 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
85+
elasticache_allow_eks_sg = var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : null
8386
}
8487

8588
module "comet_rds" {
8689
source = "./modules/comet_rds"
8790
count = var.enable_rds ? 1 : 0
8891

92+
ec2_enabled = var.enable_ec2
93+
eks_enabled = var.enable_eks
94+
8995
availability_zones = local.azs
9096
vpc_id = module.vpc.vpc_id
9197
vpc_private_subnets = module.vpc.private_subnets
9298

93-
# need to get SGs from comet_ec2 or comet_eks, depending on which is used
94-
# index is used on the comet_ec2 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"
95-
elasticache_rds_allowfrom_sg = module.comet_ec2[0].allinone_sg_id
99+
# 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
101+
rds_allow_eks_sg = var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : null
96102

97103
}
98104

modules/comet_ec2/main.tf

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,9 @@ resource "aws_instance" "allinone" {
3838
}
3939

4040
resource "aws_security_group" "allinone_sg" {
41-
name = "${var.environment}-allinone_sg"
42-
description = "Comet.ML AllInOne Security Group"
41+
name = "comet_${var.environment}_ec2_sg"
42+
description = "Comet EC2 instance security group"
4343
vpc_id = var.vpc_id
44-
/* remove inline rules in favor of separate resource declarations
45-
ingress {
46-
from_port = local.ssh_port
47-
to_port = local.ssh_port
48-
protocol = "tcp"
49-
# We recommend restricting that to your company IP or by Using a bastion host
50-
#security_groups = [aws_security_group.bastion_inbound_sg.id]
51-
cidr_blocks = [local.cidr_anywhere]
52-
53-
}
54-
55-
ingress {
56-
from_port = local.http_port
57-
to_port = local.http_port
58-
protocol = "tcp"
59-
security_groups = [aws_security_group.lb_inbound_sg.id]
60-
}
61-
62-
egress {
63-
from_port = 0
64-
to_port = 0
65-
protocol = "-1"
66-
cidr_blocks = [local.cidr_anywhere]
67-
}
68-
*/
6944
}
7045

7146
resource "aws_vpc_security_group_ingress_rule" "allinone_ingress_ssh" {
@@ -74,8 +49,6 @@ resource "aws_vpc_security_group_ingress_rule" "allinone_ingress_ssh" {
7449
from_port = local.ssh_port
7550
to_port = local.ssh_port
7651
ip_protocol = "tcp"
77-
# We recommend restricting that to your company IP or by Using a bastion host
78-
#security_groups = [aws_security_group.bastion_inbound_sg.id]
7952
cidr_ipv4 = local.cidr_anywhere
8053
}
8154

@@ -103,10 +76,6 @@ resource "aws_vpc_security_group_ingress_rule" "allinone_ingress_http" {
10376

10477
resource "aws_vpc_security_group_egress_rule" "allinone_egress_any" {
10578
security_group_id = aws_security_group.allinone_sg.id
106-
/* no port ranges permitted when specifying all protocols
107-
from_port = local.any_port
108-
to_port = local.any_port
109-
*/
11079
ip_protocol = "-1"
11180
cidr_ipv4 = local.cidr_anywhere
11281
}

modules/comet_eks/main.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ data "aws_iam_policy" "ebs_csi_policy" {
1515
arn = "arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy"
1616
}
1717

18-
/*
19-
data "aws_iam_policy" "administrator_access" {
20-
arn = "arn:aws:iam::aws:policy/AdministratorAccess"
21-
}
22-
*/
23-
2418
module "eks" {
2519
source = "terraform-aws-modules/eks/aws"
2620
version = "~> 19.9"

modules/comet_eks/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ output "cluster_certificate_authority_data" {
1010
value = module.eks.cluster_certificate_authority_data
1111
}
1212

13+
output "nodegroup_sg_id" {
14+
value = module.eks.node_security_group_id
15+
}

modules/comet_elasticache/main.tf

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,22 @@ resource "aws_security_group" "redis_inbound_sg" {
3131
vpc_id = var.vpc_id
3232
}
3333

34-
resource "aws_vpc_security_group_ingress_rule" "redis_port_inbound_allow" {
34+
resource "aws_vpc_security_group_ingress_rule" "redis_port_inbound_ec2" {
35+
count = var.ec2_enabled ? 1 : 0
3536
security_group_id = aws_security_group.redis_inbound_sg.id
3637

3738
from_port = local.redis_port
3839
to_port = local.redis_port
3940
ip_protocol = "tcp"
40-
# security groups need to change depending on whether Cx is using eks or ec2 deployment; hard-coded index won't work
41-
referenced_security_group_id = var.elasticache_rds_allowfrom_sg
41+
referenced_security_group_id = var.elasticache_allow_ec2_sg
42+
}
43+
44+
resource "aws_vpc_security_group_ingress_rule" "redis_port_inbound_eks" {
45+
count = var.eks_enabled ? 1 : 0
46+
security_group_id = aws_security_group.redis_inbound_sg.id
47+
48+
from_port = local.redis_port
49+
to_port = local.redis_port
50+
ip_protocol = "tcp"
51+
referenced_security_group_id = var.elasticache_allow_eks_sg
4252
}

modules/comet_elasticache/variables.tf

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,26 @@ variable "vpc_private_subnets" {
4646
default = []
4747
}
4848

49-
variable "elasticache_rds_allowfrom_sg" {
50-
description = "Security group(s) attached to the Comet instance(s); Specified in the ingress allow rules on the Elasticache and RDS security groups"
49+
variable "elasticache_allow_ec2_sg" {
50+
description = "Security group associated with EC2 compute, if provisioned"
5151
type = string
5252
default = ""
53+
}
54+
55+
variable "elasticache_allow_eks_sg" {
56+
description = "Security group associated with EKS compute, if provisioned"
57+
type = string
58+
default = ""
59+
}
60+
61+
variable "ec2_enabled" {
62+
description = "Indicates if EC2 compute has been provisioned for Comet"
63+
type = bool
64+
default = null
65+
}
66+
67+
variable "eks_enabled" {
68+
description = "Indicates if EKS compute has been provisioned for Comet"
69+
type = bool
70+
default = null
5371
}

0 commit comments

Comments
 (0)