diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d2cfb32..94153ac 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 + rev: v4.1.0 hooks: - id: check-added-large-files args: ['--maxkb=500'] @@ -18,7 +18,7 @@ repos: args: ['--allow-missing-credentials'] - id: trailing-whitespace - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.50.0 + rev: v1.64.0 hooks: - id: terraform_fmt - id: terraform_docs diff --git a/CHANGELOG.md b/CHANGELOG.md index 784085c..df9b97f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,19 @@ All notable changes to this project will be documented in this file. + +## [2.2.0] - 2021-08-11 + +- Add support for global_replication_group_id ([#19](https://github.com/umotif-public/terraform-aws-elasticache-redis/issues/19)) + + + +## [2.1.0] - 2021-06-11 + +- Update docs and use main branch as default ([#18](https://github.com/umotif-public/terraform-aws-elasticache-redis/issues/18)) +- Add tags support to all resources ([#17](https://github.com/umotif-public/terraform-aws-elasticache-redis/issues/17)) + + ## [2.0.0] - 2021-04-19 @@ -84,7 +97,9 @@ 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/2.0.0...HEAD +[Unreleased]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/2.2.0...HEAD +[2.2.0]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/2.1.0...2.2.0 +[2.1.0]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/2.0.0...2.1.0 [2.0.0]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.5.0...2.0.0 [1.5.0]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.4.0...1.5.0 [1.4.0]: https://github.com/umotif-public/terraform-aws-elasticache-redis/compare/1.3.1...1.4.0 diff --git a/README.md b/README.md index ec37a48..085c70d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ - +[](https://github.com/umotif-public/terraform-aws-elasticache-redis/releases/latest) # terraform-aws-elasticache-redis @@ -6,9 +6,10 @@ A Terraform module to create an AWS Redis ElastiCache cluster ## Terraform versions -Terraform 0.14.5 and higher, pin module version to `~> v2.0`. -For Terraform 0.12 to 0.14.4, pin module version to `~> v1.0`. -Submit pull-requests to `main` branch. +- For AWS Provider >= 4.0.0, pin module version to `~> v3.0`. +- For Terraform 0.14.5 and higher *and* AWS Provider < 4.0.0, pin module version to `~> v2.0`. +- For Terraform 0.12 to 0.14.4, pin module version to `~> v1.0`. +- Submit pull-requests to `main` branch. ## Usage @@ -57,27 +58,28 @@ module "redis" { ## Examples -* [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) +- [Redis Basic](https://github.com/umotif-public/terraform-aws-elasticache-redis/tree/main/examples/redis-basic) +- [Redis Clustered Mode](https://github.com/umotif-public/terraform-aws-elasticache-redis/tree/main/examples/redis-clustered-mode) +- [Redis Replication Group](https://github.com/umotif-public/terraform-aws-elasticache-redis/tree/main/examples/redis-replication-group) ## Authors -Module managed by [Marcin Cuber](https://github.com/marcincuber) [linkedin](https://www.linkedin.com/in/marcincuber/). +Module managed by [Abdul Wahid](https://github.com/Ohid25) ([LinkedIn](https://www.linkedin.com/in/abdul-wahid/)) ## Requirements | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 0.14.5 | -| [aws](#requirement\_aws) | >= 3.43 | +| [terraform](#requirement\_terraform) | >= 0.14.11 | +| [aws](#requirement\_aws) | >= 4.0.0 | | [random](#requirement\_random) | >= 3.1.0 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 3.43 | +| [aws](#provider\_aws) | >= 4.0.0 | | [random](#provider\_random) | >= 3.1.0 | ## Modules @@ -105,8 +107,7 @@ No modules. | [at\_rest\_encryption\_enabled](#input\_at\_rest\_encryption\_enabled) | Whether to enable encryption at rest. | `bool` | `true` | no | | [auth\_token](#input\_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](#input\_auto\_minor\_version\_upgrade) | n/a | `string` | `true` | no | -| [automatic\_failover\_enabled](#input\_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 | -| [availability\_zones](#input\_availability\_zones) | A list of EC2 availability zones in which the replication group's cache clusters will be created. The order of the availability zones in the list is not important. | `list(string)` | `null` | no | +| [automatic\_failover\_enabled](#input\_automatic\_failover\_enabled) | Specifies whether a read-only replica will be automatically promoted to read/write primary if the existing primary fails. If enabled, number\_cache\_clusters must be greater than 1. Must be enabled for Redis (cluster mode enabled) replication groups. | `bool` | `true` | no | | [cluster\_mode\_enabled](#input\_cluster\_mode\_enabled) | Enable creation of a native redis cluster. | `bool` | `false` | no | | [description](#input\_description) | The description of the all resources. | `string` | `"Managed by Terraform"` | no | | [engine\_version](#input\_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 | @@ -117,15 +118,16 @@ No modules. | [ingress\_self](#input\_ingress\_self) | Specify whether the security group itself will be added as a source to the ingress rule. | `bool` | `false` | no | | [kms\_key\_id](#input\_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](#input\_maintenance\_window) | Specifies the weekly time range for when maintenance on the cache cluster is performed. | `string` | `""` | no | -| [multi\_az\_enabled](#input\_multi\_az\_enabled) | Specifies whether to enable Multi-AZ Support for the replication group. If true, `automatic_failover_enabled` must also be enabled. Defaults to false. | `string` | `null` | no | +| [multi\_az\_enabled](#input\_multi\_az\_enabled) | Specifies whether to enable Multi-AZ Support for the replication group. If true, `automatic_failover_enabled` must also be enabled. Defaults to false. | `string` | `false` | no | | [name\_prefix](#input\_name\_prefix) | The replication group identifier. This parameter is stored as a lowercase string. | `string` | n/a | yes | | [node\_type](#input\_node\_type) | The compute and memory capacity of the nodes in the node group. | `string` | n/a | yes | | [notification\_topic\_arn](#input\_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](#input\_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](#input\_number\_cache\_clusters) | The number of cache clusters (primary and replicas) this replication group will have. | `number` | n/a | yes | +| [num\_cache\_clusters](#input\_num\_cache\_clusters) | The number of cache clusters (primary and replicas) this replication group will have. If Multi-AZ is enabled, the value of this parameter must be at least 2. Updates will occur before other modifications. Conflicts with num\_node\_groups. | `number` | `1` | no | +| [num\_node\_groups](#input\_num\_node\_groups) | 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 | | [parameter](#input\_parameter) | A list of Redis parameters to apply. Note that parameters may differ from one Redis family to another |
list(object({| `[]` | no | | [port](#input\_port) | The port number on which each of the cache nodes will accept connections. | `number` | `6379` | no | -| [replicas\_per\_node\_group](#input\_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 | +| [preferred\_cache\_cluster\_azs](#input\_preferred\_cache\_cluster\_azs) | A list of EC2 availability zones in which the replication group's cache clusters will be created. The order of the availability zones in the list is not important. | `list(string)` | `null` | no | +| [replicas\_per\_node\_group](#input\_replicas\_per\_node\_group) | Specify the number of replica nodes in each node group. Valid values are 0 to 5. Changing this number will trigger an online resizing operation before other settings modifications. | `number` | `0` | no | | [security\_group\_ids](#input\_security\_group\_ids) | List of Security Groups. | `list(string)` | `[]` | no | | [snapshot\_retention\_limit](#input\_snapshot\_retention\_limit) | The number of days for which ElastiCache will retain automatic cache cluster snapshots before deleting them. | `number` | `30` | no | | [snapshot\_window](#input\_snapshot\_window) | The daily time range (in UTC) during which ElastiCache will begin taking a daily snapshot of your cache cluster. | `string` | `""` | no | @@ -164,9 +166,9 @@ See LICENSE for full details. ### Install dependencies -* [`pre-commit`](https://pre-commit.com/#install) -* [`terraform-docs`](https://github.com/segmentio/terraform-docs) required for `terraform_docs` hooks. -* [`TFLint`](https://github.com/terraform-linters/tflint) required for `terraform_tflint` hook. +- [`pre-commit`](https://pre-commit.com/#install) +- [`terraform-docs`](https://github.com/segmentio/terraform-docs) required for `terraform_docs` hooks. +- [`TFLint`](https://github.com/terraform-linters/tflint) required for `terraform_tflint` hook. #### MacOS diff --git a/examples/redis-basic/main.tf b/examples/redis-basic/main.tf index 9a66d99..fcba1e8 100644 --- a/examples/redis-basic/main.tf +++ b/examples/redis-basic/main.tf @@ -12,8 +12,11 @@ data "aws_vpc" "default" { default = true } -data "aws_subnet_ids" "all" { - vpc_id = data.aws_vpc.default.id +data "aws_subnets" "all" { + filter { + name = "vpc-id" + values = [data.aws_vpc.default.id] + } } ##### # Elasticache Redis @@ -21,9 +24,9 @@ data "aws_subnet_ids" "all" { module "redis" { source = "../../" - name_prefix = "redis-basic-example" - number_cache_clusters = 2 - node_type = "cache.t3.small" + name_prefix = "redis-basic-example" + num_cache_clusters = 2 + node_type = "cache.t3.small" engine_version = "6.x" port = 6379 @@ -43,7 +46,7 @@ module "redis" { family = "redis6.x" description = "Test elasticache redis." - subnet_ids = data.aws_subnet_ids.all.ids + subnet_ids = data.aws_subnets.all.ids vpc_id = data.aws_vpc.default.id ingress_cidr_blocks = ["0.0.0.0/0"] diff --git a/examples/redis-clustered-mode/main.tf b/examples/redis-clustered-mode/main.tf index 67a933b..185f448 100644 --- a/examples/redis-clustered-mode/main.tf +++ b/examples/redis-clustered-mode/main.tf @@ -9,8 +9,11 @@ data "aws_vpc" "default" { default = true } -data "aws_subnet_ids" "all" { - vpc_id = data.aws_vpc.default.id +data "aws_subnets" "all" { + filter { + name = "vpc-id" + values = [data.aws_vpc.default.id] + } } ##### @@ -19,9 +22,9 @@ data "aws_subnet_ids" "all" { module "redis" { source = "../../" - name_prefix = "redis-clustered-example" - number_cache_clusters = 2 - node_type = "cache.t3.small" + name_prefix = "redis-clustered-example" + num_cache_clusters = 2 + node_type = "cache.t3.small" cluster_mode_enabled = true replicas_per_node_group = 1 @@ -43,7 +46,7 @@ module "redis" { family = "redis6.x" description = "Test elasticache redis." - subnet_ids = data.aws_subnet_ids.all.ids + subnet_ids = data.aws_subnets.all.ids vpc_id = data.aws_vpc.default.id ingress_cidr_blocks = ["0.0.0.0/0"] diff --git a/examples/redis-replication-group/main.tf b/examples/redis-replication-group/main.tf index 7f03dd5..dc99f03 100644 --- a/examples/redis-replication-group/main.tf +++ b/examples/redis-replication-group/main.tf @@ -21,12 +21,18 @@ data "aws_vpc" "replica" { provider = aws.replica } -data "aws_subnet_ids" "main" { - vpc_id = data.aws_vpc.main.id +data "aws_subnets" "main" { + filter { + name = "vpc-id" + values = [data.aws_vpc.main.id] + } } -data "aws_subnet_ids" "replica" { - vpc_id = data.aws_vpc.replica.id +data "aws_subnets" "replica" { + filter { + name = "vpc-id" + values = [data.aws_vpc.replica.id] + } provider = aws.replica } @@ -37,12 +43,12 @@ data "aws_subnet_ids" "replica" { module "redis_main" { source = "../../" - name_prefix = "redis-replication-example" - number_cache_clusters = 2 - node_type = "cache.m5.large" - auth_token = "1234567890asdfghjkl" + name_prefix = "redis-example-main" + num_cache_clusters = 2 + node_type = "cache.m5.large" + auth_token = "1234567890asdfghjkl" - subnet_ids = data.aws_subnet_ids.main.ids + subnet_ids = data.aws_subnets.main.ids vpc_id = data.aws_vpc.main.id } @@ -54,12 +60,12 @@ resource "aws_elasticache_global_replication_group" "this" { module "redis_replica" { source = "../../" - name_prefix = "redis-replication-example" - number_cache_clusters = 2 - node_type = "cache.m5.large" - auth_token = "1234567890asdfghjkl" + name_prefix = "redis-example-replica" + num_cache_clusters = 2 + node_type = "cache.m5.large" + auth_token = "1234567890asdfghjkl" - subnet_ids = data.aws_subnet_ids.replica.ids + subnet_ids = data.aws_subnets.replica.ids vpc_id = data.aws_vpc.replica.id global_replication_group_id = aws_elasticache_global_replication_group.this.global_replication_group_id diff --git a/main.tf b/main.tf index 019ce1e..1d5c2ba 100644 --- a/main.tf +++ b/main.tf @@ -5,10 +5,10 @@ resource "aws_elasticache_replication_group" "redis" { subnet_group_name = aws_elasticache_subnet_group.redis.name security_group_ids = concat(var.security_group_ids, [aws_security_group.redis.id]) - availability_zones = var.availability_zones - replication_group_id = var.global_replication_group_id == null ? "${var.name_prefix}-redis" : "${var.name_prefix}-redis-replica" - number_cache_clusters = var.cluster_mode_enabled ? null : var.number_cache_clusters - node_type = var.global_replication_group_id == null ? var.node_type : null + preferred_cache_cluster_azs = var.preferred_cache_cluster_azs + replication_group_id = var.global_replication_group_id == null ? "${var.name_prefix}-redis" : "${var.name_prefix}-redis-replica" + num_cache_clusters = var.cluster_mode_enabled ? null : var.num_cache_clusters + node_type = var.global_replication_group_id == null ? var.node_type : null engine_version = var.global_replication_group_id == null ? var.engine_version : null port = var.port @@ -17,7 +17,7 @@ resource "aws_elasticache_replication_group" "redis" { snapshot_window = var.snapshot_window snapshot_retention_limit = var.snapshot_retention_limit final_snapshot_identifier = var.final_snapshot_identifier - automatic_failover_enabled = var.automatic_failover_enabled && var.number_cache_clusters > 1 ? true : false + automatic_failover_enabled = var.automatic_failover_enabled && var.num_cache_clusters >= 2 ? true : false auto_minor_version_upgrade = var.auto_minor_version_upgrade multi_az_enabled = var.multi_az_enabled @@ -29,17 +29,12 @@ resource "aws_elasticache_replication_group" "redis" { apply_immediately = var.apply_immediately - replication_group_description = var.description + description = var.description 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 - } - } + replicas_per_node_group = var.cluster_mode_enabled ? var.replicas_per_node_group : null + num_node_groups = var.cluster_mode_enabled ? var.num_node_groups : null tags = merge( { @@ -63,7 +58,7 @@ resource "aws_elasticache_parameter_group" "redis" { description = var.description dynamic "parameter" { - for_each = var.cluster_mode_enabled ? concat([{ name = "cluster-enabled", value = "yes" }], var.parameter) : var.parameter + for_each = var.num_node_groups > 0 ? concat([{ name = "cluster-enabled", value = "yes" }], var.parameter) : var.parameter content { name = parameter.value.name value = parameter.value.value diff --git a/outputs.tf b/outputs.tf index 30406cc..8e5d0d9 100644 --- a/outputs.tf +++ b/outputs.tf @@ -9,12 +9,12 @@ output "elasticache_replication_group_id" { } output "elasticache_replication_group_primary_endpoint_address" { - value = var.cluster_mode_enabled ? aws_elasticache_replication_group.redis.configuration_endpoint_address : aws_elasticache_replication_group.redis.primary_endpoint_address + value = var.num_node_groups > 1 ? aws_elasticache_replication_group.redis.configuration_endpoint_address : aws_elasticache_replication_group.redis.primary_endpoint_address description = "The address of the endpoint for the primary node in the replication group." } output "elasticache_replication_group_reader_endpoint_address" { - value = var.cluster_mode_enabled ? aws_elasticache_replication_group.redis.configuration_endpoint_address : aws_elasticache_replication_group.redis.reader_endpoint_address + value = var.num_node_groups > 1 ? aws_elasticache_replication_group.redis.configuration_endpoint_address : aws_elasticache_replication_group.redis.reader_endpoint_address description = "The address of the endpoint for the reader node in the replication group." } diff --git a/variables.tf b/variables.tf index 02fa449..5c962f8 100644 --- a/variables.tf +++ b/variables.tf @@ -3,9 +3,16 @@ variable "name_prefix" { description = "The replication group identifier. This parameter is stored as a lowercase string." } -variable "number_cache_clusters" { +variable "num_cache_clusters" { type = number - description = "The number of cache clusters (primary and replicas) this replication group will have." + default = 1 + description = "The number of cache clusters (primary and replicas) this replication group will have. If Multi-AZ is enabled, the value of this parameter must be at least 2. Updates will occur before other modifications. Conflicts with num_node_groups." +} + +variable "cluster_mode_enabled" { + type = bool + description = "Enable creation of a native redis cluster." + default = false } variable "node_type" { @@ -79,7 +86,7 @@ variable "auto_minor_version_upgrade" { variable "automatic_failover_enabled" { default = true type = bool - description = "Specifies whether a read-only replica will be automatically promoted to read/write primary if the existing primary fails." + description = "Specifies whether a read-only replica will be automatically promoted to read/write primary if the existing primary fails. If enabled, number_cache_clusters must be greater than 1. Must be enabled for Redis (cluster mode enabled) replication groups." } variable "at_rest_encryption_enabled" { @@ -145,25 +152,24 @@ variable "notification_topic_arn" { 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." + description = "Specify the number of replica nodes in each node group. Valid values are 0 to 5. Changing this number will trigger an online resizing operation before other settings modifications." + + validation { + condition = var.replicas_per_node_group <= 5 + error_message = "The replicas_per_node_group value must be between 0 and 5." + } } 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." + description = "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." } -variable "availability_zones" { +variable "preferred_cache_cluster_azs" { type = list(string) description = "A list of EC2 availability zones in which the replication group's cache clusters will be created. The order of the availability zones in the list is not important." default = null @@ -172,7 +178,7 @@ variable "availability_zones" { variable "multi_az_enabled" { type = string description = "Specifies whether to enable Multi-AZ Support for the replication group. If true, `automatic_failover_enabled` must also be enabled. Defaults to false." - default = null + default = false } variable "final_snapshot_identifier" { diff --git a/versions.tf b/versions.tf index 2014928..b1a0694 100644 --- a/versions.tf +++ b/versions.tf @@ -1,8 +1,8 @@ terraform { - required_version = ">= 0.14.5" + required_version = ">= 0.14.11" required_providers { - aws = ">= 3.43" + aws = ">= 4.0.0" random = ">= 3.1.0" } }
name = string
value = string
}))