forked from cloudposse/terraform-aws-elasticache-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
130 lines (112 loc) · 4.31 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
# Define composite variables for resources
module "label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.5.3"
enabled = "${var.enabled}"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
delimiter = "${var.delimiter}"
attributes = "${var.attributes}"
tags = "${var.tags}"
}
#
# Security Group Resources
#
resource "aws_security_group" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
vpc_id = "${var.vpc_id}"
name = "${module.label.id}"
ingress {
from_port = "${var.port}" # Redis
to_port = "${var.port}"
protocol = "tcp"
security_groups = ["${var.security_groups}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = "${module.label.tags}"
}
resource "aws_elasticache_subnet_group" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${module.label.id}"
subnet_ids = ["${var.subnets}"]
}
resource "aws_elasticache_parameter_group" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${module.label.id}"
family = "${var.family}"
parameter = "${var.parameter}"
}
resource "aws_elasticache_replication_group" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
auth_token = "${var.auth_token}"
replication_group_id = "${var.replication_group_id == "" ? module.label.id : var.replication_group_id}"
replication_group_description = "${module.label.id}"
node_type = "${var.instance_type}"
number_cache_clusters = "${var.cluster_size}"
port = "${var.port}"
parameter_group_name = "${aws_elasticache_parameter_group.default.name}"
availability_zones = ["${slice(var.availability_zones, 0, var.cluster_size)}"]
automatic_failover_enabled = "${var.automatic_failover}"
subnet_group_name = "${aws_elasticache_subnet_group.default.name}"
security_group_ids = ["${aws_security_group.default.id}"]
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.transit_encryption_enabled}"
tags = "${module.label.tags}"
}
#
# CloudWatch Resources
#
resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
count = "${var.enabled == "true" ? 1 : 0}"
alarm_name = "${module.label.id}-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 = "${module.label.id}"
}
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 = "${var.enabled == "true" ? 1 : 0}"
alarm_name = "${module.label.id}-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 = "${module.label.id}"
}
alarm_actions = ["${var.alarm_actions}"]
ok_actions = ["${var.ok_actions}"]
depends_on = ["aws_elasticache_replication_group.default"]
}
module "dns" {
source = "git::https://github.com/cloudposse/terraform-aws-route53-cluster-hostname.git?ref=tags/0.2.1"
enabled = "${var.enabled == "true" && length(var.zone_id) > 0 ? "true" : "false"}"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
ttl = 60
zone_id = "${var.zone_id}"
records = ["${aws_elasticache_replication_group.default.*.primary_endpoint_address}"]
}