Skip to content

Commit e513149

Browse files
authored
Init (#1)
* Initial * Added redis * Added redis * Added redis * Added redis * Use default param group * Use default param group * Use default param group * Fix redis * Fix redis * Fix redis * Fix redis * Fix redis * Fix redis * Fix redis * Fix redis * Fix redis * Fix redis * Fix default value * Fix default value * Update main.tf * Address PR comments * Update LICENSE * Update README.md * Update LICENSE * Update README.md
1 parent 30b0152 commit e513149

File tree

5 files changed

+282
-3
lines changed

5 files changed

+282
-3
lines changed

LICENSE

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
Apache License
23
Version 2.0, January 2004
34
http://www.apache.org/licenses/
@@ -178,15 +179,15 @@
178179
APPENDIX: How to apply the Apache License to your work.
179180

180181
To apply the Apache License to your work, attach the following
181-
boilerplate notice, with the fields enclosed by brackets "{}"
182+
boilerplate notice, with the fields enclosed by brackets "[]"
182183
replaced with your own identifying information. (Don't include
183184
the brackets!) The text should be enclosed in the appropriate
184185
comment syntax for the file format. We also recommend that a
185186
file or class name and description of purpose be included on the
186187
same "printed page" as the copyright notice for easier
187188
identification within third-party archives.
188189

189-
Copyright {yyyy} {name of copyright owner}
190+
Copyright 2017 Cloud Posse, LLC
190191

191192
Licensed under the Apache License, Version 2.0 (the "License");
192193
you may not use this file except in compliance with the License.

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
1-
# tf_redis
1+
# tf_redis
2+
3+
Terraform module to provision a ElastiCache Redis Cluster
4+
5+
## Usage
6+
7+
Include this repository as a module in your existing terraform code:
8+
9+
```
10+
module "example_redis" {
11+
source = "git::https://github.com/cloudposse/tf_redis.git?ref=tags/0.1.0"
12+
namespace = "general"
13+
name = "redis"
14+
stage = "prod"
15+
zone_id = "${var.route52_zone_id}"
16+
security_groups = ["${var.security_group_id}"]
17+
18+
vpc_id = "${var.vpc_id}"
19+
subnets = "${var.private_subnets}"
20+
maintenance_window = "wed:03:00-wed:04:00"
21+
cluster_size = "2"
22+
instance_type = "cache.t2.micro"
23+
engine_version = "3.2.4"
24+
alarm_cpu_threshold_percent = "${var.cache_alarm_cpu_threshold_percent}"
25+
alarm_memory_threshold_bytes = "${var.cache_alarm_memory_threshold_bytes}"
26+
apply_immediately = "true"
27+
availability_zones = "${var.availability_zones}"
28+
29+
automatic_failover = "false"
30+
}
31+
```
32+
33+
## Input
34+
35+
| Name | Default | Decription |
36+
|:----------------------------:|:-------------------:|:------------------------------------------------------:|
37+
| namespace |global |Namespace |
38+
| stage |default |Stage |
39+
| name |redis |Name |
40+
| security_groups |[] |AWS security group ids |
41+
| vpc_id |__REQUIRED__ |AWS VPC id |
42+
| subnets | [] | AWS subnet ids |
43+
| cluster_size | 1 | Count of nodes in cluster |
44+
| instance_type | cache.t2.micro | Elastic cache instance type |
45+
| family | redis3.2 | Redis family |
46+
| engine_version | 3.2.4 | Redis engine version |
47+
| port | 6379 | Redis port |
48+
| maintenance_window | wed:03:00-wed:04:00 | Maintenance window |
49+
| notification_topic_arn | | Notificate topic arn |
50+
| alarm_cpu_threshold_percent | 75 | CPU threshold alarm level |
51+
| alarm_memory_threshold_bytes | 10000000 | Ram threshold alarm level |
52+
| alarm_actions | [] | Alarm action list |
53+
| apply_immediately | true | Apply changes immediately |
54+
| automatic_failover | false | Automatic failover (Not available for T1/T2 instances) |
55+
| availability_zones | [] | Availability zone ids |
56+
| zone_id | false | Route53 dns zone id |
57+
58+
## Output
59+
60+
| Name | Decription |
61+
|:-----------------:|:-----------------:|
62+
| id | Redis cluster id |
63+
| security_group_id | Security group id |
64+
| host | Redis host |
65+
| port | Redis port |
66+

main.tf

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}

output.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
output "id" {
2+
value = "${aws_elasticache_replication_group.default.id}"
3+
}
4+
5+
output "security_group_id" {
6+
value = "${aws_security_group.default.id}"
7+
}
8+
9+
output "port" {
10+
value = "11211"
11+
}
12+
13+
output "host" {
14+
value = "${module.dns.hostname}"
15+
}
16+

variables.tf

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
variable "namespace" {
2+
default = "global"
3+
}
4+
5+
variable "stage" {
6+
default = "default"
7+
}
8+
9+
variable "name" {
10+
default = "redis"
11+
}
12+
13+
variable "security_groups" {
14+
type = "list"
15+
}
16+
17+
variable "vpc_id" {
18+
default = ""
19+
}
20+
21+
variable "subnets" {
22+
type = "list"
23+
default = []
24+
}
25+
26+
variable "maintenance_window" {
27+
default = "wed:03:00-wed:04:00"
28+
}
29+
30+
variable "cluster_size" {
31+
default = "1"
32+
}
33+
34+
35+
variable "port" {
36+
default = "6379"
37+
}
38+
39+
40+
variable "instance_type" {
41+
default = "cache.t2.micro"
42+
}
43+
44+
variable "family" {
45+
default = "redis3.2"
46+
}
47+
48+
variable "engine_version" {
49+
default = "3.2.4"
50+
}
51+
52+
variable "notification_topic_arn" {
53+
default = ""
54+
}
55+
56+
variable "alarm_cpu_threshold_percent" {
57+
default = "75"
58+
}
59+
60+
variable "alarm_memory_threshold_bytes" {
61+
# 10MB
62+
default = "10000000"
63+
}
64+
65+
variable "alarm_actions" {
66+
type = "list"
67+
default = []
68+
}
69+
70+
variable "apply_immediately" {
71+
default = "true"
72+
}
73+
74+
variable "automatic_failover" {
75+
default = "false"
76+
}
77+
78+
variable "availability_zones" {
79+
type = "list"
80+
}
81+
82+
variable "zone_id" {}

0 commit comments

Comments
 (0)