Skip to content

Commit

Permalink
Add support for global_replication_group_id (#19)
Browse files Browse the repository at this point in the history
* Added supporting for global_replication_group_id

* Added example
  • Loading branch information
OuFinx authored Aug 11, 2021
1 parent 9b31101 commit 5789921
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ No modules.
| <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 |
| <a name="input_family"></a> [family](#input\_family) | The family of the ElastiCache parameter group. | `string` | `"redis5.0"` | no |
| <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 |
| <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 |
| <a name="input_ingress_cidr_blocks"></a> [ingress\_cidr\_blocks](#input\_ingress\_cidr\_blocks) | List of Ingress CIDR blocks. | `list(string)` | `[]` | no |
| <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 |
| <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 |
Expand Down
68 changes: 68 additions & 0 deletions examples/redis-replication-group/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
provider "aws" {
region = "eu-west-1"
}

provider "aws" {
region = "eu-west-2"
alias = "replica"
}

#####
# VPC and subnets
#####

data "aws_vpc" "main" {
default = true
}

data "aws_vpc" "replica" {
default = true

provider = aws.replica
}

data "aws_subnet_ids" "main" {
vpc_id = data.aws_vpc.main.id
}

data "aws_subnet_ids" "replica" {
vpc_id = data.aws_vpc.replica.id

provider = aws.replica
}
#####
# Elasticache Redis
#####

module "redis_main" {
source = "../../"

name_prefix = "redis-replication-example"
number_cache_clusters = 2
node_type = "cache.m5.large"
auth_token = "1234567890asdfghjkl"

subnet_ids = data.aws_subnet_ids.main.ids
vpc_id = data.aws_vpc.main.id
}

resource "aws_elasticache_global_replication_group" "this" {
global_replication_group_id_suffix = "ha"
primary_replication_group_id = module.redis_main.elasticache_replication_group_id
}

module "redis_replica" {
source = "../../"

name_prefix = "redis-replication-example"
number_cache_clusters = 2
node_type = "cache.m5.large"
auth_token = "1234567890asdfghjkl"

subnet_ids = data.aws_subnet_ids.replica.ids
vpc_id = data.aws_vpc.replica.id

global_replication_group_id = aws_elasticache_global_replication_group.this.global_replication_group_id

providers = { aws = aws.replica }
}
22 changes: 11 additions & 11 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
resource "aws_elasticache_replication_group" "redis" {
engine = "redis"
engine = var.global_replication_group_id == null ? "redis" : null

parameter_group_name = aws_elasticache_parameter_group.redis.name
parameter_group_name = var.global_replication_group_id == null ? aws_elasticache_parameter_group.redis.name : null
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.name_prefix}-redis"
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.node_type
node_type = var.global_replication_group_id == null ? var.node_type : null

engine_version = var.engine_version
engine_version = var.global_replication_group_id == null ? var.engine_version : null
port = var.port

maintenance_window = var.maintenance_window
Expand All @@ -21,10 +21,11 @@ resource "aws_elasticache_replication_group" "redis" {
auto_minor_version_upgrade = var.auto_minor_version_upgrade
multi_az_enabled = var.multi_az_enabled

at_rest_encryption_enabled = var.at_rest_encryption_enabled
transit_encryption_enabled = var.transit_encryption_enabled
auth_token = var.auth_token != "" ? var.auth_token : null
kms_key_id = var.kms_key_id
at_rest_encryption_enabled = var.global_replication_group_id == null ? var.at_rest_encryption_enabled : null
transit_encryption_enabled = var.global_replication_group_id == null ? var.transit_encryption_enabled : null
auth_token = var.auth_token != "" ? var.auth_token : null
kms_key_id = var.kms_key_id
global_replication_group_id = var.global_replication_group_id

apply_immediately = var.apply_immediately

Expand Down Expand Up @@ -77,7 +78,7 @@ resource "aws_elasticache_parameter_group" "redis" {
}

resource "aws_elasticache_subnet_group" "redis" {
name = "${var.name_prefix}-redis-sg"
name = var.global_replication_group_id == null ? "${var.name_prefix}-redis-sg" : "${var.name_prefix}-redis-sg-replica"
subnet_ids = var.subnet_ids
description = var.description

Expand Down Expand Up @@ -130,4 +131,3 @@ resource "aws_security_group_rule" "redis_egress" {
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.redis.id
}

6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,9 @@ variable "final_snapshot_identifier" {
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."
default = null
}

variable "global_replication_group_id" {
description = "The ID of the global replication group to which this replication group should belong."
type = string
default = null
}

0 comments on commit 5789921

Please sign in to comment.