Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ variable "availability_zones" {
variable "zone_id" {}

module "vpc" {
source = "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=tags/0.3.3"
source = "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=master"
namespace = "${var.namespace}"
stage = "${var.stage}"
name = "${var.name}"
}

module "subnets" {
source = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=tags/0.3.5"
source = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=master"
namespace = "${var.namespace}"
stage = "${var.stage}"
name = "${var.name}"
Expand Down
139 changes: 75 additions & 64 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# 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}"
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.14.1"
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}"
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}"
from_port = var.port # Redis
to_port = var.port
protocol = "tcp"
security_groups = ["${var.security_groups}"]
security_groups = var.security_groups
}

egress {
Expand All @@ -32,54 +32,64 @@ resource "aws_security_group" "default" {
cidr_blocks = ["0.0.0.0/0"]
}

tags = "${module.label.tags}"
tags = module.label.tags
}

locals {
elasticache_subnet_group_name = "${var.elasticache_subnet_group_name != "" ? var.elasticache_subnet_group_name : join("", aws_elasticache_subnet_group.default.*.name) }"
elasticache_subnet_group_name = var.elasticache_subnet_group_name != "" ? var.elasticache_subnet_group_name : join("", aws_elasticache_subnet_group.default.*.name)
}

resource "aws_elasticache_subnet_group" "default" {
count = "${var.enabled == "true" && var.elasticache_subnet_group_name == "" && length(var.subnets) > 0 ? 1 : 0}"
name = "${module.label.id}"
subnet_ids = ["${var.subnets}"]
count = var.enabled == "true" && var.elasticache_subnet_group_name == "" && length(var.subnets) > 0 ? 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}"
count = var.enabled == "true" ? 1 : 0
name = module.label.id
family = var.family
dynamic "parameter" {
for_each = var.parameter
content {
# TF-UPGRADE-TODO: The automatic upgrade tool can't predict
# which keys might be set in maps assigned here, so it has
# produced a comprehensive set here. Consider simplifying
# this after confirming which keys can be set in practice.

name = parameter.value.name
value = parameter.value.value
}
}
}

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 = "${local.elasticache_subnet_group_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}"
count = var.enabled == "true" ? 1 : 0

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[0].name
availability_zones = slice(var.availability_zones, 0, var.cluster_size)
automatic_failover_enabled = var.automatic_failover
subnet_group_name = local.elasticache_subnet_group_name
security_group_ids = [aws_security_group.default[0].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}"
count = var.enabled == "true" ? 1 : 0
alarm_name = "${module.label.id}-cpu-utilization"
alarm_description = "Redis cluster CPU utilization"
comparison_operator = "GreaterThanThreshold"
Expand All @@ -89,19 +99,19 @@ resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
period = "300"
statistic = "Average"

threshold = "${var.alarm_cpu_threshold_percent}"
threshold = var.alarm_cpu_threshold_percent

dimensions {
CacheClusterId = "${module.label.id}"
dimensions = {
CacheClusterId = module.label.id
}

alarm_actions = ["${var.alarm_actions}"]
ok_actions = ["${var.ok_actions}"]
depends_on = ["aws_elasticache_replication_group.default"]
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}"
count = var.enabled == "true" ? 1 : 0
alarm_name = "${module.label.id}-freeable-memory"
alarm_description = "Redis cluster freeable memory"
comparison_operator = "LessThanThreshold"
Expand All @@ -111,24 +121,25 @@ resource "aws_cloudwatch_metric_alarm" "cache_memory" {
period = "60"
statistic = "Average"

threshold = "${var.alarm_memory_threshold_bytes}"
threshold = var.alarm_memory_threshold_bytes

dimensions {
CacheClusterId = "${module.label.id}"
dimensions = {
CacheClusterId = module.label.id
}

alarm_actions = ["${var.alarm_actions}"]
ok_actions = ["${var.ok_actions}"]
depends_on = ["aws_elasticache_replication_group.default"]
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.6"
enabled = "${var.enabled == "true" && length(var.zone_id) > 0 ? "true" : "false"}"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
source = "git::https://github.com/rverma-nikiai/terraform-aws-route53-cluster-hostname.git?ref=master"
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}"]
zone_id = var.zone_id
records = aws_elasticache_replication_group.default.*.primary_endpoint_address
}

15 changes: 11 additions & 4 deletions output.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
output "id" {
value = "${join("", aws_elasticache_replication_group.default.*.id)}"
value = join("", aws_elasticache_replication_group.default.*.id)
description = "Redis cluster ID"
}

output "security_group_id" {
value = "${join("", aws_security_group.default.*.id)}"
value = join("", aws_security_group.default.*.id)
description = "Security group ID"
}

output "port" {
value = "${var.port}"
value = var.port
description = "Redis port"
}

output "host" {
value = "${coalesce(module.dns.hostname, join("", aws_elasticache_replication_group.default.*.primary_endpoint_address))}"
value = coalesce(
module.dns.hostname,
join(
"",
aws_elasticache_replication_group.default.*.primary_endpoint_address,
),
)
description = "Redis host"
}

25 changes: 13 additions & 12 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ variable "name" {
}

variable "security_groups" {
type = "list"
type = list(string)
default = []
description = "AWS security group ids"
}
Expand All @@ -29,13 +29,13 @@ variable "vpc_id" {
}

variable "subnets" {
type = "list"
type = list(string)
description = "AWS subnet IDs"
default = []
}

variable "elasticache_subnet_group_name" {
type = "string"
type = string
description = "Subnet group name for the ElastiCache instance"
default = ""
}
Expand Down Expand Up @@ -66,7 +66,7 @@ variable "family" {
}

variable "parameter" {
type = "list"
type = list(string)
default = []
description = "A list of Redis parameters to apply. Note that parameters may differ from one Redis family to another"
}
Expand Down Expand Up @@ -103,13 +103,13 @@ variable "alarm_memory_threshold_bytes" {
}

variable "alarm_actions" {
type = "list"
type = list(string)
description = "Alarm action list"
default = []
}

variable "ok_actions" {
type = "list"
type = list(string)
description = "The list of actions to execute when this alarm transitions into an OK state from any other state. Each action is specified as an Amazon Resource Number (ARN)"
default = []
}
Expand All @@ -125,7 +125,7 @@ variable "automatic_failover" {
}

variable "availability_zones" {
type = "list"
type = list(string)
description = "Availability zone ids"
default = []
}
Expand All @@ -136,31 +136,32 @@ variable "zone_id" {
}

variable "delimiter" {
type = "string"
type = string
default = "-"
description = "Delimiter between `name`, `namespace`, `stage` and `attributes`"
}

variable "attributes" {
type = "list"
type = list(string)
description = "Additional attributes (_e.g._ \"1\")"
default = []
}

variable "tags" {
type = "map"
type = map(string)
description = "Additional tags (_e.g._ map(\"BusinessUnit\",\"ABC\")"
default = {}
}

variable "auth_token" {
type = "string"
type = string
description = "Auth token for password protecting redis, transit_encryption_enabled must be set to 'true'! Password must be longer than 16 chars"
default = ""
}

variable "replication_group_id" {
type = "string"
type = string
description = "Replication group ID with the following constraints: \nA name must contain from 1 to 20 alphanumeric characters or hyphens. \n The first character must be a letter. \n A name cannot end with a hyphen or contain two consecutive hyphens."
default = ""
}

4 changes: 4 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}