Skip to content

Commit 559a44e

Browse files
authored
Merge pull request #3 from comet-ml/existing-vpc
Enable provisioning Comet resources into existing VPC
2 parents 2cdd966 + 63d12bc commit 559a44e

File tree

12 files changed

+202
-180
lines changed

12 files changed

+202
-180
lines changed

comet-infrastructure/.terraform.lock.hcl

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

comet-infrastructure/main.tf

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,34 @@
1-
data "aws_availability_zones" "available" {}
2-
31
data "aws_eks_cluster_auth" "this" {
42
count = var.enable_eks ? 1 : 0
53
name = module.comet_eks[0].cluster_name
64
}
75

86
locals {
97
resource_name = "comet-${var.environment}"
10-
vpc_cidr = "10.0.0.0/16"
11-
azs = slice(data.aws_availability_zones.available.names, 0, 3)
128

139
#set environment here, and use local.environment for the environment variables in all of the module calls
14-
1510
tags = {
1611
Terraform = "true"
1712
Environment = var.environment
1813
}
1914
}
2015

21-
module "vpc" {
22-
source = "terraform-aws-modules/vpc/aws"
23-
version = "~> 5.0.0"
24-
25-
name = local.resource_name
26-
cidr = local.vpc_cidr
27-
28-
azs = local.azs
29-
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)]
30-
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 10)]
31-
32-
enable_nat_gateway = true
33-
single_nat_gateway = true
34-
enable_dns_hostnames = true
35-
36-
# Manage so we can name
37-
manage_default_network_acl = true
38-
default_network_acl_tags = { Name = "${local.resource_name}-default" }
39-
manage_default_route_table = true
40-
default_route_table_tags = { Name = "${local.resource_name}-default" }
41-
manage_default_security_group = true
42-
default_security_group_tags = { Name = "${local.resource_name}-default" }
43-
44-
# if EKS deployment, set subnet tags for AWS Load Balancer Controller auto-discovery
45-
public_subnet_tags = var.enable_eks ? {"kubernetes.io/role/elb" = 1} : null
46-
private_subnet_tags = var.enable_eks ? {"kubernetes.io/role/internal-elb" = 1} : null
47-
48-
tags = local.tags
16+
module "comet_vpc" {
17+
source = "./modules/comet_vpc"
18+
count = var.enable_vpc ? 1 : 0
19+
environment = var.environment
20+
21+
eks_enabled = var.enable_eks
22+
single_nat_gateway = var.single_nat_gateway
4923
}
5024

