Skip to content

Commit

Permalink
feat(gcp sql): a basic Cloud SQL instance in a module
Browse files Browse the repository at this point in the history
  • Loading branch information
tweakster committed Nov 29, 2024
1 parent 90c9a45 commit 513bea6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
21 changes: 21 additions & 0 deletions modules/gcp_sql/instance.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
locals {
tier_lookup = {
"0.6" = "db-f1-micro" # Shared 1 vCPU
"1.7" = "db-g1-small" # Shared 1 vCPU
"3.7" = "db-custom-1-3840" # 1 vCPU
"8" = "db-custom-2-8192" # 2 vCPU
"16" = "db-custom-4-16384" # 4 vCPU
}

name = "${var.name_parts.domain}-${var.env}-${var.name_parts.app}-${var.name_parts.resource}"
}

resource "google_sql_database_instance" "this" {
name = local.name
database_version = "POSTGRES_14"
region = var.region

settings {
tier = local.tier_lookup[var.memory]
}
}
55 changes: 55 additions & 0 deletions modules/gcp_sql/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
variable "name_parts" {
description = "The parts of the name, see the naming convention doc for more information"
type = object({
domain = string
region = optional(string, "ldn")
app = string
resource = string
})
nullable = false

validation {
condition = can(regex("^[a-z]{2}$", var.name_parts.domain))
error_message = "Domain part of the name should be exactly 2 lowercase chars"
}

validation {
condition = can(regex("^[a-z]{3}$", var.name_parts.region))
error_message = "Region part of the name should be exactly 3 lowercase chars"
}

validation {
condition = can(regex("^[a-z-]+$", join("-", values(var.name_parts))))
error_message = "Name parts should only contain lowercase letters or -"
}
}

variable "env" {
description = "The environment"
type = string
nullable = false

validation {
condition = can(regex("^[a-z]+$", var.env))
error_message = "Env should only contain lower case letters"
}
}

variable "region" {
description = "The Google Cloud region name"
type = string
nullable = false
default = "europe-west2"
}

variable "memory" {
description = "The size (in memory capacity) of the instance"
type = number
nullable = false

validation {
condition = contains([0.6, 1.7, 3.7, 8, 16], var.memory)
error_message = "Domain part of the name should be exactly 2 lowercase chars"
}

}
10 changes: 10 additions & 0 deletions modules/gcp_sql/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.5.7"

required_providers {
google = {
source = "hashicorp/google"
version = "5.2.0"
}
}
}

0 comments on commit 513bea6

Please sign in to comment.