-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
main.tf
334 lines (278 loc) · 13.1 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#
# Security Group Resources
#
locals {
enabled = module.this.enabled
create_normal_instance = local.enabled && !var.serverless_enabled
create_serverless_instance = local.enabled && var.serverless_enabled
legacy_egress_rule = local.use_legacy_egress ? {
key = "legacy-egress"
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = var.egress_cidr_blocks
description = "Allow outbound traffic to existing CIDR blocks"
} : null
legacy_cidr_ingress_rule = length(var.allowed_cidr_blocks) == 0 ? null : {
key = "legacy-cidr-ingress"
type = "ingress"
from_port = var.port
to_port = var.port
protocol = "tcp"
cidr_blocks = var.allowed_cidr_blocks
description = "Allow inbound traffic from CIDR blocks"
}
sg_rules = {
legacy = merge(local.legacy_egress_rule, local.legacy_cidr_ingress_rule),
extra = var.additional_security_group_rules
}
}
module "aws_security_group" {
source = "cloudposse/security-group/aws"
version = "2.2.0"
enabled = local.create_security_group
target_security_group_id = var.target_security_group_id
allow_all_egress = local.allow_all_egress
security_group_name = var.security_group_name
rules_map = local.sg_rules
rule_matrix = [{
key = "in"
source_security_group_ids = local.allowed_security_group_ids
cidr_blocks = var.allowed_cidr_blocks
rules = [{
key = "in"
type = "ingress"
from_port = var.port
to_port = var.port
protocol = "tcp"
description = "Selectively allow inbound traffic"
}]
}]
vpc_id = var.vpc_id
security_group_description = local.security_group_description
create_before_destroy = var.security_group_create_before_destroy
preserve_security_group_id = var.preserve_security_group_id
inline_rules_enabled = var.inline_rules_enabled
revoke_rules_on_delete = var.revoke_rules_on_delete
security_group_create_timeout = var.security_group_create_timeout
security_group_delete_timeout = var.security_group_delete_timeout
context = module.this.context
}
locals {
elasticache_subnet_group_name = var.elasticache_subnet_group_name != "" ? var.elasticache_subnet_group_name : join("", aws_elasticache_subnet_group.default[*].name)
# 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 = local.create_normal_instance ? tolist(aws_elasticache_replication_group.default[0].member_clusters) : []
# The name of the parameter group can’t include "."
safe_family = replace(var.family, ".", "-")
parameter_group_name = (
var.parameter_group_name != null ? var.parameter_group_name : (
var.create_parameter_group
?
"${module.this.id}-${local.safe_family}" # The name of the new parameter group to be created
:
"default.${var.family}" # Default parameter group name created by AWS
)
)
arn = (
# If cluster mode is enabled, use the ARN of the replication group
local.create_normal_instance ? join("", aws_elasticache_replication_group.default[*].arn) :
# If serverless mode is enabled, use the ARN of the serverless cache
join("", aws_elasticache_serverless_cache.default[*].arn)
)
endpoint_serverless = try(aws_elasticache_serverless_cache.default[0].endpoint[0].address, null)
endpoint_cluster = try(aws_elasticache_replication_group.default[0].configuration_endpoint_address, null)
endpoint_instance = try(aws_elasticache_replication_group.default[0].primary_endpoint_address, null)
# Use the serverless endpoint if serverless mode is enabled, otherwise use the cluster endpoint, otherwise use the instance endpoint
endpoint_address = coalesce(local.endpoint_serverless, local.endpoint_cluster, local.endpoint_instance)
reader_endpoint_serverless = try(aws_elasticache_serverless_cache.default[0].reader_endpoint[0].address, null)
reader_endpoint_cluster = try(aws_elasticache_replication_group.default[0].reader_endpoint_address, null)
reader_endpoint_instance = try(aws_elasticache_replication_group.default[0].reader_endpoint_address, null)
# Use the serverless reader endpoint if serverless mode is enabled, otherwise use the cluster reader endpoint, otherwise use the instance reader endpoint, otherwise use the endpoint address
reader_endpoint_address = coalesce(local.reader_endpoint_serverless, local.reader_endpoint_cluster, local.reader_endpoint_instance, local.endpoint_address)
}
resource "aws_elasticache_subnet_group" "default" {
count = local.enabled && var.elasticache_subnet_group_name == "" && length(var.subnets) > 0 ? 1 : 0
name = module.this.id
description = "Elasticache subnet group for ${module.this.id}"
subnet_ids = var.subnets
tags = module.this.tags
}
resource "aws_elasticache_parameter_group" "default" {
count = local.enabled && var.create_parameter_group ? 1 : 0
name = local.parameter_group_name
description = var.parameter_group_description != null ? var.parameter_group_description : "Elasticache parameter group ${local.parameter_group_name}"
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 = tostring(parameter.value.value)
}
}
tags = module.this.tags
lifecycle {
create_before_destroy = true
# Ignore changes to the description since it will try to recreate the resource
ignore_changes = [
description,
]
}
}
# Create a "normal" Elasticache instance (single node or cluster)
resource "aws_elasticache_replication_group" "default" {
count = local.create_normal_instance ? 1 : 0
auth_token = var.transit_encryption_enabled ? var.auth_token : null
auth_token_update_strategy = var.auth_token_update_strategy
replication_group_id = var.replication_group_id == "" ? module.this.id : var.replication_group_id
description = coalesce(var.description, module.this.id)
node_type = var.instance_type
num_cache_clusters = var.cluster_mode_enabled ? null : var.cluster_size
port = var.port
parameter_group_name = local.parameter_group_name
preferred_cache_cluster_azs = length(var.availability_zones) == 0 ? null : [for n in range(0, var.cluster_size) : element(var.availability_zones, n)]
automatic_failover_enabled = var.cluster_mode_enabled ? true : var.automatic_failover_enabled
multi_az_enabled = var.multi_az_enabled
subnet_group_name = local.elasticache_subnet_group_name
network_type = var.network_type
# It would be nice to remove null or duplicate security group IDs, if there are any, using `compact`,
# but that causes problems, and having duplicates does not seem to cause problems.
# See https://github.com/hashicorp/terraform/issues/29799
security_group_ids = local.create_security_group ? concat(local.associated_security_group_ids, [module.aws_security_group.id]) : local.associated_security_group_ids
maintenance_window = var.maintenance_window
notification_topic_arn = var.notification_topic_arn
engine = var.engine
engine_version = var.engine_version
at_rest_encryption_enabled = var.at_rest_encryption_enabled
transit_encryption_enabled = var.transit_encryption_enabled
transit_encryption_mode = var.transit_encryption_mode
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
data_tiering_enabled = var.data_tiering_enabled
auto_minor_version_upgrade = var.auto_minor_version_upgrade
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)
}
}
tags = module.this.tags
num_node_groups = var.cluster_mode_enabled ? var.cluster_mode_num_node_groups : null
replicas_per_node_group = var.cluster_mode_enabled ? var.cluster_mode_replicas_per_node_group : null
user_group_ids = var.user_group_ids
# When importing an aws_elasticache_replication_group resource the attribute
# security_group_names is imported as null. More details:
# https://github.com/hashicorp/terraform-provider-aws/issues/32835
lifecycle {
ignore_changes = [
security_group_names,
]
}
depends_on = [
aws_elasticache_parameter_group.default
]
}
# Create a Serverless Redis/Valkey instance
resource "aws_elasticache_serverless_cache" "default" {
count = local.create_serverless_instance ? 1 : 0
name = var.replication_group_id == "" ? module.this.id : var.replication_group_id
engine = var.engine
kms_key_id = var.at_rest_encryption_enabled ? var.kms_key_id : null
subnet_ids = var.subnets
security_group_ids = local.create_security_group ? concat(local.associated_security_group_ids, [module.aws_security_group.id]) : local.associated_security_group_ids
daily_snapshot_time = var.serverless_snapshot_time
description = coalesce(var.description, module.this.id)
major_engine_version = var.serverless_major_engine_version
snapshot_retention_limit = var.snapshot_retention_limit
user_group_id = var.serverless_user_group_id
dynamic "cache_usage_limits" {
for_each = try([var.serverless_cache_usage_limits], [])
content {
dynamic "data_storage" {
for_each = try([cache_usage_limits.value.data_storage], [])
content {
maximum = try(data_storage.value.maximum, null)
minimum = try(data_storage.value.minimum, null)
unit = try(data_storage.value.unit, "GB")
}
}
dynamic "ecpu_per_second" {
for_each = try([cache_usage_limits.value.ecpu_per_second], [])
content {
maximum = try(ecpu_per_second.value.maximum, null)
minimum = try(ecpu_per_second.value.minimum, null)
}
}
}
}
tags = module.this.tags
depends_on = [
aws_elasticache_parameter_group.default
]
}
#
# CloudWatch Resources
#
resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
count = local.create_normal_instance && 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]
tags = module.this.tags
}
resource "aws_cloudwatch_metric_alarm" "cache_memory" {
count = local.create_normal_instance && 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]
tags = module.this.tags
}
module "dns" {
source = "cloudposse/route53-cluster-hostname/aws"
version = "0.13.0"
enabled = local.enabled && length(var.zone_id) > 0 ? true : false
dns_name = var.dns_subdomain != "" ? var.dns_subdomain : module.this.id
ttl = 60
zone_id = try(var.zone_id[0], tostring(var.zone_id), "")
records = [local.endpoint_address]
context = module.this.context
}