From fa970c9596fec733d43f1ace52973f437ece7d4a Mon Sep 17 00:00:00 2001 From: Rishi Kumar Date: Mon, 29 Jul 2024 12:51:00 +0530 Subject: [PATCH] feat: Automate connection profile --- modules/postgresql/gcp/main.tf | 18 ++++++++++++++++++ modules/postgresql/gcp/variables.tf | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/modules/postgresql/gcp/main.tf b/modules/postgresql/gcp/main.tf index f21bd8c3..472195c5 100644 --- a/modules/postgresql/gcp/main.tf +++ b/modules/postgresql/gcp/main.tf @@ -13,6 +13,23 @@ resource "postgresql_extension" "pglogical" { database = "postgres" } +resource "google_database_migration_service_connection_profile" "connection_profile" { + count = local.upgradable ? 1 : 0 + location = local.region + connection_profile_id = "${google_sql_database_instance.instance.name}-id" + display_name = "${google_sql_database_instance.instance.name}-connection-profile" + + postgresql { + cloud_sql_id = google_sql_database_instance.instance.name + host = google_sql_database_instance.instance.private_ip_address + port = local.database_port + + username = google_sql_user.admin.name + password = google_sql_user.admin.password + } + depends_on = [google_sql_database_instance.instance] +} + resource "google_sql_database_instance" "instance" { name = "${local.instance_name}-${random_id.db_name_suffix.hex}" @@ -119,6 +136,7 @@ module "database" { provider "postgresql" { host = google_sql_database_instance.instance.private_ip_address + port = local.database_port username = google_sql_user.admin.name password = random_password.admin.result diff --git a/modules/postgresql/gcp/variables.tf b/modules/postgresql/gcp/variables.tf index 70eea970..1aa4311d 100644 --- a/modules/postgresql/gcp/variables.tf +++ b/modules/postgresql/gcp/variables.tf @@ -51,6 +51,12 @@ variable "upgradable" { default = false } +variable "database_port" { + description = "Instance is upgradable via Database Migration Service" + type = number + default = 5432 +} + locals { gcp_project = var.gcp_project vpc_name = var.vpc_name @@ -67,4 +73,5 @@ locals { provision_read_replica = var.provision_read_replica big_query_connection_location = var.big_query_connection_location upgradable = var.upgradable + database_port = var.database_port }