1

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
    }
  }
}
9
  • Can you add the code you have tried, even the one that results in an error? Commented Jul 21, 2022 at 12:52
  • Added my most recent attempt. Commented Jul 21, 2022 at 14:00
  • Ah, ok, so when you are using resources you are basically creating a new network. Does the default network exist out of the box with GCP? If so, you might be able to query it with a data source. Commented Jul 21, 2022 at 14:06
  • 1
    For reference: registry.terraform.io/providers/hashicorp/google/latest/docs/…. Commented Jul 21, 2022 at 15:13
  • Ah I did not previously know I can reference existing resources like that. I am still having trouble getting the syntax. I have tried to reference the default network as: data.google_compute_network.default.id, and google_compute_network.default.id with no luck. See the edited code block in the original post. Commented Jul 21, 2022 at 15:42

1 Answer 1

4

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 another network named default. 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.