Skip to content

Commit 13f21a8

Browse files
committed
Added variable to store credential at secret manager and pass custom credentials
1 parent 6b722ef commit 13f21a8

File tree

7 files changed

+86
-58
lines changed

7 files changed

+86
-58
lines changed

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,30 @@ This module creates a Redis master and one or more Redis slaves, depending on th
1414

1515
| Redis Helm Chart Version | K8s supported version |
1616
| :-----: | :--- |
17-
| **16.13.2** | **1.23,1.24,1.25** |
17+
| **16.13.2** | **1.23,1.24,1.25,1.26,1.27** |
1818

1919
## Usage Example
2020

2121
```hcl
2222
module "redis" {
2323
source = "squareops/redis/kubernetes"
2424
redis_config = {
25-
name = "redis"
26-
values_yaml = ""
27-
environment = "prod"
28-
architecture = "replication"
29-
slave_volume_size = "10Gi"
30-
master_volume_size = "10Gi"
31-
storage_class_name = "gp3"
32-
slave_replica_count = 2
25+
name = "redis"
26+
values_yaml = ""
27+
environment = "prod"
28+
architecture = "replication"
29+
slave_volume_size = "10Gi"
30+
master_volume_size = "10Gi"
31+
storage_class_name = "gp3"
32+
slave_replica_count = 2
33+
store_password_to_secret_manager = true
3334
}
3435
grafana_monitoring_enabled = true
3536
recovery_window_aws_secret = 0
37+
custom_credentials_enabled = true
38+
custom_credentials_config = {
39+
password = "aajdhgduy3873683dh"
40+
}
3641
}
3742
3843
```

examples/complete/main.tf

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@ locals {
77
Expires = "Never"
88
Department = "Engineering"
99
}
10+
store_password_to_secret_manager = true
1011
}
1112

1213
module "redis" {
1314
source = "squareops/redis/kubernetes"
1415
redis_config = {
15-
name = local.name
16-
values_yaml = file("./helm/values.yaml")
17-
environment = local.environment
18-
architecture = "replication"
19-
slave_volume_size = "10Gi"
20-
master_volume_size = "10Gi"
21-
storage_class_name = "gp3"
22-
slave_replica_count = 2
16+
name = local.name
17+
values_yaml = file("./helm/values.yaml")
18+
environment = local.environment
19+
architecture = "replication"
20+
slave_volume_size = "10Gi"
21+
master_volume_size = "10Gi"
22+
storage_class_name = "gp3"
23+
slave_replica_count = 2
24+
store_password_to_secret_manager = local.store_password_to_secret_manager
2325
}
2426
grafana_monitoring_enabled = true
2527
recovery_window_aws_secret = 0
28+
custom_credentials_enabled = true
29+
custom_credentials_config = {
30+
password = "aajdhgduy3873683dh"
31+
}
2632
}

