Skip to content

Commit c3a0fbb

Browse files
committed
Stand-alone redis with 1 node
1 parent 2c83af0 commit c3a0fbb

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

examples/complete/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module "redis" {
102102
node_type = local.node_type
103103
environment = local.environment
104104
engine_version = local.redis_engine_version
105-
num_cache_nodes = 2
105+
num_cache_nodes = 2 # Set count 1 for standalone mode
106106
vpc_id = module.vpc.vpc_id
107107
subnets = module.vpc.database_subnets
108108
kms_key_arn = module.kms.key_arn

main.tf

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,64 @@ resource "aws_elasticache_parameter_group" "default" {
3535
}
3636
}
3737

38+
resource "aws_elasticache_cluster" "redis" {
39+
count = var.num_cache_nodes == 1 ? 1 : 0 # Only create if num_cache_nodes == 1
40+
cluster_id = "${var.environment}-${var.name}-standalone-redis"
41+
engine = "redis"
42+
engine_version = var.engine_version
43+
node_type = var.node_type
44+
num_cache_nodes = 1 # Standalone Redis can have only 1 node
45+
port = var.port
46+
parameter_group_name = join("", aws_elasticache_parameter_group.default.*.name)
47+
security_group_ids = [module.security_group_redis.security_group_id]
48+
subnet_group_name = aws_elasticache_subnet_group.elasticache.id
49+
snapshot_window = var.snapshot_window
50+
snapshot_retention_limit = var.snapshot_retention_limit
51+
52+
53+
dynamic "log_delivery_configuration" {
54+
for_each = local.slow_log
55+
content {
56+
log_type = "slow-log"
57+
log_format = var.slow_log_format
58+
destination = var.slow_log_destination
59+
destination_type = var.slow_log_destination_type
60+
}
61+
}
62+
63+
dynamic "log_delivery_configuration" {
64+
for_each = local.engine_log
65+
content {
66+
log_type = "engine-log"
67+
log_format = var.engine_log_format
68+
destination = var.engine_log_destination
69+
destination_type = var.engine_log_destination_type
70+
}
71+
}
72+
73+
tags = {
74+
Name = "${var.environment}-${var.name}"
75+
Environment = var.environment
76+
}
77+
}
78+
3879
resource "aws_elasticache_replication_group" "redis" {
39-
replication_group_id = "${var.environment}-${var.name}-redis"
80+
count = var.num_cache_nodes > 1 ? 1 : 0 # Only create if num_cache_nodes > 1
81+
replication_group_id = "${var.environment}-${var.name}-cluster"
4082
port = var.port
4183
engine = "redis"
4284
node_type = var.node_type
4385
description = "Redis cluster for ${var.environment}-${var.name}-redis"
4486
engine_version = var.engine_version
45-
num_cache_clusters = var.cluster_mode_enabled ? null : var.num_cache_nodes
87+
num_cache_clusters = var.num_cache_nodes # Standalone Redis has 1 node
4688
parameter_group_name = join("", aws_elasticache_parameter_group.default.*.name) #var.parameter_group_name
4789
security_group_ids = [module.security_group_redis.security_group_id]
4890
subnet_group_name = aws_elasticache_subnet_group.elasticache.id
4991
preferred_cache_cluster_azs = length(var.availability_zones) == 0 ? null : [for n in range(0, var.num_cache_nodes) : element(var.availability_zones, n)]
5092
snapshot_arns = var.snapshot_arns
5193
snapshot_window = var.snapshot_window
5294
snapshot_retention_limit = var.snapshot_retention_limit
53-
automatic_failover_enabled = var.cluster_mode_enabled ? true : var.automatic_failover_enabled
95+
automatic_failover_enabled = var.multi_az_enabled ? true : false
5496
multi_az_enabled = var.multi_az_enabled
5597
at_rest_encryption_enabled = var.at_rest_encryption_enabled
5698
kms_key_id = var.at_rest_encryption_enabled ? var.kms_key_arn : null
@@ -177,11 +219,10 @@ resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
177219
namespace = "AWS/ElastiCache"
178220
period = "300"
179221
statistic = "Average"
180-
181-
threshold = var.alarm_cpu_threshold_percent
222+
threshold = var.alarm_cpu_threshold_percent
182223

183224
dimensions = {
184-
CacheClusterId = aws_elasticache_replication_group.redis.id
225+
CacheClusterId = var.num_cache_nodes > 1 ? aws_elasticache_replication_group.redis[count.index].id : aws_elasticache_cluster.redis[0].id
185226
}
186227

187228
alarm_actions = [aws_sns_topic.slack_topic[0].arn]
@@ -204,11 +245,10 @@ resource "aws_cloudwatch_metric_alarm" "cache_memory" {
204245
namespace = "AWS/ElastiCache"
205246
period = "60"
206247
statistic = "Average"
207-
208-
threshold = var.alarm_memory_threshold_bytes
248+
threshold = var.alarm_memory_threshold_bytes
209249

210250
dimensions = {
211-
CacheClusterId = aws_elasticache_replication_group.redis.id
251+
CacheClusterId = var.num_cache_nodes > 1 ? aws_elasticache_replication_group.redis[count.index].id : aws_elasticache_cluster.redis[0].id
212252
}
213253

214254
alarm_actions = [aws_sns_topic.slack_topic[0].arn]

outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ output "elastic_cache_redis_primary_endpoint_address" {
55
}
66
output "elastic_cache_redis_subnet_group_name" {
77
description = "Subnet group name of the elasticache_redis cluster"
8-
value = aws_elasticache_subnet_group.elasticache.name
8+
value = var.num_cache_nodes > 1 ? aws_elasticache_replication_group.redis[0].id : aws_elasticache_cluster.redis[0].id
99
}
1010

1111
output "elastic_cache_redis_cluster_id" {
1212
description = "ID of the elasticache-redis cluster"
13-
value = aws_elasticache_replication_group.redis.id
13+
value = var.num_cache_nodes > 1 ? aws_elasticache_replication_group.redis[0].id : aws_elasticache_cluster.redis[0].id
1414
}
1515

1616
output "elastic_cache_redis_port" {
@@ -35,5 +35,5 @@ output "auth_token_password" {
3535

3636
output "elastic_cache_redis_member_clusters" {
3737
description = "ID of the elasticache-redis cluster"
38-
value = flatten(aws_elasticache_replication_group.redis.member_clusters)
38+
value = (var.num_cache_nodes > 1 ? flatten(aws_elasticache_replication_group.redis[0].member_clusters) : aws_elasticache_cluster.redis[0].id)
3939
}

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ variable "port" {
6666

6767
variable "automatic_failover_enabled" {
6868
description = "Enable automatic failover "
69-
default = true
69+
default = false
7070
type = bool
7171
}
7272

0 commit comments

Comments
 (0)