Skip to content

Commit 9c01d8b

Browse files
committed
feat: Add support for Redis clustered mode
1 parent f6a8889 commit 9c01d8b

File tree

22 files changed

+453
-50
lines changed

22 files changed

+453
-50
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ Examples codified under the [`examples`](https://github.com/clowdhaus/terraform-
3131
| Name | Version |
3232
|------|---------|
3333
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
34-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.23 |
34+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.27 |
35+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3.0 |
3536

3637
## Providers
3738

3839
| Name | Version |
3940
|------|---------|
40-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.23 |
41+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.27 |
42+
| <a name="provider_random"></a> [random](#provider\_random) | >= 3.0 |
4143

4244
## Modules
4345

@@ -55,6 +57,7 @@ No modules.
5557
| [aws_security_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
5658
| [aws_vpc_security_group_egress_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_security_group_egress_rule) | resource |
5759
| [aws_vpc_security_group_ingress_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_security_group_ingress_rule) | resource |
60+
| [random_id.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource |
5861

5962
## Inputs
6063

@@ -69,6 +72,7 @@ No modules.
6972
| <a name="input_availability_zone"></a> [availability\_zone](#input\_availability\_zone) | Availability Zone for the cache cluster. If you want to create cache nodes in multi-az, use `preferred_availability_zones` instead | `string` | `null` | no |
7073
| <a name="input_az_mode"></a> [az\_mode](#input\_az\_mode) | Whether the nodes in this Memcached node group are created in a single Availability Zone or created across multiple Availability Zones in the cluster's region. Valid values for this parameter are `single-az` or `cross-az`, default is `single-az` | `string` | `null` | no |
7174
| <a name="input_cluster_id"></a> [cluster\_id](#input\_cluster\_id) | Group identifier. ElastiCache converts this name to lowercase. Changing this value will re-create the resource | `string` | `""` | no |
75+
| <a name="input_cluster_mode_enabled"></a> [cluster\_mode\_enabled](#input\_cluster\_mode\_enabled) | Whether to enable Redis [cluster mode https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/Replication.Redis-RedisCluster.html] | `bool` | `false` | no |
7276
| <a name="input_create"></a> [create](#input\_create) | Determines whether resources will be created (affects all resources) | `bool` | `true` | no |
7377
| <a name="input_create_cluster"></a> [create\_cluster](#input\_create\_cluster) | Determines whether an ElastiCache cluster will be created or not | `bool` | `true` | no |
7478
| <a name="input_create_parameter_group"></a> [create\_parameter\_group](#input\_create\_parameter\_group) | Determines whether the ElastiCache parameter group will be created or not | `bool` | `false` | no |
@@ -118,7 +122,7 @@ No modules.
118122
| <a name="input_subnet_group_name"></a> [subnet\_group\_name](#input\_subnet\_group\_name) | The name of the subnet group. If `create_subnet_group` is `true`, this is the name assigned to the subnet group created. Otherwise, this is the name of an existing subnet group | `string` | `null` | no |
119123
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | List of VPC Subnet IDs for the Elasticache subnet group | `list(string)` | `[]` | no |
120124
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no |
121-
| <a name="input_transit_encryption_enabled"></a> [transit\_encryption\_enabled](#input\_transit\_encryption\_enabled) | Enable encryption in-transit. Supported only with Memcached versions `1.6.12` and later, running in a VPC | `bool` | `null` | no |
125+
| <a name="input_transit_encryption_enabled"></a> [transit\_encryption\_enabled](#input\_transit\_encryption\_enabled) | Enable encryption in-transit. Supported only with Memcached versions `1.6.12` and later, running in a VPC | `bool` | `true` | no |
122126
| <a name="input_user_group_ids"></a> [user\_group\_ids](#input\_user\_group\_ids) | User Group ID to associate with the replication group. Only a maximum of one (1) user group ID is valid | `list(string)` | `null` | no |
123127
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | Identifier of the VPC where the security group will be created | `string` | `null` | no |
124128

examples/memcached-cluster/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ Note that this example may create resources which will incur monetary charges on
2020
| Name | Version |
2121
|------|---------|
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
23-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.23 |
23+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.27 |
2424

2525
## Providers
2626

2727
| Name | Version |
2828
|------|---------|
29-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.23 |
29+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.27 |
3030

3131
## Modules
3232

examples/memcached-cluster/main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ locals {
2525
module "elasticache" {
2626
source = "../../"
2727

28-
cluster_id = local.name
29-
28+
cluster_id = local.name
3029
engine = "memcached"
3130
engine_version = "1.6.17"
3231
node_type = "cache.t4g.small"

examples/memcached-cluster/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 5.23"
7+
version = ">= 5.27"
88
}
99
}
1010
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# ElastiCache example for [Redis cluster mode](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/Replication.Redis-RedisCluster.html)
2+
3+
Configuration in this directory creates set of ElastiCaChe resources including replication group, subnet group and parameter group.
4+
5+
![Redis (Cluster Mode Disabled) vs. Redis (Cluster Mode Enabled)](https://docs.aws.amazon.com/images/AmazonElastiCache/latest/red-ug/images/ElastiCache-NodeGroups.png)
6+
7+
## Usage
8+
9+
To run this example you need to execute:
10+
11+
```bash
12+
$ terraform init
13+
$ terraform plan
14+
$ terraform apply
15+
```
16+
17+
Note that this example may create resources which will incur monetary charges on your AWS bill. Run `terraform destroy` when you no longer need these resources.
18+
19+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
20+
## Requirements
21+
22+
| Name | Version |
23+
|------|---------|
24+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
25+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.23 |
26+
27+
## Providers
28+
29+
| Name | Version |
30+
|------|---------|
31+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.23 |
32+
33+
## Modules
34+
35+
| Name | Source | Version |
36+
|------|--------|---------|
37+
| <a name="module_elasticache"></a> [elasticache](#module\_elasticache) | ../../ | n/a |
38+
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 5.0 |
39+
40+
## Resources
41+
42+
| Name | Type |
43+
|------|------|
44+
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
45+
46+
## Inputs
47+
48+
No inputs.
49+
50+
## Outputs
51+
52+
| Name | Description |
53+
|------|-------------|
54+
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | Arn of cloudwatch log group created |
55+
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of cloudwatch log group created |
56+
| <a name="output_cluster_address"></a> [cluster\_address](#output\_cluster\_address) | (Memcached only) DNS name of the cache cluster without the port appended |
57+
| <a name="output_cluster_arn"></a> [cluster\_arn](#output\_cluster\_arn) | The ARN of the ElastiCache Cluster |
58+
| <a name="output_cluster_cache_nodes"></a> [cluster\_cache\_nodes](#output\_cluster\_cache\_nodes) | List of node objects including `id`, `address`, `port` and `availability_zone` |
59+
| <a name="output_cluster_configuration_endpoint"></a> [cluster\_configuration\_endpoint](#output\_cluster\_configuration\_endpoint) | (Memcached only) Configuration endpoint to allow host discovery |
60+
| <a name="output_cluster_engine_version_actual"></a> [cluster\_engine\_version\_actual](#output\_cluster\_engine\_version\_actual) | Because ElastiCache pulls the latest minor or patch for a version, this attribute returns the running version of the cache engine |
61+
| <a name="output_parameter_group_arn"></a> [parameter\_group\_arn](#output\_parameter\_group\_arn) | The AWS ARN associated with the parameter group |
62+
| <a name="output_parameter_group_id"></a> [parameter\_group\_id](#output\_parameter\_group\_id) | The ElastiCache parameter group name |
63+
| <a name="output_rep_group_arn"></a> [rep\_group\_arn](#output\_rep\_group\_arn) | ARN of the created ElastiCache Replication Group |
64+
| <a name="output_rep_group_coniguration_endpoint_address"></a> [rep\_group\_coniguration\_endpoint\_address](#output\_rep\_group\_coniguration\_endpoint\_address) | Address of the replication group configuration endpoint when cluster mode is enabled |
65+
| <a name="output_rep_group_engine_version_actual"></a> [rep\_group\_engine\_version\_actual](#output\_rep\_group\_engine\_version\_actual) | Because ElastiCache pulls the latest minor or patch for a version, this attribute returns the running version of the cache engine |
66+
| <a name="output_rep_group_id"></a> [rep\_group\_id](#output\_rep\_group\_id) | ID of the ElastiCache Replication Group |
67+
| <a name="output_rep_group_member_clusters"></a> [rep\_group\_member\_clusters](#output\_rep\_group\_member\_clusters) | Identifiers of all the nodes that are part of this replication group |
68+
| <a name="output_rep_group_primary_endpoint_address"></a> [rep\_group\_primary\_endpoint\_address](#output\_rep\_group\_primary\_endpoint\_address) | Address of the endpoint for the primary node in the replication group, if the cluster mode is disabled |
69+
| <a name="output_rep_group_reader_endpoint_address"></a> [rep\_group\_reader\_endpoint\_address](#output\_rep\_group\_reader\_endpoint\_address) | Address of the endpoint for the reader node in the replication group, if the cluster mode is disabled |
70+
| <a name="output_security_group_arn"></a> [security\_group\_arn](#output\_security\_group\_arn) | Amazon Resource Name (ARN) of the security group |
71+
| <a name="output_security_group_id"></a> [security\_group\_id](#output\_security\_group\_id) | ID of the security group |
72+
| <a name="output_subnet_group_name"></a> [subnet\_group\_name](#output\_subnet\_group\_name) | The ElastiCache subnet group name |
73+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
74+
75+
Apache-2.0 Licensed. See [LICENSE](https://github.com/clowdhaus/terraform-aws-elasticache/blob/main/LICENSE).
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
provider "aws" {
2+
region = local.region
3+
}
4+
5+
data "aws_availability_zones" "available" {}
6+
7+
locals {
8+
region = "us-east-1"
9+
name = "ex-${basename(path.cwd)}"
10+
11+
vpc_cidr = "10.0.0.0/16"
12+
azs = slice(data.aws_availability_zones.available.names, 0, 3)
13+
14+
tags = {
15+
Name = local.name
16+
Example = local.name
17+
Repository = "https://github.com/clowdhaus/terraform-aws-elasticache"
18+
}
19+
}
20+
21+
################################################################################
22+
# ElastiCache Module
23+
################################################################################
24+
25+
module "elasticache" {
26+
source = "../../"
27+
28+
replication_group_id = local.name
29+
create_cluster = false
30+
create_replication_group = true
31+
engine_version = "7.1"
32+
node_type = "cache.t4g.small"
33+
34+
# Cluster mode
35+
cluster_mode_enabled = true
36+
num_node_groups = 2
37+
replicas_per_node_group = 3
38+
automatic_failover_enabled = true
39+
multi_az_enabled = true
40+
41+
user_group_ids = [module.elasticache_user_group.group_id]
42+
43+
# Security group
44+
vpc_id = module.vpc.vpc_id
45+
security_group_rules = {
46+
ingress_vpc = {
47+
# Default type is `ingress`
48+
# Default port is based on the default engine port
49+
description = "VPC traffic"
50+
cidr_ipv4 = module.vpc.vpc_cidr_block
51+
}
52+
}
53+
54+
# subnet group
55+
subnet_group_name = local.name
56+
subnet_group_description = "${title(local.name)} subnet group"
57+
subnet_ids = module.vpc.private_subnets
58+
59+
maintenance_window = "sun:05:00-sun:09:00"
60+
apply_immediately = true
61+
62+
# parameter group
63+
create_parameter_group = true
64+
parameter_group_family = "redis7"
65+
parameter_group_description = "${title(local.name)} parameter group"
66+
parameters = [
67+
{
68+
name = "latency-tracking"
69+
value = "yes"
70+
}
71+
]
72+
73+
tags = local.tags
74+
}
75+
76+
################################################################################
77+
# ElastiCache Module
78+
################################################################################
79+
80+
module "elasticache_user_group" {
81+
source = "../../modules/user-group"
82+
83+
user_group_id = local.name
84+
85+
default_user = {
86+
user_id = "default${lower(replace(local.name, "-", ""))}"
87+
passwords = ["password123456789"]
88+
}
89+
90+
users = {
91+
moe = {
92+
access_string = "on ~* +@all"
93+
passwords = ["password123456789"]
94+
}
95+
96+
larry = {
97+
access_string = "on ~* +@all"
98+
99+
authentication_mode = {
100+
type = "iam"
101+
}
102+
}
103+
104+
curly = {
105+
access_string = "on ~* +@all"
106+
107+
authentication_mode = {
108+
type = "password"
109+
passwords = ["password123456789", "password987654321"]
110+
}
111+
}
112+
}
113+
114+
tags = local.tags
115+
}
116+
117+
################################################################################
118+
# Supporting Resources
119+
################################################################################
120+
121+
module "vpc" {
122+
source = "terraform-aws-modules/vpc/aws"
123+
version = "~> 5.0"
124+
125+
name = local.name
126+
cidr = local.vpc_cidr
127+
128+
azs = local.azs
129+
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)]
130+
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 10)]
131+
132+
tags = local.tags
133+
}

0 commit comments

Comments
 (0)