examples/complete/output.tf

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
output "redis_port" {
2-
value = "6379"
3-
description = "The port number on which Redis is running."
1+
output "redis_endpoints" {
2+
description = "Redis endpoints in the Kubernetes cluster."
3+
value = module.redis.redis_endpoints
44
}
55

6-
output "redis_master_endpoint" {
7-
value = module.redis.redis_master_endpoint
8-
description = "The endpoint for the Redis Master Service, which is the primary node in the Redis cluster responsible for handling read-write operations."
9-
}
10-
11-
output "redis_slave_endpoint" {
12-
value = module.redis.redis_slave_endpoint
13-
description = "The endpoint for the Redis Slave Service, which is a secondary node in the Redis cluster responsible for handling read-only operations."
6+
output "redis_credential" {
7+
description = "Redis credentials used for accessing the database."
8+
value = local.store_password_to_secret_manager ? null : module.redis.redis_credential
149
}

examples/complete/provider.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ provider "kubernetes" {
1818
host = data.aws_eks_cluster.cluster.endpoint
1919
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
2020
token = data.aws_eks_cluster_auth.cluster.token
21-
2221
}
2322

2423
provider "helm" {
2524
kubernetes {
2625
host = data.aws_eks_cluster.cluster.endpoint
2726
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
2827
token = data.aws_eks_cluster_auth.cluster.token
29-
3028
}
3129
}

main.tf

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
resource "random_password" "redis_password" {
2+
count = var.custom_credentials_enabled ? 0 : 1
23
length = 20
34
special = false
45
}
56

67
resource "aws_secretsmanager_secret" "redis_password" {
8+
count = var.redis_config.store_password_to_secret_manager ? 1 : 0
79
name = format("%s/%s/%s", var.redis_config.environment, var.redis_config.name, "redis")
810
recovery_window_in_days = var.recovery_window_aws_secret
911
}
1012

1113
resource "aws_secretsmanager_secret_version" "redis_password" {
12-
secret_id = aws_secretsmanager_secret.redis_password.id
13-
secret_string = <<EOF
14-
{
15-
"username": "root",
16-
"password": "${random_password.redis_password.result}"
17-
}
18-
EOF
14+
count = var.redis_config.store_password_to_secret_manager ? 1 : 0
15+
secret_id = aws_secretsmanager_secret.redis_password[0].id
16+
secret_string = var.custom_credentials_enabled ? jsonencode(
17+
{
18+
"redis_username" : "root",
19+
"redis_password" : "${var.custom_credentials_config.password}"
20+
21+
}) : jsonencode(
22+
{
23+
"redis_username" : "root",
24+
"redis_password" : "${random_password.redis_password[0].result}"
25+
})
1926
}
2027

2128
resource "kubernetes_namespace" "redis" {
@@ -38,7 +45,7 @@ resource "helm_release" "redis" {
3845
templatefile("${path.module}/helm/values/values.yaml", {
3946
app_version = var.app_version,
4047
architecture = var.redis_config.architecture,
41-
redis_password = random_password.redis_password.result,
48+
redis_password = var.custom_credentials_enabled ? var.custom_credentials_config.password : random_password.redis_password[0].result,
4249
slave_volume_size = var.redis_config.slave_volume_size,
4350
slave_replicacount = var.redis_config.slave_replica_count,
4451
storage_class_name = var.redis_config.storage_class_name,

output.tf

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
output "redis_port" {
2-
value = "6379"
3-
description = "The port number on which Redis is running."
1+
output "redis_endpoints" {
2+
description = "Redis endpoints in the Kubernetes cluster."
3+
value = {
4+
redis_port = "6379",
5+
redis_master_endpoint = "redis-master.${var.namespace}.svc.cluster.local",
6+
redis_slave_endpoint = "redis-replicas.${var.namespace}.svc.cluster.local"
7+
}
48
}
59

6-
output "redis_master_endpoint" {
7-
value = "redis-master.${var.namespace}.svc.cluster.local"
8-
description = "The endpoint for the Redis Master Service, which is the primary node in the Redis cluster responsible for handling read-write operations."
9-
}
10-
11-
output "redis_slave_endpoint" {
12-
value = "redis-replicas.${var.namespace}.svc.cluster.local"
13-
description = "The endpoint for the Redis Slave Service, which is a secondary node in the Redis cluster responsible for handling read-only operations."
10+
output "redis_credential" {
11+
description = "Redis credentials used for accessing the database."
12+
value = var.redis_config.store_password_to_secret_manager ? null : {
13+
redis_username = "root",
14+
redis_password = var.custom_credentials_enabled ? var.custom_credentials_config.password : nonsensitive(random_password.redis_password[0].result)
15+
}
1416
}

variables.tf

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
variable "redis_config" {
22
type = any
33
default = {
4-
name = ""
5-
environment = ""
6-
master_volume_size = ""
7-
architecture = "replication"
8-
slave_replica_count = 1
9-
slave_volume_size = ""
10-
storage_class_name = ""
11-
values_yaml = ""
4+
name = ""
5+
environment = ""
6+
master_volume_size = ""
7+
architecture = "replication"
8+
slave_replica_count = 1
9+
slave_volume_size = ""
10+
storage_class_name = ""
11+
store_password_to_secret_manager = ""
12+
values_yaml = ""
1213
}
13-
description = "Specify the configuration settings for Redis, including the name, environment, storage options, replication settings, and custom YAML values."
14+
description = "Specify the configuration settings for Redis, including the name, environment, storage options, replication settings, store password to secret manager and custom YAML values."
1415
}
1516

1617
variable "chart_version" {
@@ -48,3 +49,17 @@ variable "create_namespace" {
4849
description = "Specify whether or not to create the namespace if it does not already exist. Set it to true to create the namespace."
4950
default = true
5051
}
52+
53+
variable "custom_credentials_enabled" {
54+
type = bool
55+
default = false
56+
description = "Specifies whether to enable custom credentials for Redis."
57+
}
58+
59+
variable "custom_credentials_config" {
60+
type = any
61+
default = {
62+
password = ""
63+
}
64+
description = "Specify the configuration settings for Redis to pass custom credentials during creation."
65+
}

0 commit comments

Comments
 (0)