|
1 | 1 | #
|
2 |
| -# ElastiCache resources |
| 2 | +# Security group resources |
3 | 3 | #
|
4 |
| - |
5 |
| -resource "aws_elasticache_cluster" "redis" { |
6 |
| - cluster_id = "${var.cache_name}" |
7 |
| - engine = "redis" |
8 |
| - engine_version = "${var.engine_version}" |
9 |
| - maintenance_window = "${var.maintenance_window}" |
10 |
| - node_type = "${var.instance_type}" |
11 |
| - num_cache_nodes = "1" |
12 |
| - parameter_group_name = "default.redis2.8" |
13 |
| - port = "6379" |
14 |
| - subnet_group_name = "${aws_elasticache_subnet_group.default.name}" |
15 |
| - security_group_ids = "${var.security_group_ids}" |
| 4 | +resource "aws_security_group" "redis" { |
| 5 | + vpc_id = "${var.vpc_id}" |
16 | 6 |
|
17 | 7 | tags {
|
18 |
| - Name = "${var.cache_name}" |
| 8 | + Name = "sgCacheCluster" |
| 9 | + Project = "${var.project}" |
| 10 | + Environment = "${var.environment}" |
19 | 11 | }
|
20 | 12 | }
|
21 | 13 |
|
22 |
| -resource "aws_elasticache_subnet_group" "default" { |
23 |
| - name = "${var.cache_name}-subnet-group" |
24 |
| - description = "Private subnets for the ElastiCache instances" |
25 |
| - subnet_ids = "${var.private_subnet_ids}" |
| 14 | +# |
| 15 | +# ElastiCache resources |
| 16 | +# |
| 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" |
| 30 | + |
| 31 | + tags { |
| 32 | + Name = "CacheReplicationGroup" |
| 33 | + Project = "${var.project}" |
| 34 | + Environment = "${var.environment}" |
| 35 | + } |
26 | 36 | }
|
27 | 37 |
|
28 | 38 | #
|
29 | 39 | # CloudWatch resources
|
30 | 40 | #
|
31 |
| -resource "aws_cloudwatch_metric_alarm" "cpu" { |
32 |
| - alarm_name = "alarmCacheClusterCPUUtilization-${var.cache_name}" |
33 |
| - alarm_description = "Cache cluster CPU utilization" |
| 41 | +resource "aws_cloudwatch_metric_alarm" "cache_cpu" { |
| 42 | + count = "${var.desired_clusters}" |
| 43 | + |
| 44 | + alarm_name = "alarm${var.environment}CacheCluster00${count.index + 1}CPUUtilization" |
| 45 | + alarm_description = "Redis cluster CPU utilization" |
34 | 46 | comparison_operator = "GreaterThanThreshold"
|
35 | 47 | evaluation_periods = "1"
|
36 | 48 | metric_name = "CPUUtilization"
|
37 | 49 | namespace = "AWS/ElastiCache"
|
38 | 50 | period = "300"
|
39 | 51 | statistic = "Average"
|
40 |
| - threshold = "75" |
| 52 | + |
| 53 | + threshold = "${var.alarm_cpu_threshold}" |
41 | 54 |
|
42 | 55 | dimensions {
|
43 |
| - CacheClusterName = "${var.cache_name}" |
| 56 | + CacheClusterId = "${aws_elasticache_replication_group.redis.id}-00${count.index + 1}" |
44 | 57 | }
|
45 | 58 |
|
46 |
| - # for some reason trying to pass a list here causes the error ` * aws_cloudwatch_metric_alarm.cpu: alarm_actions: should be a list` in terraform 0.7.5 |
47 |
| - # passing lists elsewhere works fine |
48 |
| - alarm_actions = ["${var.alarm_action}"] |
| 59 | + alarm_actions = ["${var.alarm_actions}"] |
49 | 60 | }
|
50 | 61 |
|
51 |
| -resource "aws_cloudwatch_metric_alarm" "memory_free" { |
52 |
| - alarm_name = "alarmCacheClusterFreeableMemory-${var.cache_name}" |
53 |
| - 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" |
54 | 67 | comparison_operator = "LessThanThreshold"
|
55 | 68 | evaluation_periods = "1"
|
56 | 69 | metric_name = "FreeableMemory"
|
57 | 70 | namespace = "AWS/ElastiCache"
|
58 | 71 | period = "60"
|
59 | 72 | statistic = "Average"
|
60 | 73 |
|
61 |
| - # 10MB in bytes |
62 |
| - threshold = "10000000" |
| 74 | + threshold = "${var.alarm_memory_threshold}" |
63 | 75 |
|
64 | 76 | dimensions {
|
65 |
| - CacheClusterName = "${var.cache_name}" |
| 77 | + CacheClusterId = "${aws_elasticache_replication_group.redis.id}-00${count.index + 1}" |
66 | 78 | }
|
67 | 79 |
|
68 |
| - # for some reason trying to pass a list here causes the error ` * aws_cloudwatch_metric_alarm.cpu: alarm_actions: should be a list` in terraform 0.7.5 |
69 |
| - # passing lists elsewhere works fine |
70 |
| - alarm_actions = ["${var.alarm_action}"] |
| 80 | + alarm_actions = ["${var.alarm_actions}"] |
71 | 81 | }
|
0 commit comments