Skip to content

Commit 1906716

Browse files
EvyBongersEvy Bongers
authored and
Evy Bongers
committed
Initial commit
0 parents  commit 1906716

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed

database.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "google_sql_database" "database" {
2+
for_each = toset(var.databases)
3+
4+
instance = google_sql_database_instance.instance.name
5+
name = each.value
6+
charset = "utf8"
7+
collation = "utf8_general_ci"
8+
}

instance.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
resource "google_sql_database_instance" "instance" {
2+
database_version = var.database_version
3+
name = var.instance_name
4+
master_instance_name = var.primary_instance_name
5+
settings {
6+
availability_type = var.highly_available == true ? "REGIONAL" : "ZONAL"
7+
disk_autoresize = var.storage_autoresize
8+
disk_autoresize_limit = var.storage_limit
9+
disk_size = local.storage_size
10+
disk_type = "PD_SSD"
11+
tier = local.tier
12+
user_labels = local.labels
13+
backup_configuration {
14+
binary_log_enabled = local.backup_config.binary_log_enabled
15+
enabled = local.backup_config.enabled
16+
location = local.backup_config.location
17+
}
18+
dynamic "database_flags" {
19+
for_each = var.flags
20+
iterator = flag
21+
content {
22+
name = flag.key
23+
value = flag.value
24+
}
25+
}
26+
ip_configuration {
27+
ipv4_enabled = true
28+
require_ssl = true
29+
}
30+
maintenance_window {
31+
day = 1
32+
hour = 4
33+
}
34+
}
35+
}

locals.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
locals {
2+
default_backup_config = {
3+
binary_log_enabled = var.highly_available == true ? true : false
4+
enabled = var.highly_available == true ? true : false
5+
location = "eu"
6+
}
7+
default_labels = {
8+
env = var.environment
9+
}
10+
default_tier = var.environment == "production" ? "db-custom-2-8192" : "db-f1-micro"
11+
12+
backup_config = defaults(var.backup_config, local.default_backup_config)
13+
labels = merge(local.default_labels, var.labels)
14+
storage_size = var.storage_autoresize == true ? null : var.storage_size
15+
tier = var.tier != null ? var.tier : local.default_tier
16+
users = { for user in var.users : "${user.name}@${user.host}" => user }
17+
}

main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
terraform {
2+
required_version = ">= 0.15.0"
3+
4+
required_providers {
5+
google = {
6+
source = "hashicorp/google"
7+
version = ">= 3.70.0"
8+
}
9+
}
10+
experiments = [module_variable_optional_attrs]
11+
}

outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "passwords" {
2+
sensitive = true
3+
value = { for user, pwd in random_password.sql_user : user => pwd.result }
4+
}

user.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "random_password" "sql_user" {
2+
for_each = local.users
3+
4+
length = 48
5+
}
6+
7+
resource "google_sql_user" "sql_user" {
8+
for_each = local.users
9+
10+
instance = google_sql_database_instance.instance.name
11+
name = each.value.name
12+
host = each.value.host
13+
password = random_password.sql_user[each.key].result
14+
}

variables.tf

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
variable "backup_config" {
2+
type = object({
3+
binary_log_enabled = optional(bool)
4+
enabled = optional(bool)
5+
location = optional(string)
6+
})
7+
default = {
8+
}
9+
}
10+
11+
variable "database_version" {
12+
type = string
13+
}
14+
15+
variable "databases" {
16+
type = list(string)
17+
}
18+
19+
variable "environment" {
20+
type = string
21+
validation {
22+
condition = contains(["production", "staging"], var.environment)
23+
error_message = "Environment must be production or staging."
24+
}
25+
}
26+
27+
variable "flags" {
28+
type = map(string)
29+
default = {}
30+
}
31+
32+
variable "highly_available" {
33+
type = bool
34+
}
35+
36+
variable "instance_name" {
37+
type = string
38+
}
39+
40+
variable "labels" {
41+
type = map(string)
42+
default = {}
43+
}
44+
45+
variable "primary_instance_name" {
46+
type = string
47+
default = null
48+
}
49+
50+
variable "storage_autoresize" {
51+
type = bool
52+
}
53+
54+
variable "storage_limit" {
55+
type = number
56+
default = 0
57+
}
58+
59+
variable "storage_size" {
60+
type = number
61+
default = 0
62+
}
63+
64+
variable "tier" {
65+
type = string
66+
default = null
67+
}
68+
69+
variable "users" {
70+
type = list(object({
71+
name = string
72+
host = string
73+
readonly = optional(bool)
74+
}))
75+
}

0 commit comments

Comments
 (0)