Commit 307e6300 authored by Josh Myers's avatar Josh Myers Committed by Andriy Knysh

Add Optional `elasticache_subnet_group` (#34)

* [issue-30] Optional elasticache_subnet_group ID

Add optional variable for the Elasticache subnet group ID so we can 
create the replication group in an already existing subnet group.

* Update main.tf
Co-Authored-By: default avataraknysh <aknysh@users.noreply.github.com>

* Update variables.tf
Co-Authored-By: default avataraknysh <aknysh@users.noreply.github.com>

* Update variables.tf
Co-Authored-By: default avataraknysh <aknysh@users.noreply.github.com>

* Update main.tf
Co-Authored-By: default avataraknysh <aknysh@users.noreply.github.com>

* Address comments, update LICENSE, update descriptions, update `README`

* Update example

* Update README
parent 1841eaae
...@@ -9,8 +9,8 @@ install: ...@@ -9,8 +9,8 @@ install:
- make init - make init
script: script:
- make terraform:install - make terraform/install
- make terraform:get-plugins - make terraform/get-plugins
- make terraform:get-modules - make terraform/get-modules
- make terraform:lint - make terraform/lint
- make terraform:validate - make terraform/validate
...@@ -187,7 +187,7 @@ ...@@ -187,7 +187,7 @@
same "printed page" as the copyright notice for easier same "printed page" as the copyright notice for easier
identification within third-party archives. identification within third-party archives.
Copyright 2017 Cloud Posse, LLC Copyright 2017-2019 Cloud Posse, LLC
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
......
...@@ -42,7 +42,11 @@ We literally have [*hundreds of terraform modules*][terraform_modules] that are ...@@ -42,7 +42,11 @@ We literally have [*hundreds of terraform modules*][terraform_modules] that are
## Usage ## Usage
Include this repository as a module in your existing terraform code:
**IMPORTANT:** The `master` branch is used in `source` just as an example. In your code, do not pin to `master` because there may be breaking changes between releases.
Instead pin to the release tag (e.g. `?ref=tags/x.y.z`) of one of our [latest releases](https://github.com/cloudposse/terraform-aws-elasticache-redis/releases).
```hcl ```hcl
// Generate a random string for auth token, no special chars // Generate a random string for auth token, no special chars
...@@ -113,6 +117,7 @@ Available targets: ...@@ -113,6 +117,7 @@ Available targets:
| availability_zones | Availability zone ids | list | `<list>` | no | | availability_zones | Availability zone ids | list | `<list>` | no |
| cluster_size | Count of nodes in cluster | string | `1` | no | | cluster_size | Count of nodes in cluster | string | `1` | no |
| delimiter | Delimiter between `name`, `namespace`, `stage` and `attributes` | string | `-` | no | | delimiter | Delimiter between `name`, `namespace`, `stage` and `attributes` | string | `-` | no |
| elasticache_subnet_group_name | Subnet group name for the ElastiCache instance | string | `` | no |
| enabled | Set to false to prevent the module from creating any resources | string | `true` | no | | enabled | Set to false to prevent the module from creating any resources | string | `true` | no |
| engine_version | Redis engine version | string | `4.0.10` | no | | engine_version | Redis engine version | string | `4.0.10` | no |
| family | Redis family | string | `redis4.0` | no | | family | Redis family | string | `redis4.0` | no |
...@@ -127,10 +132,10 @@ Available targets: ...@@ -127,10 +132,10 @@ Available targets:
| replication_group_id | Replication group ID with the following constraints: A name must contain from 1 to 20 alphanumeric characters or hyphens. The first character must be a letter. A name cannot end with a hyphen or contain two consecutive hyphens. | string | `` | no | | replication_group_id | Replication group ID with the following constraints: A name must contain from 1 to 20 alphanumeric characters or hyphens. The first character must be a letter. A name cannot end with a hyphen or contain two consecutive hyphens. | string | `` | no |
| security_groups | AWS security group ids | list | `<list>` | no | | security_groups | AWS security group ids | list | `<list>` | no |
| stage | Stage | string | `default` | no | | stage | Stage | string | `default` | no |
| subnets | AWS subnet ids | list | `<list>` | no | | subnets | AWS subnet IDs | list | `<list>` | no |
| tags | Additional tags (_e.g._ map("BusinessUnit","ABC") | map | `<map>` | no | | tags | Additional tags (_e.g._ map("BusinessUnit","ABC") | map | `<map>` | no |
| transit_encryption_enabled | Enable TLS | string | `true` | no | | transit_encryption_enabled | Enable TLS | string | `true` | no |
| vpc_id | AWS VPC id | string | `REQUIRED` | no | | vpc_id | AWS VPC id | string | - | yes |
| zone_id | Route53 DNS Zone id | string | `` | no | | zone_id | Route53 DNS Zone id | string | `` | no |
## Outputs ## Outputs
...@@ -138,9 +143,9 @@ Available targets: ...@@ -138,9 +143,9 @@ Available targets:
| Name | Description | | Name | Description |
|------|-------------| |------|-------------|
| host | Redis host | | host | Redis host |
| id | Redis cluster id | | id | Redis cluster ID |
| port | Redis port | | port | Redis port |
| security_group_id | Security group id | | security_group_id | Security group ID |
...@@ -207,7 +212,7 @@ In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow. ...@@ -207,7 +212,7 @@ In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.
## Copyright ## Copyright
Copyright © 2017-2018 [Cloud Posse, LLC](https://cpco.io/copyright) Copyright © 2017-2019 [Cloud Posse, LLC](https://cpco.io/copyright)
......
--- ---
# #
# This is the canonical configuration for the `README.md` # This is the canonical configuration for the `README.md`
# Run `make readme` to rebuild the `README.md` # Run `make readme` to rebuild the `README.md`
# #
# Name of this project # Name of this project
name: terraform-aws-elasticache-redis name: terraform-aws-elasticache-redis
# Tags of this project # Tags of this project
tags: tags:
- aws - aws
- terraform - terraform
- terraform-modules - terraform-modules
- databases - databases
- redis - redis
- elasticache - elasticache
- cache - cache
# Categories of this project # Categories of this project
categories: categories:
- terraform-modules/databases - terraform-modules/databases
# Logo for this project # Logo for this project
#logo: docs/logo.png #logo: docs/logo.png
# License of this project # License of this project
license: "APACHE2" license: "APACHE2"
# Canonical GitHub repo # Canonical GitHub repo
github_repo: cloudposse/terraform-aws-elasticache-redis github_repo: cloudposse/terraform-aws-elasticache-redis
# Badges to display # Badges to display
badges: badges:
- name: "Build Status" - name: "Build Status"
image: "https://travis-ci.org/cloudposse/terraform-aws-elasticache-redis.svg?branch=master" image: "https://travis-ci.org/cloudposse/terraform-aws-elasticache-redis.svg?branch=master"
url: "https://travis-ci.org/cloudposse/terraform-aws-elasticache-redis" url: "https://travis-ci.org/cloudposse/terraform-aws-elasticache-redis"
- name: "Latest Release" - name: "Latest Release"
image: "https://img.shields.io/github/release/cloudposse/terraform-aws-elasticache-redis.svg" image: "https://img.shields.io/github/release/cloudposse/terraform-aws-elasticache-redis.svg"
url: "https://github.com/cloudposse/terraform-aws-elastic-beanstalk-environment/releases/latest" url: "https://github.com/cloudposse/terraform-aws-elastic-beanstalk-environment/releases/latest"
- name: "Slack Community" - name: "Slack Community"
image: "https://slack.cloudposse.com/badge.svg" image: "https://slack.cloudposse.com/badge.svg"
url: "https://slack.cloudposse.com" url: "https://slack.cloudposse.com"
# Short description of this project # Short description of this project
description: |- description: |-
Terraform module to provision an [`ElastiCache`](https://aws.amazon.com/elasticache/) Redis Cluster Terraform module to provision an [`ElastiCache`](https://aws.amazon.com/elasticache/) Redis Cluster
# How to use this project # How to use this project
usage: |- usage: |-
Include this repository as a module in your existing terraform code:
```hcl
```hcl // Generate a random string for auth token, no special chars
// Generate a random string for auth token, no special chars resource "random_string" "auth_token" {
resource "random_string" "auth_token" { length = 64
length = 64 special = false
special = false }
}
module "example_redis" {
module "example_redis" { source = "git::https://github.com/cloudposse/terraform-aws-elasticache-redis.git?ref=master"
source = "git::https://github.com/cloudposse/terraform-aws-elasticache-redis.git?ref=master" namespace = "general"
namespace = "general" name = "redis"
name = "redis" stage = "prod"
stage = "prod" zone_id = "${var.route53_zone_id}"
zone_id = "${var.route53_zone_id}" security_groups = ["${var.security_group_id}"]
security_groups = ["${var.security_group_id}"]
auth_token = "${random_string.auth_token.result}"
auth_token = "${random_string.auth_token.result}" vpc_id = "${var.vpc_id}"
vpc_id = "${var.vpc_id}" subnets = "${var.private_subnets}"
subnets = "${var.private_subnets}" maintenance_window = "wed:03:00-wed:04:00"
maintenance_window = "wed:03:00-wed:04:00" cluster_size = "2"
cluster_size = "2" instance_type = "cache.t2.micro"
instance_type = "cache.t2.micro" engine_version = "4.0.10"
engine_version = "4.0.10" alarm_cpu_threshold_percent = "${var.cache_alarm_cpu_threshold_percent}"
alarm_cpu_threshold_percent = "${var.cache_alarm_cpu_threshold_percent}" alarm_memory_threshold_bytes = "${var.cache_alarm_memory_threshold_bytes}"
alarm_memory_threshold_bytes = "${var.cache_alarm_memory_threshold_bytes}" apply_immediately = "true"
apply_immediately = "true" availability_zones = "${var.availability_zones}"
availability_zones = "${var.availability_zones}"
automatic_failover = "false"
automatic_failover = "false" }
}
output "auth_token" {
output "auth_token" { value = "${random_string.auth_token.result}"
value = "${random_string.auth_token.result}" }
} ```
```
examples: |-
examples: |- Review the [complete example](examples/simple) to see how to use this module.
Review the [complete example](examples/simple) to see how to use this module.
include:
include: - "docs/targets.md"
- "docs/targets.md" - "docs/terraform.md"
- "docs/terraform.md"
# Contributors to this project
# Contributors to this project contributors:
contributors: - name: "Erik Osterman"
- name: "Erik Osterman" github: "osterman"
github: "osterman" - name: "Igor Rodionov"
- name: "Igor Rodionov" github: "goruha"
github: "goruha" - name: "Andriy Knysh"
- name: "Andriy Knysh" github: "aknysh"
github: "aknysh" - name: "Daren Desjardins"
- name: "Daren Desjardins" github: "darend"
github: "darend" - name: "Max Moon"
- name: "Max Moon" github: "MoonMoon1919"
github: "MoonMoon1919" - name: "Christopher Riley"
- name: "Christopher Riley" github: "christopherriley"
github: "christopherriley"
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
| availability_zones | Availability zone ids | list | `<list>` | no | | availability_zones | Availability zone ids | list | `<list>` | no |
| cluster_size | Count of nodes in cluster | string | `1` | no | | cluster_size | Count of nodes in cluster | string | `1` | no |
| delimiter | Delimiter between `name`, `namespace`, `stage` and `attributes` | string | `-` | no | | delimiter | Delimiter between `name`, `namespace`, `stage` and `attributes` | string | `-` | no |
| elasticache_subnet_group_name | Subnet group name for the ElastiCache instance | string | `` | no |
| enabled | Set to false to prevent the module from creating any resources | string | `true` | no | | enabled | Set to false to prevent the module from creating any resources | string | `true` | no |
| engine_version | Redis engine version | string | `4.0.10` | no | | engine_version | Redis engine version | string | `4.0.10` | no |
| family | Redis family | string | `redis4.0` | no | | family | Redis family | string | `redis4.0` | no |
...@@ -27,10 +28,10 @@ ...@@ -27,10 +28,10 @@
| replication_group_id | Replication group ID with the following constraints: A name must contain from 1 to 20 alphanumeric characters or hyphens. The first character must be a letter. A name cannot end with a hyphen or contain two consecutive hyphens. | string | `` | no | | replication_group_id | Replication group ID with the following constraints: A name must contain from 1 to 20 alphanumeric characters or hyphens. The first character must be a letter. A name cannot end with a hyphen or contain two consecutive hyphens. | string | `` | no |
| security_groups | AWS security group ids | list | `<list>` | no | | security_groups | AWS security group ids | list | `<list>` | no |
| stage | Stage | string | `default` | no | | stage | Stage | string | `default` | no |
| subnets | AWS subnet ids | list | `<list>` | no | | subnets | AWS subnet IDs | list | `<list>` | no |
| tags | Additional tags (_e.g._ map("BusinessUnit","ABC") | map | `<map>` | no | | tags | Additional tags (_e.g._ map("BusinessUnit","ABC") | map | `<map>` | no |
| transit_encryption_enabled | Enable TLS | string | `true` | no | | transit_encryption_enabled | Enable TLS | string | `true` | no |
| vpc_id | AWS VPC id | string | `REQUIRED` | no | | vpc_id | AWS VPC id | string | - | yes |
| zone_id | Route53 DNS Zone id | string | `` | no | | zone_id | Route53 DNS Zone id | string | `` | no |
## Outputs ## Outputs
...@@ -38,7 +39,7 @@ ...@@ -38,7 +39,7 @@
| Name | Description | | Name | Description |
|------|-------------| |------|-------------|
| host | Redis host | | host | Redis host |
| id | Redis cluster id | | id | Redis cluster ID |
| port | Redis port | | port | Redis port |
| security_group_id | Security group id | | security_group_id | Security group ID |
namespace = "eg" namespace = "eg"
name = "redis" name = "redis"
stage = "testing" stage = "testing"
zone_id = "Z3SO0TKDDQ0RGG" zone_id = "Z3SO0TKDDQ0RGG"
region = "us-west-2" region = "us-west-2"
availability_zones = ["us-west-2a", "us-west-2b"] availability_zones = ["us-west-2a", "us-west-2b"]
...@@ -35,8 +35,12 @@ resource "aws_security_group" "default" { ...@@ -35,8 +35,12 @@ resource "aws_security_group" "default" {
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) }"
}
resource "aws_elasticache_subnet_group" "default" { resource "aws_elasticache_subnet_group" "default" {
count = "${var.enabled == "true" ? 1 : 0}" count = "${var.enabled == "true" && var.elasticache_subnet_group_name == "" && length(var.subnets) > 0 ? 1 : 0}"
name = "${module.label.id}" name = "${module.label.id}"
subnet_ids = ["${var.subnets}"] subnet_ids = ["${var.subnets}"]
} }
...@@ -60,7 +64,7 @@ resource "aws_elasticache_replication_group" "default" { ...@@ -60,7 +64,7 @@ resource "aws_elasticache_replication_group" "default" {
parameter_group_name = "${aws_elasticache_parameter_group.default.name}" parameter_group_name = "${aws_elasticache_parameter_group.default.name}"
availability_zones = ["${slice(var.availability_zones, 0, var.cluster_size)}"] availability_zones = ["${slice(var.availability_zones, 0, var.cluster_size)}"]
automatic_failover_enabled = "${var.automatic_failover}" automatic_failover_enabled = "${var.automatic_failover}"
subnet_group_name = "${aws_elasticache_subnet_group.default.name}" subnet_group_name = "${local.elasticache_subnet_group_name}"
security_group_ids = ["${aws_security_group.default.id}"] security_group_ids = ["${aws_security_group.default.id}"]
maintenance_window = "${var.maintenance_window}" maintenance_window = "${var.maintenance_window}"
notification_topic_arn = "${var.notification_topic_arn}" notification_topic_arn = "${var.notification_topic_arn}"
...@@ -119,7 +123,7 @@ resource "aws_cloudwatch_metric_alarm" "cache_memory" { ...@@ -119,7 +123,7 @@ resource "aws_cloudwatch_metric_alarm" "cache_memory" {
} }
module "dns" { module "dns" {
source = "git::https://github.com/cloudposse/terraform-aws-route53-cluster-hostname.git?ref=tags/0.2.1" 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"}" enabled = "${var.enabled == "true" && length(var.zone_id) > 0 ? "true" : "false"}"
namespace = "${var.namespace}" namespace = "${var.namespace}"
name = "${var.name}" name = "${var.name}"
......
output "id" { output "id" {
value = "${join("", aws_elasticache_replication_group.default.*.id)}" value = "${join("", aws_elasticache_replication_group.default.*.id)}"
description = "Redis cluster id" description = "Redis cluster ID"
} }
output "security_group_id" { output "security_group_id" {
value = "${join("", aws_security_group.default.*.id)}" value = "${join("", aws_security_group.default.*.id)}"
description = "Security group id" description = "Security group ID"
} }
output "port" { output "port" {
......
...@@ -25,16 +25,21 @@ variable "security_groups" { ...@@ -25,16 +25,21 @@ variable "security_groups" {
} }
variable "vpc_id" { variable "vpc_id" {
default = "REQUIRED"
description = "AWS VPC id" description = "AWS VPC id"
} }
variable "subnets" { variable "subnets" {
type = "list" type = "list"
description = "AWS subnet ids" description = "AWS subnet IDs"
default = [] default = []
} }
variable "elasticache_subnet_group_name" {
type = "string"
description = "Subnet group name for the ElastiCache instance"
default = ""
}
variable "maintenance_window" { variable "maintenance_window" {
default = "wed:03:00-wed:04:00" default = "wed:03:00-wed:04:00"
description = "Maintenance window" description = "Maintenance window"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment