Skip to content

Commit ae39aca

Browse files
committed
Updated mysql to pass custom username and password
1 parent 5d45f93 commit ae39aca

File tree

7 files changed

+77
-36
lines changed

7 files changed

+77
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The required IAM permissions to create resources from this module can be found [
6161
5. To deploy Prometheus/Grafana, please follow the installation instructions for each tool in their respective documentation.
6262
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.
6363
7. Finally, you can use Grafana to create custom dashboards and visualize the metrics data collected by Prometheus.
64-
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.
64+
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.
6565
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6666
## Requirements
6767

examples/complete/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ module "mysql" {
2525
secondary_db_replica_count = 2
2626
store_password_to_secret_manager = local.store_password_to_secret_manager
2727
}
28+
mysqldb_custom_credentials_enabled = false
29+
mysqldb_custom_credentials_config = {
30+
root_user = ""
31+
root_password = ""
32+
custom_username = ""
33+
custom_user_password = ""
34+
replication_user = ""
35+
replication_password = ""
36+
exporter_user = ""
37+
exporter_password = ""
38+
}
2839
mysqldb_backup_enabled = true
2940
mysqldb_backup_config = {
3041
s3_bucket_uri = "s3://bucket_name"

examples/complete/output.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
output "mysql_endpoints" {
22
value = module.mysql.mysqldb_endpoints
3-
description = "Mysql_Info"
3+
description = "MySQL endpoints in the Kubernetes cluster."
44
}
55

66
output "mysql_credential" {
77
value = local.store_password_to_secret_manager ? null : module.mysql.mysqldb_credential
8-
description = "Mysql_Info"
8+
description = "MySQL credentials used for accessing the MySQL database."
99
}

examples/complete/provider.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ provider "aws" {
55
}
66
}
77

8-
98
data "aws_eks_cluster" "cluster" {
109
name = ""
1110
}
@@ -14,19 +13,16 @@ data "aws_eks_cluster_auth" "cluster" {
1413
name = ""
1514
}
1615

17-
1816
provider "kubernetes" {
1917
host = data.aws_eks_cluster.cluster.endpoint
2018
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
2119
token = data.aws_eks_cluster_auth.cluster.token
22-
2320
}
2421

2522
provider "helm" {
2623
kubernetes {
2724
host = data.aws_eks_cluster.cluster.endpoint
2825
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
2926
token = data.aws_eks_cluster_auth.cluster.token
30-
3127
}
3228
}

main.tf

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@ data "aws_eks_cluster" "kubernetes_cluster" {
1313
}
1414

1515
resource "random_password" "mysqldb_root_password" {
16+
count = var.mysqldb_custom_credentials_enabled ? 0 : 1
1617
length = 20
1718
special = false
1819
}
1920

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

2527
resource "random_password" "mysqldb_replication_user_password" {
28+
count = var.mysqldb_custom_credentials_enabled ? 0 : 1
2629
length = 20
2730
special = false
2831
}
2932

3033
resource "random_password" "mysqldb_exporter_user_password" {
34+
count = var.mysqldb_custom_credentials_enabled ? 0 : 1
3135
length = 20
3236
special = false
3337
}
@@ -39,20 +43,29 @@ resource "aws_secretsmanager_secret" "mysql_user_password" {
3943
}
4044

4145
resource "aws_secretsmanager_secret_version" "mysql_user_password" {
42-
count = var.mysqldb_config.store_password_to_secret_manager ? 1 : 0
43-
secret_id = aws_secretsmanager_secret.mysql_user_password[0].id
44-
secret_string = <<EOF
45-
{
46-
"root_user": "root",
47-
"root_password": "${random_password.mysqldb_root_password.result}",
48-
"custom_username": "${var.mysqldb_config.custom_user_username}",
49-
"custom_user_password": "${random_password.mysqldb_custom_user_password.result}",
50-
"replication_user": "replicator",
51-
"replication_password": "${random_password.mysqldb_replication_user_password.result}",
52-
"exporter_user": "mysqld_exporter",
53-
"exporter_password": "${random_password.mysqldb_exporter_user_password.result}"
54-
}
55-
EOF
46+
count = var.mysqldb_config.store_password_to_secret_manager ? 1 : 0
47+
secret_id = aws_secretsmanager_secret.mysql_user_password[0].id
48+
secret_string = var.mysqldb_custom_credentials_enabled ? jsonencode(
49+
{
50+
"root_user" : "${var.mysqldb_custom_credentials_config.root_user}",
51+
"root_password" : "${var.mysqldb_custom_credentials_config.root_password}",
52+
"custom_username" : "${var.mysqldb_custom_credentials_config.custom_username}",
53+
"custom_user_password" : "${var.mysqldb_custom_credentials_config.custom_user_password}",
54+
"replication_user" : "${var.mysqldb_custom_credentials_config.replication_user}",
55+
"replication_password" : "${var.mysqldb_custom_credentials_config.replication_password}",
56+
"exporter_user" : "${var.mysqldb_custom_credentials_config.exporter_user}",
57+
"exporter_password" : "${var.mysqldb_custom_credentials_config.exporter_password}"
58+
}) : jsonencode(
59+
{
60+
"root_user" : "root",
61+
"root_password" : "${random_password.mysqldb_root_password[0].result}",
62+
"custom_username" : "${var.mysqldb_config.custom_user_username}",
63+
"custom_user_password" : "${random_password.mysqldb_custom_user_password[0].result}",
64+
"replication_user" : "replicator",
65+
"replication_password" : "${random_password.mysqldb_replication_user_password[0].result}",
66+
"exporter_user" : "mysqld_exporter",
67+
"exporter_password" : "${random_password.mysqldb_exporter_user_password[0].result}"
68+
})
5669
}
5770

5871
resource "kubernetes_namespace" "mysqldb" {
@@ -78,13 +91,13 @@ resource "helm_release" "mysqldb" {
7891
primary_pod_size = var.mysqldb_config.primary_db_volume_size,
7992
secondary_pod_size = var.mysqldb_config.secondary_db_volume_size,
8093
storage_class_name = var.mysqldb_config.storage_class_name,
81-
custom_user_username = var.mysqldb_config.custom_user_username,
82-
custom_user_password = random_password.mysqldb_custom_user_password.result,
83-
replication_password = random_password.mysqldb_replication_user_password.result,
84-
mysqldb_root_password = random_password.mysqldb_root_password.result,
94+
custom_user_username = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.custom_username : var.mysqldb_config.custom_user_username,
95+
custom_user_password = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.custom_user_password : random_password.mysqldb_custom_user_password[0].result,
96+
replication_password = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.replication_password : random_password.mysqldb_replication_user_password[0].result,
97+
mysqldb_root_password = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.root_password : random_password.mysqldb_root_password[0].result,
8598
mysqldb_exporter_enabled = var.mysqldb_exporter_enabled,
8699
service_monitor_namespace = var.namespace
87-
metrics_exporter_password = random_password.mysqldb_exporter_user_password.result,
100+
metrics_exporter_password = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.exporter_password : random_password.mysqldb_exporter_user_password[0].result,
88101
secondary_pod_replica_count = var.mysqldb_config.secondary_db_replica_count
89102
}),
90103
var.mysqldb_config.values_yaml

output.tf

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
output "mysqldb_endpoints" {
2-
description = "Mysql_Info"
2+
description = "MySQL endpoints in the Kubernetes cluster."
33
value = {
44
mysqlport = "3306",
55
mysql_primary_endpoint = "mysqldb-primary.${var.namespace}.svc.cluster.local",
@@ -10,15 +10,15 @@ output "mysqldb_endpoints" {
1010
}
1111

1212
output "mysqldb_credential" {
13-
description = "Mysql_Info"
13+
description = "MySQL credentials used for accessing the MySQL database."
1414
value = var.mysqldb_config.store_password_to_secret_manager ? null : {
15-
root_user = "root",
16-
root_password = nonsensitive(random_password.mysqldb_root_password.result),
17-
custom_username = var.mysqldb_config.custom_user_username,
18-
custom_user_password = nonsensitive(random_password.mysqldb_custom_user_password.result),
19-
replication_user = "replicator",
20-
replication_password = nonsensitive(random_password.mysqldb_replication_user_password.result),
21-
exporter_user = "mysqld_exporter",
22-
exporter_password = nonsensitive(random_password.mysqldb_exporter_user_password.result)
15+
root_user = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.root_user : "root",
16+
root_password = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.root_password : nonsensitive(random_password.mysqldb_root_password[0].result),
17+
custom_username = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.custom_username : var.mysqldb_config.custom_user_username,
18+
custom_user_password = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.custom_user_password : nonsensitive(random_password.mysqldb_custom_user_password[0].result),
19+
replication_user = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.replication_user : "replicator",
20+
replication_password = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.replication_password : nonsensitive(random_password.mysqldb_replication_user_password[0].result),
21+
exporter_user = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.exporter_user : "mysqld_exporter",
22+
exporter_password = var.mysqldb_custom_credentials_enabled ? var.mysqldb_custom_credentials_config.exporter_password : nonsensitive(random_password.mysqldb_exporter_user_password[0].result)
2323
}
2424
}

variables.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@ variable "mysqldb_config" {
1515
description = "Specify the configuration settings for MySQL, including the name, environment, storage options, replication settings, and custom YAML values."
1616
}
1717

18+
variable "mysqldb_custom_credentials_enabled" {
19+
type = bool
20+
default = false
21+
description = "Specifies whether to enable custom for MySQL database."
22+
}
23+
24+
variable "mysqldb_custom_credentials_config" {
25+
type = any
26+
default = {
27+
root_user = ""
28+
root_password = ""
29+
custom_username = ""
30+
custom_user_password = ""
31+
replication_user = ""
32+
replication_password = ""
33+
exporter_user = ""
34+
exporter_password = ""
35+
}
36+
description = "Specify the configuration settings for MySQL to pass custom credentials during creation"
37+
}
38+
1839
variable "app_version" {
1940
type = string
2041
default = "8.0.29-debian-11-r9"

0 commit comments

Comments
 (0)