-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathelasticache.tf
108 lines (100 loc) · 5.13 KB
/
elasticache.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
//////////////////////////////////////////////////////////[ ELASTICACHE ]/////////////////////////////////////////////////
# # ---------------------------------------------------------------------------------------------------------------------#
# Create ElastiCache subnet group in our dedicated VPC
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_elasticache_subnet_group" "this" {
description = "ElastiCache Subnet for ${replace(local.project,"-"," ")}"
name = "${local.project}-elasticache-subnet"
subnet_ids = values(aws_subnet.this).*.id
tags = {
Name = "${local.project}-elasticache-subnet"
}
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create ElastiCache parameter groups
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_elasticache_parameter_group" "this" {
for_each = toset(var.redis["name"])
name = "${local.project}-${each.key}-parameter"
family = var.redis["family"]
description = "Parameter group for ${var.app["domain"]} ${each.key} backend"
dynamic "parameter" {
for_each = var.redis_parameters
content {
name = parameter.value["name"]
value = parameter.value["value"]
}
}
tags = {
Name = "${local.project}-${each.key}-parameter"
}
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create ElastiCache - Redis Replication group - session + cache
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_elasticache_replication_group" "this" {
for_each = toset(var.redis["name"])
description = "Replication group for ${var.app["domain"]} ${each.key} backend"
num_cache_clusters = var.redis["num_cache_clusters"]
at_rest_encryption_enabled = var.redis["at_rest_encryption_enabled"]
engine = "redis"
engine_version = var.redis["engine_version"]
replication_group_id = "${local.project}-${each.key}-backend"
node_type = var.redis["node_type"]
port = var.redis["port"]
parameter_group_name = aws_elasticache_parameter_group.this[each.key].id
security_group_ids = [aws_security_group.redis.id]
subnet_group_name = aws_elasticache_subnet_group.this.name
automatic_failover_enabled = var.redis["num_cache_clusters"] > 1 ? true : false
multi_az_enabled = var.redis["num_cache_clusters"] > 1 ? true : false
notification_topic_arn = aws_sns_topic.default.arn
transit_encryption_enabled = true
auth_token = random_password.this["redis"].result
auth_token_update_strategy = "ROTATE"
lifecycle {
ignore_changes = [num_cache_clusters]
}
tags = {
Name = "${local.project}-${each.key}-backend"
}
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create CloudWatch CPU Utilization metrics and email alerts
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_cloudwatch_metric_alarm" "elasticache_cpu" {
for_each = aws_elasticache_replication_group.this
alarm_name = "${local.project}-elasticache-${each.key}-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 = 80
alarm_actions = ["${aws_sns_topic.default.arn}"]
ok_actions = ["${aws_sns_topic.default.arn}"]
dimensions = {
CacheClusterId = aws_elasticache_replication_group.this[each.key].id
}
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create CloudWatch Freeable Memory metrics and email alerts
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_cloudwatch_metric_alarm" "elasticache_memory" {
for_each = aws_elasticache_replication_group.this
alarm_name = "${local.project}-elasticache-${each.key}-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 = 10000000
alarm_actions = ["${aws_sns_topic.default.arn}"]
ok_actions = ["${aws_sns_topic.default.arn}"]
dimensions = {
CacheClusterId = aws_elasticache_replication_group.this[each.key].id
}
}