This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
forked from cloudposse/terraform-aws-elasticache-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
159 lines (134 loc) · 6.65 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
locals {
elasticache_subnet_group_name = var.existing_elasticache_subnet_group_name != "" ? var.existing_elasticache_subnet_group_name : join("", aws_elasticache_subnet_group.default.*.name)
replication_group_id = var.replication_group_id != "" ? var.replication_group_id : join("", module.this.*.id)
# if !cluster, then node_count = replica cluster_size, if cluster then node_count = shard*(replica + 1)
# Why doing this 'The "count" value depends on resource attributes that cannot be determined until apply'. So pre-calculating
member_clusters_count = (var.cluster_mode_enabled
?
(var.cluster_mode_num_node_groups * (var.cluster_mode_replicas_per_node_group + 1))
:
var.cluster_size
)
elasticache_member_clusters = module.this.enabled ? tolist(aws_elasticache_replication_group.default.0.member_clusters) : []
security_group_enabled = module.this.enabled && var.security_group_enabled
}
module "security_group" {
source = "cloudposse/security-group/aws"
version = "0.3.1"
use_name_prefix = var.security_group_use_name_prefix
rules = var.security_group_rules
description = var.security_group_description
vpc_id = var.vpc_id
enabled = local.security_group_enabled
context = module.this.context
}
resource "aws_elasticache_subnet_group" "default" {
count = module.this.enabled && length(var.subnets) > 0 ? 1 : 0
name = var.existing_elasticache_subnet_group_name == "" ? module.this.id : var.existing_elasticache_subnet_group_name
subnet_ids = var.subnets
}
resource "aws_elasticache_parameter_group" "default" {
count = module.this.enabled && var.parameter_group == "" ? 1 : 0
name = module.this.id
family = var.family
dynamic "parameter" {
for_each = var.cluster_mode_enabled ? concat([{ name = "cluster-enabled", value = "yes" }], var.parameter) : var.parameter
content {
name = parameter.value.name
value = parameter.value.value
}
}
}
resource "aws_elasticache_replication_group" "default" {
count = module.this.enabled ? 1 : 0
auth_token = var.transit_encryption_enabled ? var.auth_token : null
replication_group_id = local.replication_group_id
replication_group_description = module.this.id
node_type = var.instance_type
number_cache_clusters = var.cluster_mode_enabled ? null : var.cluster_size
port = var.port
parameter_group_name = var.parameter_group == "" ? join("", aws_elasticache_parameter_group.default.*.name) : var.parameter_group
availability_zones = length(var.availability_zones) == 0 ? null : [for n in range(0, var.cluster_size) : element(var.availability_zones, n)]
automatic_failover_enabled = var.automatic_failover_enabled
multi_az_enabled = var.multi_az_enabled
subnet_group_name = local.elasticache_subnet_group_name
security_group_ids = compact(concat(module.security_group.*.id, var.security_groups))
maintenance_window = var.maintenance_window
notification_topic_arn = var.notification_topic_arn
engine_version = var.engine_version
at_rest_encryption_enabled = var.at_rest_encryption_enabled
transit_encryption_enabled = var.auth_token != null ? coalesce(true, var.transit_encryption_enabled) : var.transit_encryption_enabled
kms_key_id = var.at_rest_encryption_enabled ? var.kms_key_id : null
snapshot_name = var.snapshot_name
snapshot_arns = var.snapshot_arns
snapshot_window = var.snapshot_window
snapshot_retention_limit = var.snapshot_retention_limit
final_snapshot_identifier = var.final_snapshot_identifier
apply_immediately = var.apply_immediately
tags = module.this.tags
dynamic "cluster_mode" {
for_each = var.cluster_mode_enabled ? ["true"] : []
content {
replicas_per_node_group = var.cluster_mode_replicas_per_node_group
num_node_groups = var.cluster_mode_num_node_groups
}
}
dynamic "log_delivery_configuration" {
for_each = var.log_delivery_configuration
content {
destination = lookup(log_delivery_configuration.value, "destination", null)
destination_type = lookup(log_delivery_configuration.value, "destination_type", null)
log_format = lookup(log_delivery_configuration.value, "log_format", null)
log_type = lookup(log_delivery_configuration.value, "log_type", null)
}
}
}
#
# CloudWatch Resources
#
resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
count = module.this.enabled && var.cloudwatch_metric_alarms_enabled ? local.member_clusters_count : 0
alarm_name = "${element(local.elasticache_member_clusters, count.index)}-cpu-utilization"
alarm_description = "Redis cluster CPU utilization"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/ElastiCache"
period = "300"
statistic = "Average"
threshold = var.alarm_cpu_threshold_percent
dimensions = {
CacheClusterId = element(local.elasticache_member_clusters, count.index)
}
alarm_actions = var.alarm_actions
ok_actions = var.ok_actions
depends_on = [aws_elasticache_replication_group.default]
}
resource "aws_cloudwatch_metric_alarm" "cache_memory" {
count = module.this.enabled && var.cloudwatch_metric_alarms_enabled ? local.member_clusters_count : 0
alarm_name = "${element(local.elasticache_member_clusters, count.index)}-freeable-memory"
alarm_description = "Redis cluster freeable memory"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "FreeableMemory"
namespace = "AWS/ElastiCache"
period = "60"
statistic = "Average"
threshold = var.alarm_memory_threshold_bytes
dimensions = {
CacheClusterId = element(local.elasticache_member_clusters, count.index)
}
alarm_actions = var.alarm_actions
ok_actions = var.ok_actions
depends_on = [aws_elasticache_replication_group.default]
}
module "dns" {
source = "cloudposse/route53-cluster-hostname/aws"
version = "0.12.0"
enabled = module.this.enabled && var.zone_id != "" ? true : false
dns_name = var.dns_subdomain != "" ? var.dns_subdomain : module.this.id
ttl = 60
zone_id = var.zone_id
records = var.cluster_mode_enabled ? [join("", aws_elasticache_replication_group.default.*.configuration_endpoint_address)] : [join("", aws_elasticache_replication_group.default.*.primary_endpoint_address)]
context = module.this.context
}