Skip to content

Commit 07023d1

Browse files
Merge pull request #57 from clouddrove/feature/random_pass
Feature/random pass
2 parents 2c960b8 + 6fec54b commit 07023d1

File tree

7 files changed

+56
-21
lines changed

7 files changed

+56
-21
lines changed
File renamed without changes.

_example/redis-cluster/main.tf renamed to _example/redis-cluster/example.tf

+12-11
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ 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

4848
name = "redis-cluster"
4949
environment = "test"
5050
label_order = ["environment", "name"]
5151

52-
####----------------------------------------------------------------------------------
53-
## Below A security group controls the traffic that is allowed to reach and leave the resources that it is associated with.
54-
####----------------------------------------------------------------------------------
52+
###----------------------------------------------------------------------------------
53+
# Below A security group controls the traffic that is allowed to reach and leave the resources that it is associated with.
54+
###----------------------------------------------------------------------------------
5555
vpc_id = module.vpc.vpc_id
5656
allowed_ip = [module.vpc.vpc_cidr_block]
5757
allowed_ports = [6379]
@@ -71,9 +71,10 @@ module "redis-cluster" {
7171
Application = "CloudDrove"
7272
}
7373

74-
####----------------------------------------------------------------------------------
75-
## will create ROUTE-53 for redis which will add the dns of the cluster.
76-
####----------------------------------------------------------------------------------
74+
75+
###----------------------------------------------------------------------------------
76+
# will create ROUTE-53 for redis which will add the dns of the cluster.
77+
###----------------------------------------------------------------------------------
7778
route53_record_enabled = false
7879
ssm_parameter_endpoint_enabled = false
7980
dns_record_name = "prod"

_example/redis-cluster/outputs.tf

+5
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+
}
File renamed without changes.

main.tf

+14-4
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 = var.length
147+
special = var.special
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

+6
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

+19-6
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
@@ -324,12 +330,6 @@ variable "enable_security_group" {
324330
description = "Enable default Security Group with only Egress traffic allowed."
325331
}
326332

327-
variable "existing_sg_id" {
328-
type = string
329-
default = null
330-
description = "Provide existing security group id for updating existing rule"
331-
}
332-
333333
variable "egress_rule" {
334334
type = bool
335335
default = true
@@ -432,3 +432,16 @@ variable "ssm_parameter_type" {
432432
default = "SecureString"
433433
description = "Type of the parameter."
434434
}
435+
436+
###------------------------------- random_password----------------------------
437+
438+
variable "length" {
439+
type = number
440+
default = 25
441+
}
442+
443+
variable "special" {
444+
type = bool
445+
default = false
446+
447+
}

0 commit comments

Comments
 (0)