GCP: Creating private postgre instance in a specific subnet

93 Views Asked by At

I want to create a private postgresql instance that is pointing to a specific subnet in my VPC. I have 2 resources:

resource "google_compute_global_address" "private_ip_address_some_name" {
  name          = "private-ip-address-some-name"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = module.vpc.vpc.id
}

resource "google_service_networking_connection" "some_other_name" {
  network                 = module.vpc.vpc.id
  service                 = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.private_ip_address_some_name.name]
}

And I do understand that this would create the IP in my VPC, but I wish the postgres instance to be created inside a specific subnet inside my VPC.

The VPC kind of looks like this:

dev-01 : {
  vpc_cidr = "x.x.x.x/16"
  subnets = {
    "subnet-a" : ["10.2.1.0/24", "secondary_ip_range/20", "x.x.x.x/20"],
    "subnet-b" : ["10.2.2.0/24", "secondary_ip_range/20", "x.x.x.x/20"],

  }

And the ip_configuration on my postgresql module looks like this:

  ip_configuration = {
    ipv4_enabled       = true                                # Disable public IP
    private_network    = module.vpc.vpc.id                   
    allocated_ip_range = var.network_map[local.env].vpc_cidr 
    require_ssl        = true                               
    authorized_networks = [
      {
        name  = "vpc"
        value = "10.2.1.0/24"
      }
    ]
  }

I have the subnet configured as an output:

output "subnet-a" {
  value = google_compute_subnetwork.db_a
}

I have tried to reference it like so:

private_network    = module.vpc.subnet-a.id

but without any luck.

I am kind of lost on this as every attempt I tried to point towards a subnet has failed and any help will be appreciated.

1

There are 1 best solutions below

0
On

private_network expects the id of a subnet, not a subnet (as suggested by the error, the regex to match must contains global/networks), docs.

Instead, you are passing it the id of a subnetwork, thus the error.