Skip to content

Commit 581d5d7

Browse files
Merge pull request #10 from ankur6405/main
Added option to upload secret to secret manager
2 parents 4a09ade + 48355a2 commit 581d5d7

File tree

8 files changed

+108
-47
lines changed

8 files changed

+108
-47
lines changed

README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This module is for deploying a highly available MongoDB cluster on Kubernetes us
1212

1313
| MongoDB Helm Chart Version | K8s supported version |
1414
| :-----: | :--- |
15-
| **13.1.5** | **1.23,1.24,1.25** |
15+
| **13.1.5** | **1.23,1.24,1.25,1.26,1.27** |
1616

1717

1818
## Usage Example
@@ -22,13 +22,21 @@ module "mongodb" {
2222
source = "squareops/mongodb/kubernetes"
2323
cluster_name = "prod-cluster"
2424
mongodb_config = {
25-
name = "mongo"
26-
values_yaml = ""
27-
environment = "prod"
28-
volume_size = "10Gi"
29-
architecture = "replicaset"
30-
replica_count = 2
31-
storage_class_name = "gp3"
25+
name = "mongo"
26+
values_yaml = ""
27+
environment = "prod"
28+
volume_size = "10Gi"
29+
architecture = "replicaset"
30+
replica_count = 2
31+
storage_class_name = "gp3"
32+
store_password_to_secret_manager = true
33+
}
34+
mongodb_custom_credentials_enabled = true
35+
mongodb_custom_credentials_config = {
36+
root_user = "root"
37+
root_password = "NCPFUKEMd7rrWuvMAa73"
38+
metric_exporter_user = "mongodb_exporter"
39+
metric_exporter_password = "nvAHhm1uGQNYWVw6ZyAH"
3240
}
3341
mongodb_backup_enabled = true
3442
mongodb_backup_config = {
@@ -64,7 +72,7 @@ The required IAM permissions to create resources from this module can be found [
6472
5. To deploy Prometheus/Grafana, please follow the installation instructions for each tool in their respective documentation.
6573
6. Once Prometheus and Grafana are deployed, the exporter can be configured to scrape metrics data from your application or system and send it to Prometheus.
6674
7. Finally, you can use Grafana to create custom dashboards and visualize the metrics data collected by Prometheus.
67-
8. This module is compatible with EKS version 1.23, which is great news for users deploying the module on an EKS cluster running that version. Review the module's documentation, meet specific configuration requirements, and test thoroughly after deployment to ensure everything works as expected.
75+
8. This module is compatible with EKS version 1.23,1.24,1.25,1.26 and 1.27 which is great news for users deploying the module on an EKS cluster running that version. Review the module's documentation, meet specific configuration requirements, and test thoroughly after deployment to ensure everything works as expected.
6876
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6977
## Requirements
7078

@@ -111,7 +119,9 @@ No modules.
111119
| <a name="input_create_namespace"></a> [create\_namespace](#input\_create\_namespace) | Specify whether or not to create the namespace if it does not already exist. Set it to true to create the namespace. | `string` | `true` | no |
112120
| <a name="input_mongodb_backup_config"></a> [mongodb\_backup\_config](#input\_mongodb\_backup\_config) | Configuration options for Mongodb database backups. It includes properties such as the S3 bucket URI, the S3 bucket region, and the cron expression for full backups. | `any` | <pre>{<br> "cron_for_full_backup": "*/5 * * * *",<br> "s3_bucket_region": "us-east-2",<br> "s3_bucket_uri": ""<br>}</pre> | no |
113121
| <a name="input_mongodb_backup_enabled"></a> [mongodb\_backup\_enabled](#input\_mongodb\_backup\_enabled) | Specifies whether to enable backups for Mongodb database. | `bool` | `false` | no |
114-
| <a name="input_mongodb_config"></a> [mongodb\_config](#input\_mongodb\_config) | Specify the configuration settings for Mongodb, including the name, environment, storage options, replication settings, and custom YAML values. | `any` | <pre>{<br> "architecture": "",<br> "environment": "",<br> "name": "",<br> "replica_count": 2,<br> "storage_class_name": "",<br> "values_yaml": "",<br> "volume_size": ""<br>}</pre> | no |
122+
| <a name="input_mongodb_config"></a> [mongodb\_config](#input\_mongodb\_config) | Specify the configuration settings for Mongodb, including the name, environment, storage options, replication settings, and custom YAML values. | `any` | <pre>{<br> "architecture": "",<br> "environment": "",<br> "name": "",<br> "replica_count": 2,<br> "storage_class_name": "",<br> "store_password_to_secret_manager": true,<br> "values_yaml": "",<br> "volume_size": ""<br>}</pre> | no |
123+
| <a name="input_mongodb_custom_credentials_config"></a> [mongodb\_custom\_credentials\_config](#input\_mongodb\_custom\_credentials\_config) | Specify the configuration settings for Mongodb to pass custom credentials during creation. | `any` | <pre>{<br> "metric_exporter_password": "",<br> "metric_exporter_user": "",<br> "root_password": "",<br> "root_user": ""<br>}</pre> | no |
124+
| <a name="input_mongodb_custom_credentials_enabled"></a> [mongodb\_custom\_credentials\_enabled](#input\_mongodb\_custom\_credentials\_enabled) | Specifies whether to enable custom credentials for MongoDB database. | `bool` | `false` | no |
115125
| <a name="input_mongodb_exporter_config"></a> [mongodb\_exporter\_config](#input\_mongodb\_exporter\_config) | Specify whether or not to deploy Mongodb exporter to collect Mongodb metrics for monitoring in Grafana. | `any` | <pre>{<br> "version": "2.9.0"<br>}</pre> | no |
116126
| <a name="input_mongodb_exporter_enabled"></a> [mongodb\_exporter\_enabled](#input\_mongodb\_exporter\_enabled) | Specify whether or not to deploy Mongodb exporter to collect Mongodb metrics for monitoring in Grafana. | `bool` | `false` | no |
117127
| <a name="input_mongodb_restore_config"></a> [mongodb\_restore\_config](#input\_mongodb\_restore\_config) | Configuration options for restoring dump to the Mongodb database. | `any` | <pre>{<br> "file_name_full": "",<br> "file_name_incremental": "",<br> "full_restore_enable": false,<br> "incremental_restore_enable": false,<br> "s3_bucket_region": "us-east-2",<br> "s3_bucket_uri": "s3://mymongo/mongodumpfull_20230424_112501.gz"<br>}</pre> | no |
@@ -123,7 +133,8 @@ No modules.
123133

124134
| Name | Description |
125135
|------|-------------|
126-
| <a name="output_mongodb"></a> [mongodb](#output\_mongodb) | MongoDB\_Info |
136+
| <a name="output_mongodb_credential"></a> [mongodb\_credential](#output\_mongodb\_credential) | MongoDB credentials used for accessing the MongoDB database. |
137+
| <a name="output_mongodb_endpoints"></a> [mongodb\_endpoints](#output\_mongodb\_endpoints) | MongoDB endpoints in the Kubernetes cluster. |
127138
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
128139

129140
## Contribution & Issue Reporting

example/complete/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ No inputs.
3838

3939
| Name | Description |
4040
|------|-------------|
41-
| <a name="output_mongodb_configuration"></a> [mongodb\_configuration](#output\_mongodb\_configuration) | Mongodb\_Info |
41+
| <a name="output_mongodb_credential"></a> [mongodb\_credential](#output\_mongodb\_credential) | MongoDB credentials used for accessing the MongoDB database. |
42+
| <a name="output_mongodb_endpoints"></a> [mongodb\_endpoints](#output\_mongodb\_endpoints) | MongoDB endpoints in the Kubernetes cluster. |
4243
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

example/complete/main.tf

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

1213
module "mongodb" {
1314
source = "squareops/mongodb/kubernetes"
1415
cluster_name = "dev-cluster"
1516
mongodb_config = {
16-
name = local.name
17-
values_yaml = file("./helm/values.yaml")
18-
environment = local.environment
19-
volume_size = "10Gi"
20-
architecture = "replicaset"
21-
replica_count = 2
22-
storage_class_name = "gp3"
17+
name = local.name
18+
values_yaml = file("./helm/values.yaml")
19+
environment = local.environment
20+
volume_size = "10Gi"
21+
architecture = "replicaset"
22+
replica_count = 2
23+
storage_class_name = "gp3"
24+
store_password_to_secret_manager = local.store_password_to_secret_manager
25+
}
26+
mongodb_custom_credentials_enabled = true
27+
mongodb_custom_credentials_config = {
28+
root_user = "root"
29+
root_password = "NCPFUKEMd7rrWuvMAa73"
30+
metric_exporter_user = "mongodb_exporter"
31+
metric_exporter_password = "nvAHhm1uGQNYWVw6ZyAH"
2332
}
2433
mongodb_backup_enabled = true
2534
mongodb_backup_config = {

example/complete/output.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
output "mongodb_configuration" {
2-
value = module.mongodb.mongodb
3-
description = "Mongodb_Info"
1+
output "mongodb_endpoints" {
2+
value = module.mongodb.mongodb_endpoints
3+
description = "MongoDB endpoints in the Kubernetes cluster."
4+
}
5+
6+
output "mongodb_credential" {
7+
value = local.store_password_to_secret_manager ? null : module.mongodb.mongodb_credential
8+
description = "MongoDB credentials used for accessing the MongoDB database."
49
}

example/complete/provider.tf

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

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

main.tf

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,39 @@ data "aws_eks_cluster" "kubernetes_cluster" {
1313
name = var.cluster_name
1414
}
1515
resource "random_password" "mongodb_root_password" {
16+
count = var.mongodb_custom_credentials_enabled ? 0 : 1
1617
length = 20
1718
special = false
1819
}
1920

2021
resource "random_password" "mongodb_exporter_password" {
22+
count = var.mongodb_custom_credentials_enabled ? 0 : 1
2123
length = 20
2224
special = false
2325
}
2426

2527
resource "aws_secretsmanager_secret" "mongodb_user_password" {
28+
count = var.mongodb_config.store_password_to_secret_manager ? 1 : 0
2629
name = format("%s/%s/%s", var.mongodb_config.environment, var.mongodb_config.name, "mongodb")
2730
recovery_window_in_days = var.recovery_window_aws_secret
2831
}
2932

3033
resource "aws_secretsmanager_secret_version" "mongodb_root_password" {
31-
secret_id = aws_secretsmanager_secret.mongodb_user_password.id
32-
secret_string = <<EOF
33-
{
34-
"root_user": "root",
35-
"root_password": "${random_password.mongodb_root_password.result}",
36-
"metric_exporter_user": "mongodb_exporter",
37-
"metric_exporter_password": "${random_password.mongodb_exporter_password.result}"
38-
}
39-
EOF
34+
count = var.mongodb_config.store_password_to_secret_manager ? 1 : 0
35+
secret_id = aws_secretsmanager_secret.mongodb_user_password[0].id
36+
secret_string = var.mongodb_custom_credentials_enabled ? jsonencode(
37+
{
38+
"root_user" : "${var.mongodb_custom_credentials_config.root_user}",
39+
"root_password" : "${var.mongodb_custom_credentials_config.root_password}",
40+
"metric_exporter_user" : "${var.mongodb_custom_credentials_config.metric_exporter_user}",
41+
"metric_exporter_password" : "${var.mongodb_custom_credentials_config.metric_exporter_password}"
42+
}) : jsonencode(
43+
{
44+
"root_user" : "root",
45+
"root_password" : "${random_password.mongodb_root_password[0].result}",
46+
"metric_exporter_user" : "mongodb_exporter",
47+
"metric_exporter_password" : "${random_password.mongodb_exporter_password[0].result}"
48+
})
4049
}
4150

4251
resource "kubernetes_namespace" "mongodb" {
@@ -64,8 +73,8 @@ resource "helm_release" "mongodb" {
6473
replicacount = var.mongodb_config.replica_count,
6574
arbiterValue = local.arbiterValue,
6675
storage_class_name = var.mongodb_config.storage_class_name,
67-
mongodb_exporter_password = random_password.mongodb_exporter_password.result,
68-
mongodb_root_user_password = random_password.mongodb_root_password.result
76+
mongodb_exporter_password = var.mongodb_custom_credentials_enabled ? var.mongodb_custom_credentials_config.metric_exporter_password : random_password.mongodb_exporter_password[0].result,
77+
mongodb_root_user_password = var.mongodb_custom_credentials_enabled ? var.mongodb_custom_credentials_config.root_password : random_password.mongodb_root_password[0].result
6978
}),
7079
var.mongodb_config.values_yaml
7180
]
@@ -84,7 +93,7 @@ resource "helm_release" "mongodb_backup" {
8493
s3_bucket_uri = var.mongodb_backup_config.s3_bucket_uri,
8594
s3_bucket_region = var.mongodb_backup_config.s3_bucket_region,
8695
cron_for_full_backup = var.mongodb_backup_config.cron_for_full_backup,
87-
mongodb_root_user_password = random_password.mongodb_root_password.result
96+
mongodb_root_user_password = var.mongodb_custom_credentials_enabled ? var.mongodb_custom_credentials_config.root_password : random_password.mongodb_root_password[0].result
8897
})
8998
]
9099
}
@@ -100,7 +109,7 @@ resource "helm_release" "mongodb_exporter" {
100109
repository = "https://prometheus-community.github.io/helm-charts"
101110
values = [
102111
templatefile("${path.module}/helm/values/exporter/values.yaml", {
103-
mongodb_exporter_password = "${random_password.mongodb_exporter_password.result}"
112+
mongodb_exporter_password = var.mongodb_custom_credentials_enabled ? var.mongodb_custom_credentials_config.metric_exporter_password : "${random_password.mongodb_exporter_password[0].result}"
104113
service_monitor_namespace = var.namespace
105114
}),
106115
var.mongodb_config.values_yaml
@@ -166,7 +175,7 @@ resource "helm_release" "mongodb_restore" {
166175
full_restore_enable = var.mongodb_restore_config.full_restore_enable,
167176
file_name_incremental = var.mongodb_restore_config.file_name_incremental,
168177
incremental_restore_enable = var.mongodb_restore_config.incremental_restore_enable,
169-
mongodb_root_user_password = random_password.mongodb_root_password.result
178+
mongodb_root_user_password = var.mongodb_custom_credentials_enabled ? var.mongodb_custom_credentials_config.root_password : random_password.mongodb_root_password[0].result
170179
})
171180
]
172181
}

output.tf

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
output "mongodb" {
2-
description = "MongoDB_Info"
1+
output "mongodb_endpoints" {
2+
description = "MongoDB endpoints in the Kubernetes cluster."
33
value = {
44
mongoport = "27017",
55
mongodb_endpoint = "mongodb-headless.${var.namespace}.svc.cluster.local"
66
}
77
}
8+
9+
output "mongodb_credential" {
10+
description = "MongoDB credentials used for accessing the MongoDB database."
11+
value = var.mongodb_config.store_password_to_secret_manager ? null : {
12+
root_user = var.mongodb_custom_credentials_enabled ? var.mongodb_custom_credentials_config.root_user : "root",
13+
root_password = var.mongodb_custom_credentials_enabled ? var.mongodb_custom_credentials_config.root_password : nonsensitive(random_password.mongodb_root_password[0].result),
14+
metric_exporter_user = var.mongodb_custom_credentials_enabled ? var.mongodb_custom_credentials_config.metric_exporter_user : "mongodb_exporter",
15+
metric_exporter_password = var.mongodb_custom_credentials_enabled ? var.mongodb_custom_credentials_config.metric_exporter_password : nonsensitive(random_password.mongodb_exporter_password[0].result)
16+
}
17+
}

variables.tf

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
variable "mongodb_config" {
22
type = any
33
default = {
4-
name = ""
5-
environment = ""
6-
volume_size = ""
7-
architecture = ""
8-
replica_count = 2
9-
values_yaml = ""
10-
storage_class_name = ""
4+
name = ""
5+
environment = ""
6+
volume_size = ""
7+
architecture = ""
8+
replica_count = 2
9+
values_yaml = ""
10+
storage_class_name = ""
11+
store_password_to_secret_manager = true
1112
}
1213
description = "Specify the configuration settings for Mongodb, including the name, environment, storage options, replication settings, and custom YAML values."
1314
}
1415

16+
variable "mongodb_custom_credentials_enabled" {
17+
type = bool
18+
default = false
19+
description = "Specifies whether to enable custom credentials for MongoDB database."
20+
}
21+
22+
variable "mongodb_custom_credentials_config" {
23+
type = any
24+
default = {
25+
root_user = ""
26+
root_password = ""
27+
metric_exporter_user = ""
28+
metric_exporter_password = ""
29+
}
30+
description = "Specify the configuration settings for Mongodb to pass custom credentials during creation."
31+
}
32+
1533
variable "chart_version" {
1634
type = string
1735
default = "13.1.5"

0 commit comments

Comments
 (0)