5125
module "comet_ec2" {
5226
source = "./modules/comet_ec2"
5327
count = var.enable_ec2 ? 1 : 0
5428
environment = var.environment
5529

56-
vpc_id = module.vpc.vpc_id
57-
comet_ec2_subnet = module.vpc.public_subnets[count.index % length(module.vpc.public_subnets)]
30+
vpc_id = var.enable_vpc ? module.comet_vpc[0].vpc_id : var.comet_vpc_id
31+
comet_ec2_subnet = var.enable_vpc ? module.comet_vpc[0].public_subnets[0] : var.comet_public_subnets[0]
5832
comet_ec2_ami_type = var.comet_ec2_ami_type
5933
comet_ec2_instance_type = var.comet_ec2_instance_type
6034
comet_ec2_instance_count = var.comet_ec2_instance_count
@@ -74,8 +48,8 @@ module "comet_ec2_alb" {
7448
count = var.enable_ec2_alb ? 1 : 0
7549
environment = var.environment
7650

77-
vpc_id = module.vpc.vpc_id
78-
public_subnets = module.vpc.public_subnets
51+
vpc_id = var.enable_vpc ? module.comet_vpc[0].vpc_id : var.comet_vpc_id
52+
public_subnets = var.enable_vpc ? module.comet_vpc[0].public_subnets : var.comet_public_subnets
7953
ssl_certificate_arn = var.enable_ec2_alb ? var.ssl_certificate_arn : null
8054
}
8155

@@ -84,8 +58,8 @@ module "comet_eks" {
8458
count = var.enable_eks ? 1 : 0
8559
environment = var.environment
8660

87-
vpc_id = module.vpc.vpc_id
88-
eks_private_subnets = module.vpc.private_subnets
61+
vpc_id = var.enable_vpc ? module.comet_vpc[0].vpc_id : var.comet_vpc_id
62+
eks_private_subnets = var.enable_vpc ? module.comet_vpc[0].private_subnets : var.comet_private_subnets
8963
eks_cluster_name = var.eks_cluster_name
9064
eks_cluster_version = var.eks_cluster_version
9165
eks_mng_name = var.eks_mng_name
@@ -107,13 +81,11 @@ module "comet_elasticache" {
10781
count = var.enable_elasticache ? 1 : 0
10882
environment = var.environment
10983

110-
ec2_enabled = var.enable_ec2
111-
eks_enabled = var.enable_eks
112-
113-
vpc_id = module.vpc.vpc_id
114-
elasticache_private_subnets = module.vpc.private_subnets
115-
elasticache_allow_ec2_sg = var.enable_ec2 ? module.comet_ec2[0].comet_ec2_sg_id : null
116-
elasticache_allow_eks_sg = var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : null
84+
vpc_id = var.enable_vpc ? module.comet_vpc[0].vpc_id : var.comet_vpc_id
85+
elasticache_private_subnets = var.enable_vpc ? module.comet_vpc[0].private_subnets : var.comet_private_subnets
86+
elasticache_allow_from_sg = var.enable_ec2 ? module.comet_ec2[0].comet_ec2_sg_id : (
87+
var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : (
88+
var.elasticache_allow_from_sg))
11789
elasticache_engine = var.elasticache_engine
11890
elasticache_engine_version = var.elasticache_engine_version
11991
elasticache_instance_type = var.elasticache_instance_type
@@ -126,14 +98,12 @@ module "comet_rds" {
12698
count = var.enable_rds ? 1 : 0
12799
environment = var.environment
128100

129-
ec2_enabled = var.enable_ec2
130-
eks_enabled = var.enable_eks
131-
132-
availability_zones = local.azs
133-
vpc_id = module.vpc.vpc_id
134-
rds_private_subnets = module.vpc.private_subnets
135-
rds_allow_ec2_sg = var.enable_ec2 ? module.comet_ec2[0].comet_ec2_sg_id : null
136-
rds_allow_eks_sg = var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : null
101+
availability_zones = var.enable_vpc ? module.comet_vpc[0].azs : var.availability_zones
102+
vpc_id = var.enable_vpc ? module.comet_vpc[0].vpc_id : var.comet_vpc_id
103+
rds_private_subnets = var.enable_vpc ? module.comet_vpc[0].private_subnets : var.comet_private_subnets
104+
rds_allow_from_sg = var.enable_ec2 ? module.comet_ec2[0].comet_ec2_sg_id : (
105+
var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : (
106+
var.rds_allow_from_sg))
137107
rds_engine = var.rds_engine
138108
rds_engine_version = var.rds_engine_version
139109
rds_instance_type = var.rds_instance_type

comet-infrastructure/modules/comet_elasticache/main.tf

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,11 @@ resource "aws_security_group" "redis_inbound_sg" {
3030
vpc_id = var.vpc_id
3131
}
3232

33-
resource "aws_vpc_security_group_ingress_rule" "redis_port_inbound_ec2" {
34-
count = var.ec2_enabled ? 1 : 0
33+
resource "aws_vpc_security_group_ingress_rule" "redis_port_inbound_rule" {
3534
security_group_id = aws_security_group.redis_inbound_sg.id
3635

3736
from_port = local.redis_port
3837
to_port = local.redis_port
3938
ip_protocol = "tcp"
40-
referenced_security_group_id = var.elasticache_allow_ec2_sg
41-
}
42-
43-
resource "aws_vpc_security_group_ingress_rule" "redis_port_inbound_eks" {
44-
count = var.eks_enabled ? 1 : 0
45-
security_group_id = aws_security_group.redis_inbound_sg.id
46-
47-
from_port = local.redis_port
48-
to_port = local.redis_port
49-
ip_protocol = "tcp"
50-
referenced_security_group_id = var.elasticache_allow_eks_sg
39+
referenced_security_group_id = var.elasticache_allow_from_sg
5140
}

comet-infrastructure/modules/comet_elasticache/variables.tf

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@ variable "elasticache_private_subnets" {
1414
type = list(string)
1515
}
1616

17-
variable "elasticache_allow_ec2_sg" {
18-
description = "Security group associated with EC2 compute, if provisioned"
19-
type = string
20-
}
21-
22-
variable "elasticache_allow_eks_sg" {
23-
description = "Security group associated with EKS compute, if provisioned"
17+
variable "elasticache_allow_from_sg" {
18+
description = "Security group from which connections to ElastiCache will be allowed"
2419
type = string
2520
}
2621

@@ -47,16 +42,4 @@ variable "elasticache_param_group_name" {
4742
variable "elasticache_num_cache_nodes" {
4843
description = "Number of nodes in the Elasticache cluster"
4944
type = number
50-
}
51-
52-
variable "ec2_enabled" {
53-
description = "Indicates if EC2 compute has been provisioned for Comet"
54-
type = bool
55-
default = null
56-
}
57-
58-
variable "eks_enabled" {
59-
description = "Indicates if EKS compute has been provisioned for Comet"
60-
type = bool
61-
default = null
6245
}

comet-infrastructure/modules/comet_rds/main.tf

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,9 @@ resource "aws_security_group" "mysql_sg" {
107107
}
108108

109109
resource "aws_vpc_security_group_ingress_rule" "mysql_port_inbound_ec2" {
110-
count = var.ec2_enabled ? 1 : 0
111110
security_group_id = aws_security_group.mysql_sg.id
112111
from_port = local.mysql_port
113112
to_port = local.mysql_port
114113
ip_protocol = "tcp"
115-
referenced_security_group_id = var.rds_allow_ec2_sg
116-
}
117-
118-
resource "aws_vpc_security_group_ingress_rule" "mysql_port_inbound_eks" {
119-
count = var.eks_enabled ? 1 : 0
120-
security_group_id = aws_security_group.mysql_sg.id
121-
from_port = local.mysql_port
122-
to_port = local.mysql_port
123-
ip_protocol = "tcp"
124-
referenced_security_group_id = var.rds_allow_eks_sg
114+
referenced_security_group_id = var.rds_allow_from_sg
125115
}

comet-infrastructure/modules/comet_rds/variables.tf

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
variable "environment" {
22
description = "Deployment environment, i.e. dev/stage/prod, etc"
33
type = string
4-
default = "dev"
54
}
65

76
variable "availability_zones" {
@@ -19,13 +18,8 @@ variable "rds_private_subnets" {
1918
type = list(string)
2019
}
2120

22-
variable "rds_allow_ec2_sg" {
23-
description = "Security group associated with EC2 compute, if provisioned"
24-
type = string
25-
}
26-
27-
variable "rds_allow_eks_sg" {
28-
description = "Security group associated with EKS compute, if provisioned"
21+
variable "rds_allow_from_sg" {
22+
description = "Security group from which to allow connections to RDS"
2923
type = string
3024
}
3125

@@ -78,15 +72,3 @@ variable "rds_root_password" {
7872
description = "Root password for RDS database"
7973
type = string
8074
}
81-
82-
variable "ec2_enabled" {
83-
description = "Indicates if EC2 compute has been provisioned for Comet"
84-
type = bool
85-
default = null
86-
}
87-
88-
variable "eks_enabled" {
89-
description = "Indicates if EKS compute has been provisioned for Comet"
90-
type = bool
91-
default = null
92-
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
data "aws_availability_zones" "available" {}
2+
3+
locals {
4+
resource_name = "comet-${var.environment}"
5+
vpc_cidr = "10.0.0.0/16"
6+
azs = slice(data.aws_availability_zones.available.names, 0, 3)
7+
8+
tags = {
9+
Terraform = "true"
10+
Environment = var.environment
11+
}
12+
}
13+
14+
module "vpc" {
15+
source = "terraform-aws-modules/vpc/aws"
16+
version = "~> 5.0.0"
17+
18+
name = "${local.resource_name}-vpc"
19+
cidr = local.vpc_cidr
20+
21+
azs = local.azs
22+
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)]
23+
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 10)]
24+
25+
enable_nat_gateway = true
26+
enable_dns_hostnames = true
27+
single_nat_gateway = var.single_nat_gateway
28+
29+
# Manage so we can name
30+
manage_default_network_acl = true
31+
default_network_acl_tags = { Name = "${local.resource_name}-default" }
32+
manage_default_route_table = true
33+
default_route_table_tags = { Name = "${local.resource_name}-default" }
34+
manage_default_security_group = true
35+
default_security_group_tags = { Name = "${local.resource_name}-default" }
36+
37+
# if EKS deployment, set subnet tags for AWS Load Balancer Controller auto-discovery
38+
public_subnet_tags = var.eks_enabled ? {"kubernetes.io/role/elb" = 1} : null
39+
private_subnet_tags = var.eks_enabled ? {"kubernetes.io/role/internal-elb" = 1} : null
40+
41+
tags = local.tags
42+
}

0 commit comments

Comments
 (0)