Skip to content

Commit

Permalink
feat(subnet_group): Allow to pass an existing subnet group (#44)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
egarbi authored Aug 15, 2023
1 parent e2748d4 commit 75ddee5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ No modules.
| <a name="input_security_group_ids"></a> [security\_group\_ids](#input\_security\_group\_ids) | List of Security Groups. | `list(string)` | `[]` | no |
| <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 |
| <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 |
| <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 |
| <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 |
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | List of VPC Subnet IDs for the cache subnet group. | `list(string)` | `[]` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to all resources. | `map(string)` | `{}` | no |
| <a name="input_transit_encryption_enabled"></a> [transit\_encryption\_enabled](#input\_transit\_encryption\_enabled) | Whether to enable encryption in transit. | `bool` | `true` | no |
| <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 |
Expand Down
7 changes: 6 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
locals {
subnet_group_name = var.subnet_group_name != null ? var.subnet_group_name : aws_elasticache_subnet_group.redis[0].name
}

resource "aws_elasticache_replication_group" "redis" {
engine = var.global_replication_group_id == null ? "redis" : null

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
subnet_group_name = local.subnet_group_name
security_group_ids = concat(var.security_group_ids, [aws_security_group.redis.id])

preferred_cache_cluster_azs = var.preferred_cache_cluster_azs
Expand Down Expand Up @@ -87,6 +91,7 @@ resource "aws_elasticache_parameter_group" "redis" {
}

resource "aws_elasticache_subnet_group" "redis" {
count = var.subnet_group_name == null && length(var.subnet_ids) > 0 ? 1 : 0
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
7 changes: 7 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ variable "node_type" {
variable "subnet_ids" {
type = list(string)
description = "List of VPC Subnet IDs for the cache subnet group."
default = []
}

variable "subnet_group_name" {
type = string
description = "The name of the subnet group. If it is not specified, the module will create one for you"
default = null
}

variable "vpc_id" {
Expand Down

0 comments on commit 75ddee5

Please sign in to comment.