-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.tf
93 lines (83 loc) · 2.15 KB
/
db.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
resource "google_sql_database_instance" "mysql" {
name = "${local.project_name}-${random_id.mysql.hex}"
region = data.google_client_config.this.region
database_version = "MYSQL_5_7"
settings {
tier = "db-g1-small"
ip_configuration {
ipv4_enabled = true
private_network = module.vpc.network_self_link
dynamic "authorized_networks" {
for_each = concat(var.authorized_ips, var.mysql_authorized_ips)
content {
value = authorized_networks.value.cidr_block
name = authorized_networks.value.display_name
}
}
}
maintenance_window {
day = "6"
hour = "2"
update_track = "stable"
}
backup_configuration {
enabled = true
start_time = "02:00"
}
}
lifecycle {
prevent_destroy = true
}
}
resource "kubernetes_service" "mysql" {
metadata {
name = "mysql"
}
spec {
type = "ExternalName"
external_name = google_sql_database_instance.mysql.private_ip_address
}
}
resource "random_id" "mysql" {
byte_length = 4
}
resource "random_password" "mysql_root_password" {
length = 12
special = false
}
resource "google_sql_user" "mysql_root" {
name = "root"
instance = google_sql_database_instance.mysql.name
password = random_password.mysql_root_password.result
host = "%"
}
resource "google_compute_global_address" "mysql"{
name = "${local.project_name}-mysql-db"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 24
network = module.vpc.network_name
}
resource "google_service_networking_connection" "mysql" {
network = module.vpc.network_id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [
google_compute_global_address.mysql.name
]
}
resource "google_compute_network_peering_routes_config" "mysql" {
for_each = toset([
"servicenetworking",
])
peering = "${each.value}-googleapis-com"
network = module.vpc.network_name
import_custom_routes = true
export_custom_routes = true
depends_on = [
google_sql_database_instance.mysql
]
}
output "mysql" {
value = random_password.mysql_root_password.result
sensitive = true
}