Skip to content

Commit 66a75fa

Browse files
committed
feat: add argument to control auth_token update strategy
1 parent 269e196 commit 66a75fa

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

examples/valkey/example.tf

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,36 @@ module "subnets" {
3838
ipv6_cidr_block = module.vpc.ipv6_cidr_block
3939
}
4040

41+
##----------------------------------------------------------------------------------
42+
## VALKEY MODULE CALL
43+
##----------------------------------------------------------------------------------
44+
module "secrets_manager" {
45+
source = "clouddrove/secrets-manager/aws"
46+
version = "2.0.0"
47+
48+
name = local.name
49+
environment = local.environment
50+
51+
unmanaged = true
52+
secrets = [
53+
{
54+
name = "aws/elasticache/auth-tokens"
55+
description = "Elasticache AUTH Token"
56+
recovery_window_in_days = 7
57+
secret_string = "{ \"auth_token\": \"UseSomethingSecure*1234\"}"
58+
}
59+
]
60+
}
61+
62+
data "aws_secretsmanager_secret" "auth_token" {
63+
depends_on = [module.secrets_manager]
64+
name = "aws/elasticache/auth-tokens"
65+
}
66+
67+
data "aws_secretsmanager_secret_version" "auth_token" {
68+
secret_id = data.aws_secretsmanager_secret.auth_token.id
69+
}
70+
4171
##----------------------------------------------------------------------------------
4272
## VALKEY MODULE CALL
4373
##----------------------------------------------------------------------------------
@@ -47,29 +77,29 @@ module "valkey" {
4777
name = local.name
4878
environment = local.environment
4979

50-
vpc_id = module.vpc.vpc_id
51-
allowed_ip = [module.vpc.vpc_cidr_block]
52-
allowed_ports = [6379]
80+
vpc_id = module.vpc.vpc_id
81+
allowed_ip = [module.vpc.vpc_cidr_block]
82+
allowed_ports = [6379]
83+
subnet_ids = concat(module.subnets.private_subnet_id, module.subnets.public_subnet_id)
84+
subnet_group_description = "${local.environment}-${local.name} subnet group."
85+
availability_zones = ["${local.region}a", "${local.region}c"]
5386

54-
# -- valkey configuration
5587
cluster_replication_enabled = true
5688
replication_group = {
5789
engine = "valkey"
5890
engine_version = "8.1"
5991
parameter_group_name = "default.valkey8"
6092
port = 6379
6193
num_cache_clusters = 2
62-
node_type = "cache.t3.medium"
94+
apply_immediately = true
95+
node_type = "cache.t3.micro"
6396
replication_group_description = "${local.environment}-${local.name} replication group."
64-
maintenance_window = "tue:07:00-tue:08:00"
97+
maintenance_window = "sat:03:30-sat:04:30"
6598
}
99+
az_mode = "single-az"
100+
kms_key_id = null # -- AWS Owned KMS Key
101+
auth_token = jsondecode(data.aws_secretsmanager_secret_version.auth_token.secret_string)["auth_token"]
102+
auth_token_update_strategy = "SET"
103+
sg_ids = [module.vpc.vpc_default_security_group_id]
66104

67-
az_mode = "single-az"
68-
num_cache_nodes = 2
69-
kms_key_id = null
70-
auth_token = "UseSomethingSecure*1234"
71-
# ---- valkey end -----------------
72-
subnet_ids = concat(module.subnets.private_subnet_id, module.subnets.public_subnet_id)
73-
subnet_group_description = "${local.environment}-${local.name} subnet group."
74-
availability_zones = ["${local.region}a", "${local.region}c"]
75-
}
105+
}

main.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,12 @@ resource "aws_elasticache_replication_group" "cluster" {
178178
multi_az_enabled = lookup(var.replication_group, "multi_az_enabled", false)
179179
network_type = var.network_type
180180

181-
auth_token = var.auth_token_enable ? (var.auth_token == null ? random_password.auth_token[0].result : var.auth_token) : ""
182-
kms_key_id = var.kms_key_id == "" ? join("", aws_kms_key.default[*].arn) : var.kms_key_id
183-
tags = module.labels.tags
184-
num_cache_clusters = lookup(var.replication_group, "num_cache_clusters", 1)
185-
user_group_ids = var.user_group_ids
181+
auth_token = var.auth_token_enable ? (var.auth_token == null ? random_password.auth_token[0].result : var.auth_token) : ""
182+
auth_token_update_strategy = var.auth_token_enable ? var.auth_token_update_strategy : null
183+
kms_key_id = var.kms_key_id == "" ? join("", aws_kms_key.default[*].arn) : var.kms_key_id
184+
tags = module.labels.tags
185+
num_cache_clusters = lookup(var.replication_group, "num_cache_clusters", 1)
186+
user_group_ids = var.user_group_ids
186187

187188
dynamic "log_delivery_configuration" {
188189
for_each = var.log_delivery_configuration

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ variable "auth_token" {
9999
description = "The password used to access a password protected server. Can be specified only if transit_encryption_enabled = true. Find auto generated auth_token in terraform.tfstate or in AWS SSM Parameter Store."
100100
}
101101

102+
variable "auth_token_update_strategy" {
103+
type = string
104+
default = null
105+
description = "(Optional) Strategy to use when updating the auth_token. Valid values are SET, ROTATE, and DELETE. Required if auth_token is set. Defaults to ROTATE"
106+
}
107+
102108
variable "cluster_replication_enabled" {
103109
type = bool
104110
default = false

0 commit comments

Comments
 (0)