-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.tf
128 lines (120 loc) · 5.99 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
terraform {
required_version = ">= 0.13.1" # see https://releases.hashicorp.com/terraform/
}
locals {
master_instance_name = (
var.full_name_master_instance == "" ? format("mysql-%s-%s", var.name_master_instance, var.name_suffix) : var.full_name_master_instance
)
read_replica_name_suffix = format("-%s-", var.name_read_replica)
master_authorized_networks = [
for authorized_network in var.authorized_networks_master_instance : {
name = authorized_network.display_name
value = authorized_network.cidr_block
}
]
default_region = data.google_client_config.google_client.region
region_master_instance = coalesce(var.region_master_instance, local.default_region)
region_read_replica = coalesce(var.region_read_replica, local.region_master_instance)
zone_master_instance = format("%s-%s", local.region_master_instance, var.zone_master_instance)
zone_read_replica = format("%s-%s", local.region_read_replica, var.zone_read_replica)
read_replica_authorized_networks = [
for authorized_network in var.authorized_networks_read_replica : {
name = authorized_network.display_name
value = authorized_network.cidr_block
}
]
db_flags_master_instance = [for key, val in var.db_flags_master_instance : { name = key, value = val }]
db_flags_read_replica = [for key, val in var.db_flags_read_replica : { name = key, value = val }]
backup_location = var.backup_enabled ? coalesce(var.backup_location, local.region_master_instance) : null
}
data "google_client_config" "google_client" {}
resource "google_project_service" "compute_api" {
service = "compute.googleapis.com"
disable_on_destroy = false
}
resource "google_project_service" "cloudsql_api" {
service = "sqladmin.googleapis.com"
disable_on_destroy = false
}
module "google_mysql_db" {
source = "GoogleCloudPlatform/sql-db/google//modules/mysql"
version = "13.0.1"
depends_on = [google_project_service.compute_api, google_project_service.cloudsql_api]
deletion_protection = var.deletion_protection_master_instance
project_id = data.google_client_config.google_client.project
name = local.master_instance_name
db_name = var.default_db_name
db_collation = var.default_db_collation
db_charset = var.default_db_charset
database_version = var.db_version
region = local.region_master_instance
zone = local.zone_master_instance
availability_type = var.highly_available ? "REGIONAL" : null
tier = var.instance_size_master_instance
disk_size = var.disk_size_gb_master_instance
disk_autoresize = var.disk_auto_resize_master_instance
disk_type = "PD_SSD"
create_timeout = var.db_timeout
update_timeout = var.db_timeout
delete_timeout = var.db_timeout
user_name = var.root_user_name
user_password = var.root_user_password
user_host = var.root_user_host
database_flags = local.db_flags_master_instance
user_labels = var.labels_master_instance
additional_users = var.additional_users
additional_databases = var.additional_databases
maintenance_window_day = var.maintenance_window.day_utc
maintenance_window_hour = var.maintenance_window.hour_utc
maintenance_window_update_track = var.maintenance_window.update_track
insights_config = var.insights_config
ip_configuration = {
authorized_networks = local.master_authorized_networks
ipv4_enabled = var.public_access_master_instance
private_network = var.private_network
require_ssl = null
allocated_ip_range = var.allocated_ip_range
}
# backup settings
backup_configuration = {
enabled = var.backup_enabled
binary_log_enabled = var.pit_recovery_enabled
start_time = "00:05"
location = local.backup_location
transaction_log_retention_days = null
retained_backups = null
retention_unit = null
}
# read replica settings
read_replica_deletion_protection = var.deletion_protection_read_replica
read_replica_name_suffix = local.read_replica_name_suffix
read_replicas = [
for array_index in range(var.read_replica_count) : {
name = array_index
tier = var.instance_size_read_replica
zone = local.zone_read_replica
ip_configuration = {
authorized_networks = local.read_replica_authorized_networks
ipv4_enabled = var.public_access_read_replica
private_network = var.private_network
require_ssl = null
allocated_ip_range = var.allocated_ip_range_read_replica
}
database_flags = local.db_flags_read_replica
disk_autoresize = var.disk_auto_resize_read_replica
disk_autoresize_limit = var.disk_autoresize_limit_read_replica
disk_size = var.disk_size_gb_read_replica
disk_type = "PD_SSD"
availability_type = var.highly_available_read_replica ? "REGIONAL" : "ZONAL"
user_labels = var.labels_read_replica
encryption_key_name = var.encryption_key_name_read_replica
}
]
}
resource "google_project_iam_member" "cloudsql_proxy_user" {
for_each = toset(var.sql_proxy_user_groups)
project = data.google_client_config.google_client.project
role = "roles/cloudsql.client" # see https://cloud.google.com/sql/docs/mysql/quickstart-proxy-test#before-you-begin
member = "group:${each.value}"
depends_on = [google_project_service.compute_api, google_project_service.cloudsql_api]
}