diff --git a/data.tf b/data.tf new file mode 100644 index 0000000..af01986 --- /dev/null +++ b/data.tf @@ -0,0 +1,33 @@ +locals { + subnet_tag_name = "Type" +} + +data "aws_vpc" "selected" { + filter { + name = "tag:${var.vpc_tag_name}" + values = ["${var.vpc_name}${var.vpc_tag_value}"] + } +} + +data "aws_subnet_ids" "private" { + vpc_id = "${data.aws_vpc.selected.id}" + + filter { + name ="tag:${var.vpc_subnet_tag_name}" + values = ["${var.vpc_subnet_tag_value}"] + } + +} + +data "aws_security_group" "default" { + // This is to use our existing security group for access from the hopper + filter { + name = "${var.sg_filter_name}" + values = ["${var.sg_filter_value}"] + } + + filter { + name = "vpc-id" + values = ["${data.aws_vpc.selected.id}"] + } +} diff --git a/main.tf b/main.tf index cd327d2..5809176 100644 --- a/main.tf +++ b/main.tf @@ -11,6 +11,13 @@ resource "aws_security_group" "redis" { } } +resource "aws_elasticache_subnet_group" "default" { + count = "${var.enabled == "true" ? 1 : 0}" + name = "subnet-group-${lower(var.cache_identifier)}" + description = "Private subnets for the ElastiCache instances: ${lower(var.cache_identifier)}" + subnet_ids = ["${data.aws_subnet_ids.private.ids}"] +} + # # ElastiCache resources # @@ -22,8 +29,8 @@ resource "aws_elasticache_replication_group" "redis" { node_type = "${var.instance_type}" engine_version = "${var.engine_version}" parameter_group_name = "${var.parameter_group}" - subnet_group_name = "${var.subnet_group}" - security_group_ids = ["${aws_security_group.redis.id}"] + subnet_group_name = "${var.subnet_group == "" ? aws_elasticache_subnet_group.default.name : var.subnet_group }" + security_group_ids = ["${aws_security_group.redis.id},${data.aws_security_group.default.id}"] maintenance_window = "${var.maintenance_window}" notification_topic_arn = "${var.notification_topic_arn}" port = "6379" diff --git a/variables.tf b/variables.tf index 84bb11a..fa4103d 100644 --- a/variables.tf +++ b/variables.tf @@ -8,15 +8,40 @@ variable "environment" { variable "vpc_id" {} +variable "vpc_name" { + description = "Provide vpc name for your region" +} + +variable "vpc_tag_name" {} +variable "vpc_tag_value" {} +variable "vpc_subnet_tag_name" {} +variable "vpc_subnet_tag_value" {} + +variable "sg_filter_name" { + default = "description" +} + +variable "sg_filter_value" { + default = "" +} + +variable "enabled" { + description = "Set to false to prevent the module from creating any resources" + default = "true" +} + variable "cache_identifier" {} variable "parameter_group" { default = "default.redis3.2" } -variable "subnet_group" {} +variable "subnet_group" { + default = "cloud-elast-19f609hoj8an" +} -variable "maintenance_window" {} +variable "maintenance_window" { +} variable "desired_clusters" { default = "1" @@ -34,7 +59,8 @@ variable "automatic_failover_enabled" { default = false } -variable "notification_topic_arn" {} +variable "notification_topic_arn" { +} variable "alarm_cpu_threshold" { default = "75" @@ -46,5 +72,5 @@ variable "alarm_memory_threshold" { } variable "alarm_actions" { - type = "list" + type = "list" }