-
Notifications
You must be signed in to change notification settings - Fork 22
/
database.tf
39 lines (32 loc) · 1.12 KB
/
database.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
resource "random_id" "db_name_suffix" {
count = var.db_config.is_enabled ? 1 : 0
byte_length = 4
}
resource "google_sql_database" "application" {
count = var.db_config.is_enabled ? 1 : 0
name = var.db_config.db_name
instance = google_sql_database_instance.application[count.index].name
}
resource "google_sql_database_instance" "application" {
count = var.db_config.is_enabled ? 1 : 0
name = "${var.service_name}-${random_id.db_name_suffix[count.index].hex}"
region = var.region
database_version = var.db_config.db_version
settings {
tier = var.db_config.db_tier
ip_configuration {
ipv4_enabled = true
}
}
deletion_protection = "false"
}
resource "google_sql_user" "application" {
count = var.db_config.is_enabled ? 1 : 0
name = var.db_config.db_user
instance = google_sql_database_instance.application[count.index].name
host = "%"
password = data.google_secret_manager_secret_version.db_password.secret_data
}
output "database_connection_name" {
value = join("", google_sql_database_instance.application[*].connection_name)
}