|
| 1 | +# Define composite variables for resources |
| 2 | +module "label" { |
| 3 | + source = "git::https://github.com/cloudposse/tf_label.git?ref=tags/0.1.0" |
| 4 | + namespace = "${var.namespace}" |
| 5 | + name = "${var.name}" |
| 6 | + stage = "${var.stage}" |
| 7 | +} |
| 8 | + |
| 9 | +# |
| 10 | +# Security Group Resources |
| 11 | +# |
| 12 | +resource "aws_security_group" "default" { |
| 13 | + vpc_id = "${var.vpc_id}" |
| 14 | + name = "${module.label.id}" |
| 15 | + ingress { |
| 16 | + from_port = "${var.port}" # Redis |
| 17 | + to_port = "${var.port}" |
| 18 | + protocol = "tcp" |
| 19 | + security_groups = ["${var.security_groups}"] |
| 20 | + } |
| 21 | + |
| 22 | + egress { |
| 23 | + from_port = 0 |
| 24 | + to_port = 0 |
| 25 | + protocol = "-1" |
| 26 | + cidr_blocks = ["0.0.0.0/0"] |
| 27 | + } |
| 28 | + |
| 29 | + tags { |
| 30 | + Name = "${module.label.id}" |
| 31 | + Namespace = "${var.namespace}" |
| 32 | + Stage = "${var.stage}" |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +resource "aws_elasticache_subnet_group" "default" { |
| 37 | + name = "${module.label.id}" |
| 38 | + subnet_ids = ["${var.subnets}"] |
| 39 | +} |
| 40 | + |
| 41 | +resource "aws_elasticache_parameter_group" "default" { |
| 42 | + name = "${module.label.id}" |
| 43 | + family = "${var.family}" |
| 44 | +} |
| 45 | + |
| 46 | +resource "aws_elasticache_replication_group" "default" { |
| 47 | + replication_group_id = "${module.label.id}" |
| 48 | + replication_group_description = "${module.label.id}" |
| 49 | + node_type = "${var.instance_type}" |
| 50 | + number_cache_clusters = "${var.cluster_size}" |
| 51 | + port = "${var.port}" |
| 52 | + parameter_group_name = "${aws_elasticache_parameter_group.default.name}" |
| 53 | + availability_zones = ["${slice(var.availability_zones, 0, var.cluster_size)}"] |
| 54 | + automatic_failover_enabled = "${var.automatic_failover}" |
| 55 | + subnet_group_name = "${aws_elasticache_subnet_group.default.name}" |
| 56 | + security_group_ids = ["${aws_security_group.default.id}"] |
| 57 | + maintenance_window = "${var.maintenance_window}" |
| 58 | + notification_topic_arn = "${var.notification_topic_arn}" |
| 59 | + |
| 60 | + tags = "${module.label.tags}" |
| 61 | +} |
| 62 | + |
| 63 | +# |
| 64 | +# CloudWatch Resources |
| 65 | +# |
| 66 | +resource "aws_cloudwatch_metric_alarm" "cache_cpu" { |
| 67 | + alarm_name = "${module.label.id}-cpu-utilization" |
| 68 | + alarm_description = "Redis cluster CPU utilization" |
| 69 | + comparison_operator = "GreaterThanThreshold" |
| 70 | + evaluation_periods = "1" |
| 71 | + metric_name = "CPUUtilization" |
| 72 | + namespace = "AWS/ElastiCache" |
| 73 | + period = "300" |
| 74 | + statistic = "Average" |
| 75 | + |
| 76 | + threshold = "${var.alarm_cpu_threshold_percent}" |
| 77 | + |
| 78 | + dimensions { |
| 79 | + CacheClusterId = "${module.label.id}" |
| 80 | + } |
| 81 | + |
| 82 | + alarm_actions = ["${var.alarm_actions}"] |
| 83 | + depends_on = ["aws_elasticache_replication_group.default"] |
| 84 | +} |
| 85 | + |
| 86 | +resource "aws_cloudwatch_metric_alarm" "cache_memory" { |
| 87 | + alarm_name = "${module.label.id}-freeable-memory" |
| 88 | + alarm_description = "Redis cluster freeable memory" |
| 89 | + comparison_operator = "LessThanThreshold" |
| 90 | + evaluation_periods = "1" |
| 91 | + metric_name = "FreeableMemory" |
| 92 | + namespace = "AWS/ElastiCache" |
| 93 | + period = "60" |
| 94 | + statistic = "Average" |
| 95 | + |
| 96 | + threshold = "${var.alarm_memory_threshold_bytes}" |
| 97 | + |
| 98 | + dimensions { |
| 99 | + CacheClusterId = "${module.label.id}" |
| 100 | + } |
| 101 | + |
| 102 | + alarm_actions = ["${var.alarm_actions}"] |
| 103 | + depends_on = ["aws_elasticache_replication_group.default"] |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +module "dns" { |
| 108 | + source = "git::https://github.com/cloudposse/tf_hostname.git?ref=tags/0.1.0" |
| 109 | + namespace = "${var.namespace}" |
| 110 | + name = "${var.name}" |
| 111 | + stage = "${var.stage}" |
| 112 | + ttl = 60 |
| 113 | + zone_id = "${var.zone_id}" |
| 114 | + records = ["${aws_elasticache_replication_group.default.primary_endpoint_address}"] |
| 115 | +} |
0 commit comments