Skip to content

Commit 9a75d0f

Browse files
committed
Refactor ElastiCache module SG rule creation
1 parent f8b52a9 commit 9a75d0f

File tree

5 files changed

+17
-38
lines changed

5 files changed

+17
-38
lines changed

comet-infrastructure/main.tf

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,11 @@ module "comet_elasticache" {
8181
count = var.enable_elasticache ? 1 : 0
8282
environment = var.environment
8383

84-
ec2_enabled = var.enable_ec2
85-
eks_enabled = var.enable_eks
86-
8784
vpc_id = var.enable_vpc ? module.comet_vpc[0].vpc_id : var.comet_vpc_id
8885
elasticache_private_subnets = var.enable_vpc ? module.comet_vpc[0].private_subnets : var.comet_private_subnets
89-
elasticache_allow_ec2_sg = var.enable_ec2 ? module.comet_ec2[0].comet_ec2_sg_id : null
90-
elasticache_allow_eks_sg = var.enable_eks ? module.comet_eks[0].nodegroup_sg_id : null
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))
9189
elasticache_engine = var.elasticache_engine
9290
elasticache_engine_version = var.elasticache_engine_version
9391
elasticache_instance_type = var.elasticache_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/terraform.tfvars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"]
1515
comet_public_subnets = ["subnet-012345abcdefghijkl", "subnet-012345abcdefghijkl", "subnet-012345abcdefghijkl"]
1616
comet_private_subnets = ["subnet-012345abcdefghijkl", "subnet-012345abcdefghijkl", "subnet-012345abcdefghijkl"]
1717

18+
# if provisioning ElastiCache in existing VPC, set the variable below to specify an SG that connections will be allowed from
19+
elasticache_allow_from_sg = "sg-012345abcdefghijkl"
20+
1821
s3_bucket_name = "comet-use2-bucket"
1922
rds_root_password = "CHANGE-ME"
2023
ssl_certificate_arn = ""

comet-infrastructure/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ variable "region" {
1111
}
1212

1313
variable "availability_zones" {
14-
description = "List of availability zones from VPC"
14+
description = "List of availability zones from region"
1515
type = list(string)
1616
default = null
1717
}
@@ -193,6 +193,12 @@ variable "eks_external_dns" {
193193
}
194194

195195
#comet_elasticache
196+
variable "elasticache_allow_from_sg" {
197+
description = "Security group from which to allow connections to ElastiCache, for use when provisioning in existing VPC"
198+
type = string
199+
default = null
200+
}
201+
196202
variable "elasticache_engine" {
197203
description = "Engine type for ElastiCache cluster"
198204
type = string

0 commit comments

Comments
 (0)