Skip to content

Commit

Permalink
Merge pull request #105 from yunkon-kim/241111-20
Browse files Browse the repository at this point in the history
Support creating DB instance in GCP
  • Loading branch information
yunkon-kim authored Nov 11, 2024
2 parents d66969e + a33c06d commit dab1d94
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
11 changes: 11 additions & 0 deletions templates/sql-db/gcp/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Outputs wrapped in SqlDbInstanceInfo object, including public IP
output "sql_db_info" {
value = {
instance_name = google_sql_database_instance.instance.name
database_name = google_sql_database.engine.name
database_user = google_sql_user.admin_user.name
connection_name = google_sql_database_instance.instance.connection_name
public_ip = google_sql_database_instance.instance.public_ip_address
}
description = "Information for SQL Database instance, including instance name, database name, user, connection name, and public IP address"
}
31 changes: 31 additions & 0 deletions templates/sql-db/gcp/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Define the required version of Terraform and the providers that will be used in the project
terraform {
# Required Tofu version
required_version = "~>1.8.3"

required_providers {
# Google provider is specified with its source and version
google = {
source = "hashicorp/google"
version = "~>5.21"
}
}
}

# CAUTION: Manage your credentials carefully to avoid disclosure.
locals {
# Read and assign credential JSON string
my_gcp_credential = file("../../../secrets/credential-gcp.json")
# Decode JSON string and get project ID
my_gcp_project_id = jsondecode(local.my_gcp_credential).project_id
}

# Provider block for Google specifies the configuration for the provider
provider "google" {
credentials = local.my_gcp_credential

project = local.my_gcp_project_id
region = var.csp_region
# zone = "asia-northeast3-c"
}

25 changes: 25 additions & 0 deletions templates/sql-db/gcp/sql-db.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Create SQL MySQL instance
resource "google_sql_database_instance" "instance" {
name = "${var.terrarium_id}-db-instance"
database_version = var.db_engine_version # Specify the MySQL version you need, such as MYSQL_8_0
# region = "us-central1"

settings {
tier = var.db_instance_spec # Set the instance type, such as db-f1-micro
}

# deletion_protection = false # Disable deletion protection
}

# Create database
resource "google_sql_database" "engine" {
name = "${var.terrarium_id}-db-engine"
instance = google_sql_database_instance.instance.name
}

# Create user (optional)
resource "google_sql_user" "admin_user" {
instance = google_sql_database_instance.instance.name
name = var.db_admin_username
password = var.db_admin_username # Set a strong password
}
101 changes: 101 additions & 0 deletions templates/sql-db/gcp/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
variable "terrarium_id" {
type = string
description = "Unique ID to distinguish and manage infrastructure."

validation {
condition = var.terrarium_id != ""
error_message = "The terrarium ID must be set"
}
}


#######################################################################
# Google Cloud Platform (GCP)

variable "csp_region" {
type = string
description = "A location (region) in MS Azure."
default = "asia-northeast3"

}

# variable "csp_resource_group" {
# type = string
# default = "tr-rg-01"
# description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
# }

# variable "azure-virtual-network-name" {
# type = string
# description = "A virtual network name in MS Azure."
# default = "tr-azure-vnet"
# }

# # Required network information
# variable "csp_vnet_id" {
# type = string
# description = "The VPC ID in AWS."
# }

# variable "csp_subnet1_id" {
# type = string
# description = "The subnet ID in Azure."
# }

# variable "csp_subnet2_id" {
# type = string
# description = "The subnet ID in AWS."
# }

# # Required security group information
# variable "db_engine_port" {
# type = number
# description = "The port number for the database engine."
# default = 3306
# }

# variable "ingress_cidr_block" {
# type = string
# description = "The CIDR block for ingress traffic."
# default = "0.0.0.0/0"
# }

# variable "egress_cidr_block" {
# type = string
# description = "The CIDR block for egress traffic."
# default = "0.0.0.0/0"
# }

# # Required database engine information
# variable "db_instance_identifier" {
# type = string
# description = "The identifier for the database."
# default = "mydbinstance"
# }

variable "db_engine_version" {
type = string
description = "The version of the database engine."
default = "MYSQL_8_0"
}

variable "db_instance_spec" {
type = string
description = "The instance class for the database."
default = "db-f1-micro"
}

variable "db_admin_username" {
type = string
description = "The admin username for the database."
default = "mydbadmin"
}

# NOTE - "administrator_password" must contain characters from three of the categories
# – uppercase letters, lowercase letters, numbers and non-alphanumeric characters, got mysdbpass
variable "db_admin_password" {
type = string
description = "The admin password for the database."
default = "P@ssword1234!"
}

0 comments on commit dab1d94

Please sign in to comment.