Home > database >  How to setup Terraform GCP PostgreSQL DB with private IP on default network
How to setup Terraform GCP PostgreSQL DB with private IP on default network

Time:07-22

I am trying to setup a PostgreSQL DB on GCP using terraform with a private IP on the default network. See following of GCP GUI equivalent setting:

enter image description here

I have deployed successfully this example but it creates a new vpc network private-network: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance#private-ip-instance

But I do not wish to create a new private network, and I just want to use the "default" one defined with the VPC. Every configuration I have tried within terraform usually results in either (1) wrong syntax, or (2) default network already exists therefore cannot be created.

data "google_compute_network" "default" {
  name = "default-us-west1"
}
        
resource "google_compute_global_address" "private_ip_address" {
  provider      = google
  name          = "private-ip-address"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = data.google_compute_network.default.id
}

resource "google_service_networking_connection" "private_vpc_connection" {
  provider                = google
  network                 = data.google_compute_network.default.id
  service                 = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
}

resource "random_id" "db_name_suffix" {
  byte_length = 4
}

resource "google_sql_database_instance" "main" {
  name             = "test-db"
  database_version = "POSTGRES_12"
  region           = "us-west1"
  
  depends_on = [google_service_networking_connection.private_vpc_connection] 

  settings {
    availability_type = "REGIONAL"
    tier              = "db-custom-2-8192"
    disk_size         = "10"
    disk_type         = "PD_SSD"
    disk_autoresize   = "true"
    ip_configuration {
      ipv4_enabled    = "false"
      private_network = data.google_compute_network.default.id
    }
  }
}

CodePudding user response:

The original post used the resource for GCP network. Using resource blocks always creates a new resource instead of using the one that is already present. To poll the information about the resources that already exist, it is always a good idea to use data sources [1]. The problem here seems to be that GCP did not complain about creating a network. In AWS for example if you were to try that, there would be an error and you would know what the reason for the error was. So, to fix the issue with creating a DB in the default network provided by GCP it is enough to use the data source but the name of the network has to be correct. As per conversation in the comments, the data source block should look like:

data "google_compute_network" "default" {
  name = "default"
}

Then, since this data source outputs id as an attribute [2], it is enough to reference that value everywhere where it is required with:

data.google_compute_network.default.id

Additionally, the documentation for the SQL DB says that the depends_on meta-argument has to be used [3]:

For private IP instance setup, note that the google_sql_database_instance does not actually interpolate values from google_service_networking_connection. You must explicitly add a depends_on reference as shown below.


[1] https://www.terraform.io/language/data-sources

[2] https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_network#id

[3] https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance#private-ip-instance

  • Related