Skip to content

Commit f8b52a9

Browse files
committed
Enable provisioning in existing VPC with VPC creation moved into submodule
1 parent 2cdd966 commit f8b52a9

File tree

8 files changed

+177
-113
lines changed

8 files changed

+177
-113
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: 18 additions & 44 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
@@ -110,8 +84,8 @@ module "comet_elasticache" {
11084
ec2_enabled = var.enable_ec2
11185
eks_enabled = var.enable_eks
11286

113-
vpc_id = module.vpc.vpc_id
114-
elasticache_private_subnets = module.vpc.private_subnets
87+
vpc_id = var.enable_vpc ? module.comet_vpc[0].vpc_id : var.comet_vpc_id
88+
elasticache_private_subnets = var.enable_vpc ? module.comet_vpc[0].private_subnets : var.comet_private_subnets
11589
elasticache_allow_ec2_sg = var.enable_ec2 ? module.comet_ec2[0].comet_ec2_sg_id : null
11690
elasticache_allow_eks_sg = var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : null
11791
elasticache_engine = var.elasticache_engine
@@ -129,9 +103,9 @@ module "comet_rds" {
129103
ec2_enabled = var.enable_ec2
130104
eks_enabled = var.enable_eks
131105

132-
availability_zones = local.azs
133-
vpc_id = module.vpc.vpc_id
134-
rds_private_subnets = module.vpc.private_subnets
106+
availability_zones = var.enable_vpc ? module.comet_vpc[0].azs : var.availability_zones
107+
vpc_id = var.enable_vpc ? module.comet_vpc[0].vpc_id : var.comet_vpc_id
108+
rds_private_subnets = var.enable_vpc ? module.comet_vpc[0].private_subnets : var.comet_private_subnets
135109
rds_allow_ec2_sg = var.enable_ec2 ? module.comet_ec2[0].comet_ec2_sg_id : null
136110
rds_allow_eks_sg = var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : null
137111
rds_engine = var.rds_engine
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "azs" {
2+
description = "List of availability zones in the region"
3+
value = module.vpc.azs
4+
}
5+
6+
output "private_subnets" {
7+
description = "List of IDs for private subnets provisioned in the VPC"
8+
value = module.vpc.private_subnets
9+
}
10+
11+
output "public_subnets" {
12+
description = "List of IDs for public subnets provisioned in the VPC"
13+
value = module.vpc.public_subnets
14+
}
15+
16+
output "vpc_id" {
17+
description = "ID of the provisioned VPC"
18+
value = module.vpc.vpc_id
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "environment" {
2+
description = "Deployment environment, i.e. dev/stage/prod, etc"
3+
type = string
4+
}
5+
6+
variable "eks_enabled" {
7+
description = "Indicates if EKS module enabled"
8+
type = bool
9+
}
10+
11+
variable "single_nat_gateway" {
12+
description = "Controls whether single NAT gateway used for all public subnets"
13+
type = bool
14+
}

comet-infrastructure/modules/comet_vpc/versions.tf

Whitespace-only changes.

comet-infrastructure/terraform.tfvars

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
enable_vpc = false
12
enable_ec2 = false
23
enable_ec2_alb = false
34
enable_eks = false
45
enable_elasticache = false
56
enable_rds = false
67
enable_s3 = false
7-
region = "us-east-2"
8-
environment = "dev"
8+
9+
region = "us-east-1"
10+
environment = "prod"
11+
12+
# if not using enable_vpc to provision a VPC for the Comet resources, set the variables below to specify the existing VPC
13+
comet_vpc_id = "vpc-012345abcdefghijkl"
14+
availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"]
15+
comet_public_subnets = ["subnet-012345abcdefghijkl", "subnet-012345abcdefghijkl", "subnet-012345abcdefghijkl"]
16+
comet_private_subnets = ["subnet-012345abcdefghijkl", "subnet-012345abcdefghijkl", "subnet-012345abcdefghijkl"]
17+
918
s3_bucket_name = "comet-use2-bucket"
1019
rds_root_password = "CHANGE-ME"
1120
ssl_certificate_arn = ""

0 commit comments

Comments
 (0)