Skip to content

Commit 5789921

Browse files
authored
Add support for global_replication_group_id (#19)
* Added supporting for global_replication_group_id * Added example
1 parent 9b31101 commit 5789921

File tree

4 files changed

+86
-11
lines changed

4 files changed

+86
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ No modules.
112112
| <a name="input_engine_version"></a> [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 |
113113
| <a name="input_family"></a> [family](#input\_family) | The family of the ElastiCache parameter group. | `string` | `"redis5.0"` | no |
114114
| <a name="input_final_snapshot_identifier"></a> [final\_snapshot\_identifier](#input\_final\_snapshot\_identifier) | The name of your final node group (shard) snapshot. ElastiCache creates the snapshot from the primary node in the cluster. If omitted, no final snapshot will be made. | `string` | `null` | no |
115+
| <a name="input_global_replication_group_id"></a> [global\_replication\_group\_id](#input\_global\_replication\_group\_id) | The ID of the global replication group to which this replication group should belong. | `string` | `null` | no |
115116
| <a name="input_ingress_cidr_blocks"></a> [ingress\_cidr\_blocks](#input\_ingress\_cidr\_blocks) | List of Ingress CIDR blocks. | `list(string)` | `[]` | no |
116117
| <a name="input_ingress_self"></a> [ingress\_self](#input\_ingress\_self) | Specify whether the security group itself will be added as a source to the ingress rule. | `bool` | `false` | no |
117118
| <a name="input_kms_key_id"></a> [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 |
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
provider "aws" {
6+
region = "eu-west-2"
7+
alias = "replica"
8+
}
9+
10+
#####
11+
# VPC and subnets
12+
#####
13+
14+
data "aws_vpc" "main" {
15+
default = true
16+
}
17+
18+
data "aws_vpc" "replica" {
19+
default = true
20+
21+
provider = aws.replica
22+
}
23+
24+
data "aws_subnet_ids" "main" {
25+
vpc_id = data.aws_vpc.main.id
26+
}
27+
28+
data "aws_subnet_ids" "replica" {
29+
vpc_id = data.aws_vpc.replica.id
30+
31+
provider = aws.replica
32+
}
33+
#####
34+
# Elasticache Redis
35+
#####
36+
37+
module "redis_main" {
38+
source = "../../"
39+
40+
name_prefix = "redis-replication-example"
41+
number_cache_clusters = 2
42+
node_type = "cache.m5.large"
43+
auth_token = "1234567890asdfghjkl"
44+
45+
subnet_ids = data.aws_subnet_ids.main.ids
46+
vpc_id = data.aws_vpc.main.id
47+
}
48+
49+
resource "aws_elasticache_global_replication_group" "this" {
50+
global_replication_group_id_suffix = "ha"
51+
primary_replication_group_id = module.redis_main.elasticache_replication_group_id
52+
}
53+
54+
module "redis_replica" {
55+
source = "../../"
56+
57+
name_prefix = "redis-replication-example"
58+
number_cache_clusters = 2
59+
node_type = "cache.m5.large"
60+
auth_token = "1234567890asdfghjkl"
61+
62+
subnet_ids = data.aws_subnet_ids.replica.ids
63+
vpc_id = data.aws_vpc.replica.id
64+
65+
global_replication_group_id = aws_elasticache_global_replication_group.this.global_replication_group_id
66+
67+
providers = { aws = aws.replica }
68+
}

main.tf

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
resource "aws_elasticache_replication_group" "redis" {
2-
engine = "redis"
2+
engine = var.global_replication_group_id == null ? "redis" : null
33

4-
parameter_group_name = aws_elasticache_parameter_group.redis.name
4+
parameter_group_name = var.global_replication_group_id == null ? aws_elasticache_parameter_group.redis.name : null
55
subnet_group_name = aws_elasticache_subnet_group.redis.name
66
security_group_ids = concat(var.security_group_ids, [aws_security_group.redis.id])
77

88
availability_zones = var.availability_zones
9-
replication_group_id = "${var.name_prefix}-redis"
9+
replication_group_id = var.global_replication_group_id == null ? "${var.name_prefix}-redis" : "${var.name_prefix}-redis-replica"
1010
number_cache_clusters = var.cluster_mode_enabled ? null : var.number_cache_clusters
11-
node_type = var.node_type
11+
node_type = var.global_replication_group_id == null ? var.node_type : null
1212

13-
engine_version = var.engine_version
13+
engine_version = var.global_replication_group_id == null ? var.engine_version : null
1414
port = var.port
1515

1616
maintenance_window = var.maintenance_window
@@ -21,10 +21,11 @@ resource "aws_elasticache_replication_group" "redis" {
2121
auto_minor_version_upgrade = var.auto_minor_version_upgrade
2222
multi_az_enabled = var.multi_az_enabled
2323

24-
at_rest_encryption_enabled = var.at_rest_encryption_enabled
25-
transit_encryption_enabled = var.transit_encryption_enabled
26-
auth_token = var.auth_token != "" ? var.auth_token : null
27-
kms_key_id = var.kms_key_id
24+
at_rest_encryption_enabled = var.global_replication_group_id == null ? var.at_rest_encryption_enabled : null
25+
transit_encryption_enabled = var.global_replication_group_id == null ? var.transit_encryption_enabled : null
26+
auth_token = var.auth_token != "" ? var.auth_token : null
27+
kms_key_id = var.kms_key_id
28+
global_replication_group_id = var.global_replication_group_id
2829

2930
apply_immediately = var.apply_immediately
3031

@@ -77,7 +78,7 @@ resource "aws_elasticache_parameter_group" "redis" {
7778
}
7879

7980
resource "aws_elasticache_subnet_group" "redis" {
80-
name = "${var.name_prefix}-redis-sg"
81+
name = var.global_replication_group_id == null ? "${var.name_prefix}-redis-sg" : "${var.name_prefix}-redis-sg-replica"
8182
subnet_ids = var.subnet_ids
8283
description = var.description
8384

@@ -130,4 +131,3 @@ resource "aws_security_group_rule" "redis_egress" {
130131
cidr_blocks = ["0.0.0.0/0"]
131132
security_group_id = aws_security_group.redis.id
132133
}
133-

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,9 @@ variable "final_snapshot_identifier" {
180180
description = "The name of your final node group (shard) snapshot. ElastiCache creates the snapshot from the primary node in the cluster. If omitted, no final snapshot will be made."
181181
default = null
182182
}
183+
184+
variable "global_replication_group_id" {
185+
description = "The ID of the global replication group to which this replication group should belong."
186+
type = string
187+
default = null
188+
}

0 commit comments

Comments
 (0)