Skip to content

Commit 75ddee5

Browse files
authored
feat(subnet_group): Allow to pass an existing subnet group (#44)
Create a dedicated subnet group per cluster is most of the time not required. This is also useful when importing manually created resources into terraform Also - pre-commit hook updated because a bug - docs are up-to-date now
1 parent e2748d4 commit 75ddee5

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ No modules.
145145
| <a name="input_security_group_ids"></a> [security\_group\_ids](#input\_security\_group\_ids) | List of Security Groups. | `list(string)` | `[]` | no |
146146
| <a name="input_snapshot_retention_limit"></a> [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 |
147147
| <a name="input_snapshot_window"></a> [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 |
148-
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | List of VPC Subnet IDs for the cache subnet group. | `list(string)` | n/a | yes |
148+
| <a name="input_subnet_group_name"></a> [subnet\_group\_name](#input\_subnet\_group\_name) | The name of the subnet group. If it is not specified, the module will create one for you | `string` | `null` | no |
149+
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | List of VPC Subnet IDs for the cache subnet group. | `list(string)` | `[]` | no |
149150
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to all resources. | `map(string)` | `{}` | no |
150151
| <a name="input_transit_encryption_enabled"></a> [transit\_encryption\_enabled](#input\_transit\_encryption\_enabled) | Whether to enable encryption in transit. | `bool` | `true` | no |
151152
| <a name="input_user_group_ids"></a> [user\_group\_ids](#input\_user\_group\_ids) | User Group ID to associate with the replication group | `list(string)` | `null` | no |

main.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
locals {
2+
subnet_group_name = var.subnet_group_name != null ? var.subnet_group_name : aws_elasticache_subnet_group.redis[0].name
3+
}
4+
15
resource "aws_elasticache_replication_group" "redis" {
26
engine = var.global_replication_group_id == null ? "redis" : null
37

48
parameter_group_name = var.global_replication_group_id == null ? aws_elasticache_parameter_group.redis.name : null
5-
subnet_group_name = aws_elasticache_subnet_group.redis.name
9+
subnet_group_name = local.subnet_group_name
610
security_group_ids = concat(var.security_group_ids, [aws_security_group.redis.id])
711

812
preferred_cache_cluster_azs = var.preferred_cache_cluster_azs
@@ -87,6 +91,7 @@ resource "aws_elasticache_parameter_group" "redis" {
8791
}
8892

8993
resource "aws_elasticache_subnet_group" "redis" {
94+
count = var.subnet_group_name == null && length(var.subnet_ids) > 0 ? 1 : 0
9095
name = var.global_replication_group_id == null ? "${var.name_prefix}-redis-sg" : "${var.name_prefix}-redis-sg-replica"
9196
subnet_ids = var.subnet_ids
9297
description = var.description

variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ variable "node_type" {
2323
variable "subnet_ids" {
2424
type = list(string)
2525
description = "List of VPC Subnet IDs for the cache subnet group."
26+
default = []
27+
}
28+
29+
variable "subnet_group_name" {
30+
type = string
31+
description = "The name of the subnet group. If it is not specified, the module will create one for you"
32+
default = null
2633
}
2734

2835
variable "vpc_id" {

0 commit comments

Comments
 (0)