|
1 | 1 | #
|
2 | 2 | # Security group resources
|
3 | 3 | #
|
4 |
| - |
5 | 4 | resource "aws_security_group" "redis" {
|
6 | 5 | vpc_id = "${var.vpc_id}"
|
7 | 6 |
|
8 | 7 | tags {
|
9 |
| - Name = "sgCacheCluster" |
| 8 | + Name = "sgCacheCluster" |
| 9 | + Project = "${var.project}" |
| 10 | + Environment = "${var.environment}" |
10 | 11 | }
|
11 | 12 | }
|
12 | 13 |
|
13 | 14 | #
|
14 | 15 | # ElastiCache resources
|
15 | 16 | #
|
16 |
| - |
17 |
| -resource "aws_elasticache_cluster" "redis" { |
18 |
| - cluster_id = "${var.cache_name}" |
19 |
| - engine = "redis" |
20 |
| - engine_version = "${var.engine_version}" |
21 |
| - maintenance_window = "${var.maintenance_window}" |
22 |
| - node_type = "${var.instance_type}" |
23 |
| - num_cache_nodes = "1" |
24 |
| - parameter_group_name = "default.redis2.8" |
25 |
| - port = "6379" |
26 |
| - subnet_group_name = "${aws_elasticache_subnet_group.default.name}" |
27 |
| - security_group_ids = ["${aws_security_group.redis.id}"] |
| 17 | +resource "aws_elasticache_replication_group" "redis" { |
| 18 | + replication_group_id = "${lower(var.cache_identifier)}" |
| 19 | + replication_group_description = "Replication group for Redis" |
| 20 | + automatic_failover_enabled = "${var.automatic_failover_enabled}" |
| 21 | + number_cache_clusters = "${var.desired_clusters}" |
| 22 | + node_type = "${var.instance_type}" |
| 23 | + engine_version = "${var.engine_version}" |
| 24 | + parameter_group_name = "${var.parameter_group}" |
| 25 | + subnet_group_name = "${var.subnet_group}" |
| 26 | + security_group_ids = ["${aws_security_group.redis.id}"] |
| 27 | + maintenance_window = "${var.maintenance_window}" |
| 28 | + notification_topic_arn = "${var.notification_topic_arn}" |
| 29 | + port = "6379" |
28 | 30 |
|
29 | 31 | tags {
|
30 |
| - Name = "CacheCluster" |
| 32 | + Name = "CacheReplicationGroup" |
| 33 | + Project = "${var.project}" |
| 34 | + Environment = "${var.environment}" |
31 | 35 | }
|
32 | 36 | }
|
33 | 37 |
|
34 |
| -resource "aws_elasticache_subnet_group" "default" { |
35 |
| - name = "${var.cache_name}-subnet-group" |
36 |
| - description = "Private subnets for the ElastiCache instances" |
37 |
| - subnet_ids = ["${split(",", var.private_subnet_ids)}"] |
38 |
| -} |
39 |
| - |
40 | 38 | #
|
41 | 39 | # CloudWatch resources
|
42 | 40 | #
|
| 41 | +resource "aws_cloudwatch_metric_alarm" "cache_cpu" { |
| 42 | + count = "${var.desired_clusters}" |
43 | 43 |
|
44 |
| -resource "aws_cloudwatch_metric_alarm" "cpu" { |
45 |
| - alarm_name = "alarmCacheClusterCPUUtilization-${var.cache_name}" |
46 |
| - alarm_description = "Cache cluster CPU utilization" |
| 44 | + alarm_name = "alarm${var.environment}CacheCluster00${count.index + 1}CPUUtilization" |
| 45 | + alarm_description = "Redis cluster CPU utilization" |
47 | 46 | comparison_operator = "GreaterThanThreshold"
|
48 | 47 | evaluation_periods = "1"
|
49 | 48 | metric_name = "CPUUtilization"
|
50 | 49 | namespace = "AWS/ElastiCache"
|
51 | 50 | period = "300"
|
52 | 51 | statistic = "Average"
|
53 |
| - threshold = "75" |
| 52 | + |
| 53 | + threshold = "${var.alarm_cpu_threshold}" |
54 | 54 |
|
55 | 55 | dimensions {
|
56 |
| - CacheClusterId = "${aws_elasticache_cluster.redis.id}" |
| 56 | + CacheClusterId = "${aws_elasticache_replication_group.redis.id}-00${count.index + 1}" |
57 | 57 | }
|
58 | 58 |
|
59 |
| - alarm_actions = ["${split(",", var.alarm_actions)}"] |
| 59 | + alarm_actions = ["${var.alarm_actions}"] |
60 | 60 | }
|
61 | 61 |
|
62 |
| -resource "aws_cloudwatch_metric_alarm" "memory_free" { |
63 |
| - alarm_name = "alarmCacheClusterFreeableMemory-${var.cache_name}" |
64 |
| - alarm_description = "Cache cluster freeable memory" |
| 62 | +resource "aws_cloudwatch_metric_alarm" "cache_memory" { |
| 63 | + count = "${var.desired_clusters}" |
| 64 | + |
| 65 | + alarm_name = "alarm${var.environment}CacheCluster00${count.index + 1}FreeableMemory" |
| 66 | + alarm_description = "Redis cluster freeable memory" |
65 | 67 | comparison_operator = "LessThanThreshold"
|
66 | 68 | evaluation_periods = "1"
|
67 | 69 | metric_name = "FreeableMemory"
|
68 | 70 | namespace = "AWS/ElastiCache"
|
69 | 71 | period = "60"
|
70 | 72 | statistic = "Average"
|
71 | 73 |
|
72 |
| - # 10MB in bytes |
73 |
| - threshold = "10000000" |
| 74 | + threshold = "${var.alarm_memory_threshold}" |
74 | 75 |
|
75 | 76 | dimensions {
|
76 |
| - CacheClusterId = "${aws_elasticache_cluster.redis.id}" |
| 77 | + CacheClusterId = "${aws_elasticache_replication_group.redis.id}-00${count.index + 1}" |
77 | 78 | }
|
78 | 79 |
|
79 |
| - alarm_actions = ["${split(",", var.alarm_actions)}"] |
| 80 | + alarm_actions = ["${var.alarm_actions}"] |
80 | 81 | }
|
0 commit comments