Skip to content

Commit 59b4d1b

Browse files
committed
Feat: Added the random password resource block to genrate the auth token
1 parent 2c960b8 commit 59b4d1b

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

_example/redis-cluster/main.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ module "subnets" {
3737
ipv6_cidr_block = module.vpc.ipv6_cidr_block
3838
}
3939

40-
####----------------------------------------------------------------------------------
41-
## Amazon ElastiCache [REDIS-CLUSTER] is a fully managed in-memory data store and cache service by Amazon Web Services.
42-
## The service improves the performance of web applications by retrieving information from managed in-memory caches,
43-
## instead of relying entirely on slower disk-based databases.
44-
####----------------------------------------------------------------------------------
40+
###----------------------------------------------------------------------------------
41+
# Amazon ElastiCache [REDIS-CLUSTER] is a fully managed in-memory data store and cache service by Amazon Web Services.
42+
# The service improves the performance of web applications by retrieving information from managed in-memory caches,
43+
# instead of relying entirely on slower disk-based databases.
44+
###----------------------------------------------------------------------------------
4545
module "redis-cluster" {
4646
source = "./../../"
4747

_example/redis-cluster/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ output "redis_ssm_arn" {
2222
value = module.redis-cluster.redis_ssm_name
2323
description = "A map of the names and ARNs created"
2424
}
25+
26+
output "auth_token" {
27+
value = module.redis-cluster.auth_token
28+
sensitive = true
29+
}

main.tf

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ resource "aws_elasticache_subnet_group" "default" {
137137
tags = module.labels.tags
138138
}
139139

140+
##----------------------------------------------------------------------------------
141+
## Below resource will create random passoword for the auth_token
142+
##----------------------------------------------------------------------------------
143+
144+
resource "random_password" "auth_token" {
145+
count = var.auth_token_enable && var.auth_token == null ? 1 : 0
146+
length = 25
147+
special = false
148+
}
149+
140150
##----------------------------------------------------------------------------------
141151
## Below resource will create replication-group resource for redis-cluster and memcached.
142152
##----------------------------------------------------------------------------------
@@ -165,7 +175,7 @@ resource "aws_elasticache_replication_group" "cluster" {
165175
at_rest_encryption_enabled = var.at_rest_encryption_enabled
166176
transit_encryption_enabled = var.transit_encryption_enabled
167177
multi_az_enabled = var.multi_az_enabled
168-
auth_token = var.auth_token
178+
auth_token = var.auth_token_enable ? ( var.auth_token == null ? random_password.auth_token[0].result : var.auth_token ) : null
169179
kms_key_id = var.kms_key_id == "" ? join("", aws_kms_key.default[*].arn) : var.kms_key_id
170180
tags = module.labels.tags
171181
num_cache_clusters = var.num_cache_clusters
@@ -223,15 +233,15 @@ resource "aws_route53_record" "elasticache" {
223233
}
224234

225235
##----------------------------------------------------------------------------------
226-
## Below resource will create ssm-parameter resource for redisand memcached with auth-token.
236+
## Below resource will create ssm-parameter resource for redis and memcached with auth-token.
227237
##----------------------------------------------------------------------------------
228238
resource "aws_ssm_parameter" "secret" {
229-
count = var.auth_token != null ? 1 : 0
239+
count = var.auth_token_enable ? 1 : 0
230240

231241
name = format("/%s/%s/auth-token", var.environment, var.name)
232242
description = var.ssm_parameter_description
233243
type = var.ssm_parameter_type
234-
value = var.auth_token
244+
value = var.auth_token == null ? random_password.auth_token[0].result : var.auth_token
235245
key_id = var.kms_key_id == "" ? join("", aws_kms_key.default[*].arn) : var.kms_key_id
236246
}
237247

outputs.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,9 @@ output "Memcached_ssm_name" {
5959
value = join("", aws_ssm_parameter.memcached_secret-endpoint[*].name)
6060
description = "A list of all of the parameter values"
6161
}
62+
63+
output "auth_token" {
64+
value = random_password.auth_token[0].result
65+
sensitive = true
66+
description = "Auth token generated value"
67+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ variable "transit_encryption_enabled" {
175175
description = "Whether to enable encryption in transit."
176176
}
177177

178+
variable "auth_token_enable" {
179+
type = bool
180+
default = true
181+
description = "Flag to specify whether to create auth token (password) protected cluster. Can be specified only if transit_encryption_enabled = true."
182+
}
183+
178184
variable "auth_token" {
179185
type = string
180186
default = null

0 commit comments

Comments
 (0)