Skip to content

SREFTP-4733 - Update code from upstream changes, include fix for parameter group versioning bug #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7af8bb0
Added terraform011 note in README
antonbabenko May 22, 2020
e88714e
fix: conditionally update random_id (#38)
pathouse May 22, 2020
2f43d57
fix: Use lookup() for VPC name getting (#40)
rinrailin May 22, 2020
13fbc70
docs: Updated README
antonbabenko May 22, 2020
b3ec28e
feat: Add new arguments support (#43)
rinrailin May 23, 2020
9a58d76
docs: Fixed README
antonbabenko May 23, 2020
cf261f5
Fix source instructions in readme (#45)
LucaPrete Jul 20, 2020
9752fe5
Terraform 0.15.* updates (#50)
Stretch96 May 11, 2021
fbe0f7d
multiaz enable add (#48)
omeroner Jun 7, 2021
6f3d160
fix: Make aws_elasticache_parameter_group to use redis6.x as value fo…
sharath-sequoia Aug 15, 2022
87373b2
fix issue #34
vicendominguez Jan 21, 2020
ba54b5f
Adds support for multi_az clustering
Mar 10, 2021
a316374
Adds support for replicating from another Redis instance
Mar 10, 2021
ecf49b9
[PLATOP-2685] Use AWS provider v4
joseluis-fw Nov 28, 2022
0f35141
Remove duplicated argument keepers
akae Oct 4, 2023
2e96e5e
Josele Fix for versions in parameter group family
akae Oct 4, 2023
63962e0
indentation
akae Oct 4, 2023
686f39e
Merge branch 'master' into master
akae Oct 4, 2023
092f7d6
Update providers definition
akae Oct 5, 2023
538f45b
Update parameter naming
akae Oct 5, 2023
3f38950
Update gitignore
akae Oct 5, 2023
f4502c0
Update Readme
akae Oct 5, 2023
5799982
Add pre-commit hook tags
akae Oct 5, 2023
17e7b76
Add outputs descriptions
akae Oct 5, 2023
a0020e9
apply terraform fmt
akae Oct 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
repos:
- repo: git://github.com/antonbabenko/pre-commit-terraform
rev: v1.21.0
rev: v1.30.0
hooks:
- id: terraform_fmt
- id: terraform_docs
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
rev: v3.1.0
hooks:
- id: check-merge-conflict
77 changes: 47 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ This module

Terraform 0.12. Pin module version to `~> v2.0`. Submit pull-requests to `master` branch.

Terraform 0.11. Pin module version to `~> v1.0`.
Terraform 0.11. Pin module version to `~> v1.0`. Submit pull-requests to `terraform011` branch.

## Usage

```hcl
module "redis" {
source = "github.com/terraform-community-modules/tf_aws_elasticache_redis.git"
version = "~> 2.0"
source = "github.com/terraform-community-modules/tf_aws_elasticache_redis.git?ref=v2.2.0"

env = "dev"
name = "thtest"
Expand All @@ -41,41 +40,59 @@ module "redis" {
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| aws | n/a |
| random | n/a |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| alarm\_actions | | list | n/a | yes |
| alarm\_cpu\_threshold | These vars would be used by cloudwatch.tf and should be uncommented if we decide to use them. | string | `"75"` | no |
| alarm\_memory\_threshold | | string | `"10000000"` | no |
| allowed\_cidr | A list of Security Group ID's to allow access to. | list(string) | `[ "127.0.0.1/32" ]` | no |
| allowed\_security\_groups | A list of Security Group ID's to allow access to. | list(string) | `[]` | no |
| apply\_immediately | Specifies whether any modifications are applied immediately, or during the next maintenance window. Default is false. | bool | `"false"` | no |
| env | env to deploy into, should typically dev/staging/prod | string | n/a | yes |
| name | Name for the Redis replication group i.e. UserObject | string | n/a | yes |
| redis\_clusters | Number of Redis cache clusters (nodes) to create | string | n/a | yes |
| redis\_failover | | bool | `"false"` | no |
| redis\_maintenance\_window | Specifies the weekly time range for when maintenance on the cache cluster is performed. The format is ddd:hh24:mi-ddd:hh24:mi (24H Clock UTC). The minimum maintenance window is a 60 minute period | string | `"fri:08:00-fri:09:00"` | no |
| redis\_node\_type | Instance type to use for creating the Redis cache clusters | string | `"cache.m3.medium"` | no |
| redis\_parameters | additional parameters modifyed in parameter group | list(map(any)) | `[]` | no |
| redis\_port | | number | `"6379"` | no |
| redis\_snapshot\_retention\_limit | The number of days for which ElastiCache will retain automatic cache cluster snapshots before deleting them. For example, if you set SnapshotRetentionLimit to 5, then a snapshot that was taken today will be retained for 5 days before being deleted. If the value of SnapshotRetentionLimit is set to zero (0), backups are turned off. Please note that setting a snapshot_retention_limit is not supported on cache.t1.micro or cache.t2.* cache nodes | number | `"0"` | no |
| redis\_snapshot\_window | The daily time range (in UTC) during which ElastiCache will begin taking a daily snapshot of your cache cluster. The minimum snapshot window is a 60 minute period | string | `"06:30-07:30"` | no |
| redis\_version | Redis version to use, defaults to 3.2.10 | string | `"3.2.10"` | no |
| subnets | List of VPC Subnet IDs for the cache subnet group | list(string) | n/a | yes |
| tags | Tags for redis nodes | map(string) | `{}` | no |
| vpc\_id | VPC ID | string | n/a | yes |
|------|-------------|------|---------|:--------:|
| allowed\_cidr | A list CIDRs to allow access to. | `list(string)` | <pre>[<br> "127.0.0.1/32"<br>]</pre> | no |
| allowed\_security\_groups | A list of Security Group ID's to allow access to. | `list(string)` | `[]` | no |
| apply\_immediately | Specifies whether any modifications are applied immediately, or during the next maintenance window. Default is false. | `bool` | `false` | no |
| at\_rest\_encryption\_enabled | Whether to enable encryption at rest | `bool` | `false` | no |
| auth\_token | The password used to access a password protected server. Can be specified only if transit\_encryption\_enabled = true. If specified must contain from 16 to 128 alphanumeric characters or symbols | `string` | `null` | no |
| auto\_minor\_version\_upgrade | Specifies whether a minor engine upgrades will be applied automatically to the underlying Cache Cluster instances during the maintenance window | `bool` | `true` | no |
| availability\_zones | A list of EC2 availability zones in which the replication group's cache clusters will be created. The order of the availability zones in the list is not important | `list(string)` | `[]` | no |
| env | env to deploy into, should typically dev/staging/prod | `string` | n/a | yes |
| 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 |
| name | Name for the Redis replication group i.e. UserObject | `string` | n/a | yes |
| notification\_topic\_arn | An Amazon Resource Name (ARN) of an SNS topic to send ElastiCache notifications to. Example: arn:aws:sns:us-east-1:012345678999:my\_sns\_topic | `string` | `""` | no |
| redis\_clusters | Number of Redis cache clusters (nodes) to create | `string` | n/a | yes |
| redis\_failover | n/a | `bool` | `false` | no |
| redis\_maintenance\_window | Specifies the weekly time range for when maintenance on the cache cluster is performed. The format is ddd:hh24:mi-ddd:hh24:mi (24H Clock UTC). The minimum maintenance window is a 60 minute period | `string` | `"fri:08:00-fri:09:00"` | no |
| redis\_node\_type | Instance type to use for creating the Redis cache clusters | `string` | `"cache.m3.medium"` | no |
| redis\_parameters | additional parameters modifyed in parameter group | `list(map(any))` | `[]` | no |
| redis\_port | n/a | `number` | `6379` | no |
| redis\_snapshot\_retention\_limit | The number of days for which ElastiCache will retain automatic cache cluster snapshots before deleting them. For example, if you set SnapshotRetentionLimit to 5, then a snapshot that was taken today will be retained for 5 days before being deleted. If the value of SnapshotRetentionLimit is set to zero (0), backups are turned off. Please note that setting a snapshot\_retention\_limit is not supported on cache.t1.micro or cache.t2.\* cache nodes | `number` | `0` | no |
| redis\_snapshot\_window | The daily time range (in UTC) during which ElastiCache will begin taking a daily snapshot of your cache cluster. The minimum snapshot window is a 60 minute period | `string` | `"06:30-07:30"` | no |
| redis\_version | Redis version to use, defaults to 3.2.10 | `string` | `"3.2.10"` | no |
| security\_group\_names | A list of cache security group names to associate with this replication group | `list(string)` | `[]` | no |
| snapshot\_arns | A single-element string list containing an Amazon Resource Name (ARN) of a Redis RDB snapshot file stored in Amazon S3. Example: arn:aws:s3:::my\_bucket/snapshot1.rdb | `list(string)` | `[]` | no |
| snapshot\_name | The name of a snapshot from which to restore data into the new node group. Changing the snapshot\_name forces a new resource | `string` | `""` | no |
| subnets | List of VPC Subnet IDs for the cache subnet group | `list(string)` | n/a | yes |
| tags | Tags for redis nodes | `map(string)` | `{}` | no |
| transit\_encryption\_enabled | Whether to enable encryption in transit. Requires 3.2.6 or >=4.0 redis\_version | `bool` | `false` | no |
| vpc\_id | VPC ID | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| endpoint | |
| id | |
| parameter\_group | |
| port | |
| redis\_security\_group\_id | |
| redis\_subnet\_group\_name | |
| endpoint | n/a |
| id | n/a |
| parameter\_group | n/a |
| port | n/a |
| redis\_security\_group\_id | n/a |
| redis\_subnet\_group\_name | n/a |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Expand Down
4 changes: 2 additions & 2 deletions cloudwatch.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
count = "${var.redis_clusters}"

alarm_name = "alarm-${var.name}-${data.aws_vpc.vpc.tags["Name"]}-CacheCluster00${count.index + 1}CPUUtilization"
alarm_name = "alarm-${var.name}-${local.vpc_name}-CacheCluster00${count.index + 1}CPUUtilization"
alarm_description = "Redis cluster CPU utilization"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
Expand All @@ -27,7 +27,7 @@ resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
resource "aws_cloudwatch_metric_alarm" "cache_memory" {
count = "${var.redis_clusters}"

alarm_name = "alarm-${var.name}-${data.aws_vpc.vpc.tags["Name"]}-CacheCluster00${count.index + 1}FreeableMemory"
alarm_name = "alarm-${var.name}-${local.vpc_name}-CacheCluster00${count.index + 1}FreeableMemory"
alarm_description = "Redis cluster freeable memory"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
Expand Down
29 changes: 23 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ data "aws_vpc" "vpc" {
id = var.vpc_id
}

locals {
vpc_name = lookup(data.aws_vpc.vpc.tags, "Name", var.vpc_id)
major_redis_version = parseint(substr(var.redis_version, 0,1),10)
parameter_group_family = local.major_redis_version < 6 ? "redis${replace(var.redis_version, "/\\.[\\d]+$/", "")}" : local.major_redis_version == 6 ? "redis${replace(var.redis_version, "/\\.[\\d|x]+$/", "")}.x" : "redis${local.major_redis_version}"
}

resource "random_id" "salt" {
keepers = {
redis_version = "${var.redis_version}"
Expand All @@ -11,30 +17,41 @@ resource "random_id" "salt" {

resource "aws_elasticache_replication_group" "redis" {
replication_group_id = format("%.20s", "${var.name}-${var.env}")
replication_group_description = "Terraform-managed ElastiCache replication group for ${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}"
replication_group_description = "Terraform-managed ElastiCache replication group for ${var.name}-${var.env}-${local.vpc_name}"
number_cache_clusters = var.redis_clusters
node_type = var.redis_node_type
automatic_failover_enabled = var.redis_failover
auto_minor_version_upgrade = var.auto_minor_version_upgrade
availability_zones = var.availability_zones
multi_az_enabled = var.multi_az_enabled
engine = "redis"
at_rest_encryption_enabled = var.at_rest_encryption_enabled
kms_key_id = var.kms_key_id
transit_encryption_enabled = var.transit_encryption_enabled
auth_token = var.transit_encryption_enabled ? var.auth_token : null
engine_version = var.redis_version
port = var.redis_port
parameter_group_name = aws_elasticache_parameter_group.redis_parameter_group.id
subnet_group_name = aws_elasticache_subnet_group.redis_subnet_group.id
security_group_names = var.security_group_names
security_group_ids = [aws_security_group.redis_security_group.id]
snapshot_arns = var.snapshot_arns
snapshot_name = var.snapshot_name
apply_immediately = var.apply_immediately
maintenance_window = var.redis_maintenance_window
notification_topic_arn = var.notification_topic_arn
snapshot_window = var.redis_snapshot_window
snapshot_retention_limit = var.redis_snapshot_retention_limit
tags = merge(map("Name", format("tf-elasticache-%s-%s", var.name, lookup(data.aws_vpc.vpc.tags, "Name", ""))), var.tags)
tags = merge(tomap({"Name" = format("tf-elasticache-%s-%s", var.name, local.vpc_name)}), var.tags)
}

resource "aws_elasticache_parameter_group" "redis_parameter_group" {
name = replace(format("%.255s", lower(replace("tf-redis-${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}-${random_id.salt.hex}", "_", "-"))), "/\\s/", "-")
name = replace(format("%.255s", lower(replace("tf-redis-${var.name}-${var.env}-${local.vpc_name}-${random_id.salt.hex}", "_", "-"))), "/\\s/", "-")

description = "Terraform-managed ElastiCache parameter group for ${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}"
description = "Terraform-managed ElastiCache parameter group for ${var.name}-${var.env}-${local.vpc_name}"

# Strip the patch version from redis_version var
family = "redis${replace(var.redis_version, "/\\.[\\d]+$/", "")}"
family = local.parameter_group_family
dynamic "parameter" {
for_each = var.redis_parameters
content {
Expand All @@ -49,6 +66,6 @@ resource "aws_elasticache_parameter_group" "redis_parameter_group" {
}

resource "aws_elasticache_subnet_group" "redis_subnet_group" {
name = replace(format("%.255s", lower(replace("tf-redis-${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}", "_", "-"))), "/\\s/", "-")
name = replace(format("%.255s", lower(replace("tf-redis-${var.name}-${var.env}-${local.vpc_name}", "_", "-"))), "/\\s/", "-")
subnet_ids = var.subnets
}
6 changes: 3 additions & 3 deletions security_groups.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
resource "aws_security_group" "redis_security_group" {
name = format("%.255s", "tf-sg-ec-${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}")
description = "Terraform-managed ElastiCache security group for ${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}"
name = format("%.255s", "tf-sg-ec-${var.name}-${var.env}-${local.vpc_name}")
description = "Terraform-managed ElastiCache security group for ${var.name}-${var.env}-${local.vpc_name}"
vpc_id = data.aws_vpc.vpc.id

tags = {
Name = "tf-sg-ec-${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}"
Name = "tf-sg-ec-${var.name}-${var.env}-${local.vpc_name}"
}
}

Expand Down
60 changes: 60 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,63 @@ variable "tags" {
type = map(string)
default = {}
}

variable "auto_minor_version_upgrade" {
description = "Specifies whether a minor engine upgrades will be applied automatically to the underlying Cache Cluster instances during the maintenance window"
type = bool
default = true
}

variable "availability_zones" {
description = "A list of EC2 availability zones in which the replication group's cache clusters will be created. The order of the availability zones in the list is not important"
type = list(string)
default = []
}

variable "at_rest_encryption_enabled" {
description = "Whether to enable encryption at rest"
type = bool
default = false
}

variable "kms_key_id" {
description = "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"
type = string
default = ""
}

variable "transit_encryption_enabled" {
description = "Whether to enable encryption in transit. Requires 3.2.6 or >=4.0 redis_version"
type = bool
default = false
}

variable "auth_token" {
description = "The password used to access a password protected server. Can be specified only if transit_encryption_enabled = true. If specified must contain from 16 to 128 alphanumeric characters or symbols"
type = string
default = null
}

variable "security_group_names" {
description = "A list of cache security group names to associate with this replication group"
type = list(string)
default = []
}

variable "snapshot_arns" {
description = "A single-element string list containing an Amazon Resource Name (ARN) of a Redis RDB snapshot file stored in Amazon S3. Example: arn:aws:s3:::my_bucket/snapshot1.rdb"
type = list(string)
default = []
}

variable "snapshot_name" {
description = " The name of a snapshot from which to restore data into the new node group. Changing the snapshot_name forces a new resource"
type = string
default = ""
}

variable "notification_topic_arn" {
description = "An Amazon Resource Name (ARN) of an SNS topic to send ElastiCache notifications to. Example: arn:aws:sns:us-east-1:012345678999:my_sns_topic"
type = string
default = ""
}