From 94258bb53f30b2f0090cf3a008f0f7b53ca54aa5 Mon Sep 17 00:00:00 2001 From: Marcin Cuber Date: Tue, 10 Nov 2020 10:06:42 +0000 Subject: [PATCH] Add support for clustered mode and upgrade fixes (#8) --- CHANGELOG.md | 31 +++++++++- README.md | 10 +++- examples/{core => redis-basic}/README.md | 0 examples/{core => redis-basic}/main.tf | 4 +- examples/redis-clustered-mode/main.tf | 72 ++++++++++++++++++++++++ main.tf | 26 ++++++++- variables.tf | 18 ++++++ versions.tf | 3 +- 8 files changed, 154 insertions(+), 10 deletions(-) rename examples/{core => redis-basic}/README.md (100%) rename examples/{core => redis-basic}/main.tf (94%) create mode 100644 examples/redis-clustered-mode/main.tf diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cfe759..c7b684f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,30 @@ All notable changes to this project will be documented in this file. ## [Unreleased] -- Update module versions to support v3 provider + + + +## [1.2.0] - 2020-11-09 + +- Add self sg ingress rule option ([#7](https://github.com/umotif-public/terraform-aws-elasticache-redis/issues/7)) + + + +## [1.1.1] - 2020-11-09 + +- Update module to remove 0.14 limit ([#6](https://github.com/umotif-public/terraform-aws-elasticache-redis/issues/6)) + + + +## [1.1.0] - 2020-08-20 + +- Update main.tf + + + +## [1.0.3] - 2020-08-05 + +- Feature/v3 provider support ([#4](https://github.com/umotif-public/terraform-aws-elasticache-redis/issues/4)) - Feature/updates ([#3](https://github.com/umotif-public/terraform-aws-elasticache-redis/issues/3)) - add git hooks and update docs - Update README.md @@ -30,6 +53,10 @@ All notable changes to this project will be documented in this file. - Initial commit of docs -[Unreleased]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.0.2...HEAD +[Unreleased]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.2.0...HEAD +[1.2.0]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.1.1...1.2.0 +[1.1.1]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.1.0...1.1.1 +[1.1.0]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.0.3...1.1.0 +[1.0.3]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.0.2...1.0.3 [1.0.2]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.0.1...1.0.2 [1.0.1]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.0.0...1.0.1 diff --git a/README.md b/README.md index 7d28029..6a061f4 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,8 @@ Module is to be used with Terraform > 0.12. ## Examples -* [Elasicache Redis](https://github.com/umotif-public/terraform-aws-elasticache-redis/tree/master/examples/core) +* [Redis Basic](https://github.com/umotif-public/terraform-aws-elasticache-redis/tree/master/examples/redis-basic) +* [Redis Clustered Mode](https://github.com/umotif-public/terraform-aws-elasticache-redis/tree/master/examples/redis-clustered-mode) ## Authors @@ -70,12 +71,14 @@ Module managed by [Marcin Cuber](https://github.com/marcincuber) [linkedin](http |------|---------| | terraform | >= 0.12.6 | | aws | >= 2.41 | +| random | >= 3.0 | ## Providers | Name | Version | |------|---------| | aws | >= 2.41 | +| random | >= 3.0 | ## Inputs @@ -86,19 +89,22 @@ Module managed by [Marcin Cuber](https://github.com/marcincuber) [linkedin](http | auth\_token | The password used to access a password protected server. Can be specified only if `transit_encryption_enabled = true`. | `string` | `""` | no | | auto\_minor\_version\_upgrade | n/a | `string` | `true` | no | | automatic\_failover\_enabled | Specifies whether a read-only replica will be automatically promoted to read/write primary if the existing primary fails. | `bool` | `true` | no | +| cluster\_mode\_enabled | Enable creation of a native redis cluster. | `bool` | `false` | no | | description | The description of the all resources. | `string` | `"Managed by Terraform"` | no | | engine\_version | The version number of the cache engine to be used for the cache clusters in this replication group. | `string` | `"5.0.6"` | no | | family | The family of the ElastiCache parameter group. | `string` | `"redis5.0"` | no | -| ingress\_self | Specify whether the security group itself will be added as a source to the ingress rule. | `bool` | `false` | no | | ingress\_cidr\_blocks | List of Ingress CIDR blocks. | `list(string)` | `[]` | no | +| ingress\_self | Specify whether the security group itself will be added as a source to the ingress rule. | `bool` | `false` | no | | kms\_key\_id | The ARN of the key that you wish to use if encrypting at rest. If not supplied, uses service managed encryption. Can be specified only if `at_rest_encryption_enabled = true` | `string` | `""` | no | | maintenance\_window | Specifies the weekly time range for when maintenance on the cache cluster is performed. | `string` | `""` | no | | name\_prefix | The replication group identifier. This parameter is stored as a lowercase string. | `string` | n/a | yes | | node\_type | The compute and memory capacity of the nodes in the node group. | `string` | n/a | yes | | notification\_topic\_arn | An Amazon Resource Name (ARN) of an SNS topic to send ElastiCache notifications to. Example: `arn:aws:sns:us-east-1:012345678999:my_sns_topic` | `string` | `""` | no | +| num\_node\_groups | Required when `cluster_mode_enabled` is set to true. Specify the number of node groups (shards) for this Redis replication group. Changing this number will trigger an online resizing operation before other settings modifications. | `number` | `0` | no | | number\_cache\_clusters | The number of cache clusters (primary and replicas) this replication group will have. | `number` | n/a | yes | | parameter | A list of Redis parameters to apply. Note that parameters may differ from one Redis family to another |
list(object({
name = string
value = string
}))
| `[]` | no | | port | The port number on which each of the cache nodes will accept connections. | `number` | `6379` | no | +| replicas\_per\_node\_group | Required when `cluster_mode_enabled` is set to true. Specify the number of replica nodes in each node group. Valid values are 0 to 5. Changing this number will force a new resource. | `number` | `0` | no | | security\_group\_ids | List of Security Groups. | `list(string)` | `[]` | no | | snapshot\_retention\_limit | The number of days for which ElastiCache will retain automatic cache cluster snapshots before deleting them. | `number` | `30` | no | | snapshot\_window | The daily time range (in UTC) during which ElastiCache will begin taking a daily snapshot of your cache cluster. | `string` | `""` | no | diff --git a/examples/core/README.md b/examples/redis-basic/README.md similarity index 100% rename from examples/core/README.md rename to examples/redis-basic/README.md diff --git a/examples/core/main.tf b/examples/redis-basic/main.tf similarity index 94% rename from examples/core/main.tf rename to examples/redis-basic/main.tf index f42fcad..2bce8d6 100644 --- a/examples/core/main.tf +++ b/examples/redis-basic/main.tf @@ -7,7 +7,7 @@ provider "aws" { ##### module "vpc" { source = "terraform-aws-modules/vpc/aws" - version = "2.21.0" + version = "2.63.0" name = "simple-vpc" @@ -30,7 +30,7 @@ module "vpc" { module "redis" { source = "../../" - name_prefix = "core-example" + name_prefix = "redis-basic-example" number_cache_clusters = 2 node_type = "cache.t3.small" diff --git a/examples/redis-clustered-mode/main.tf b/examples/redis-clustered-mode/main.tf new file mode 100644 index 0000000..d957a0c --- /dev/null +++ b/examples/redis-clustered-mode/main.tf @@ -0,0 +1,72 @@ +provider "aws" { + region = "eu-west-1" +} + +##### +# VPC and subnets +##### +module "vpc" { + source = "terraform-aws-modules/vpc/aws" + version = "2.63.0" + + name = "simple-vpc" + + cidr = "10.0.0.0/16" + + azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"] + private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] + + enable_nat_gateway = false + + tags = { + Environment = "test" + } +} + +##### +# Elasticache Redis +##### +module "redis" { + source = "../../" + + name_prefix = "redis-clustered-example" + number_cache_clusters = 2 + node_type = "cache.t3.small" + + cluster_mode_enabled = true + replicas_per_node_group = 1 + num_node_groups = 1 + + engine_version = "6.x" + port = 6379 + maintenance_window = "mon:03:00-mon:04:00" + snapshot_window = "04:00-06:00" + snapshot_retention_limit = 7 + + automatic_failover_enabled = true + + at_rest_encryption_enabled = true + transit_encryption_enabled = true + auth_token = "1234567890asdfghjkl" + + apply_immediately = true + family = "redis6.x" + description = "Test elasticache redis." + + subnet_ids = module.vpc.private_subnets + vpc_id = module.vpc.vpc_id + + ingress_cidr_blocks = ["0.0.0.0/0"] + + parameter = [ + { + name = "repl-backlog-size" + value = "16384" + } + ] + + tags = { + Project = "Test" + } +} diff --git a/main.tf b/main.tf index 7b3076f..bdbfc0f 100644 --- a/main.tf +++ b/main.tf @@ -6,7 +6,7 @@ resource "aws_elasticache_replication_group" "redis" { security_group_ids = concat(var.security_group_ids, [aws_security_group.redis.id]) replication_group_id = "${var.name_prefix}-redis" - number_cache_clusters = var.number_cache_clusters + number_cache_clusters = var.cluster_mode_enabled ? null : var.number_cache_clusters node_type = var.node_type engine_version = var.engine_version @@ -29,6 +29,14 @@ resource "aws_elasticache_replication_group" "redis" { notification_topic_arn = var.notification_topic_arn + dynamic "cluster_mode" { + for_each = var.cluster_mode_enabled ? [1] : [] + content { + replicas_per_node_group = var.replicas_per_node_group + num_node_groups = var.num_node_groups + } + } + tags = merge( { "Name" = "${var.name_prefix}-redis" @@ -37,18 +45,30 @@ resource "aws_elasticache_replication_group" "redis" { ) } +resource "random_id" "redis_pg" { + keepers = { + family = var.family + } + + byte_length = 2 +} + resource "aws_elasticache_parameter_group" "redis" { - name = "${var.name_prefix}-redis-pg" + name = "${var.name_prefix}-redis-${random_id.redis_pg.hex}" family = var.family description = var.description dynamic "parameter" { - for_each = var.parameter + for_each = var.cluster_mode_enabled ? concat([{ name = "cluster-enabled", value = "yes" }], var.parameter) : var.parameter content { name = parameter.value.name value = parameter.value.value } } + + lifecycle { + create_before_destroy = true + } } resource "aws_elasticache_subnet_group" "redis" { diff --git a/variables.tf b/variables.tf index 352b102..ed3d84d 100644 --- a/variables.tf +++ b/variables.tf @@ -144,3 +144,21 @@ variable "notification_topic_arn" { default = "" description = "An Amazon Resource Name (ARN) of an SNS topic to send ElastiCache notifications to. Example: `arn:aws:sns:us-east-1:012345678999:my_sns_topic`" } + +variable "cluster_mode_enabled" { + type = bool + description = "Enable creation of a native redis cluster." + default = false +} + +variable "replicas_per_node_group" { + type = number + default = 0 + description = "Required when `cluster_mode_enabled` is set to true. Specify the number of replica nodes in each node group. Valid values are 0 to 5. Changing this number will force a new resource." +} + +variable "num_node_groups" { + type = number + default = 0 + description = "Required when `cluster_mode_enabled` is set to true. Specify the number of node groups (shards) for this Redis replication group. Changing this number will trigger an online resizing operation before other settings modifications." +} diff --git a/versions.tf b/versions.tf index 03815eb..fa10960 100644 --- a/versions.tf +++ b/versions.tf @@ -2,6 +2,7 @@ terraform { required_version = ">= 0.12.6" required_providers { - aws = ">= 2.41" + aws = ">= 2.41" + random = ">= 3.0" } }