Issue creating Service connection between network and cloud sql in google cloud

34 views Asked by At

I am creating custom network and cloud SQL in Google Cloud. I am using module approach while creating.

below is my block of code of main.tf

module "network" {
  source = "./cloudNetwork"
  subnet_count = var.subnets
  subnet_names = var.sub_names
  region = var.region
  vpc_cidr = var.cidr
}

module "cloudsql" {
  source = "./cloudSQL"
  depends_on = [module.network]
  custom_network = module.network.vpc_network_self_link
  db_instance_name = var.db_name
  db_version = var.versions
  db_storage_size = var.dbsize
  db_machine_type = var.type
  query_insights_enabled = var.insights
  db_labels = var.labels
  dbname = var.name
  password = var.dbpassword
  dbuser = var.user
  address = local.my_ip_address
  region = var.region
  
}

My Network Configuration,

resource "google_compute_network" "vpc_network" {
  name = "terraform-network"
  auto_create_subnetworks = false
  routing_mode = "REGIONAL"
 
}

resource "google_compute_subnetwork" "my_subnets" {
  count           = var.subnet_count
  name            = var.subnet_names[count.index]
  network         = google_compute_network.vpc_network.name
  region          = var.region 
  ip_cidr_range   = cidrsubnet(var.vpc_cidr, 8, count.index)
}

resource "google_service_networking_connection" "private_service_connection" {
  network    = google_compute_network.vpc_network.name
  service    = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [element(google_compute_subnetwork.my_subnets[*].ip_cidr_range, 1)]
  depends_on = [google_compute_subnetwork.my_subnets]
}

Error I am getting like below,

ERROR

Anyone have any idea? Suggest me what I have done wrong..

Thank You

1

There are 1 answers

0
Hardik Patel On

Below changes I have made in code,

resource "google_compute_global_address" "private_ip_address" {
  //provider = google-beta

  name          = "private-ip-address"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = google_compute_network.vpc_network.id
}

resource "google_service_networking_connection" "private_service_connection" {
  network    = google_compute_network.vpc_network.name
  service    = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
  
  //[element(google_compute_subnetwork.my_subnets[*].ip_cidr_range, 1)]
  
}

-----------

cloudSQL side

resource "google_sql_database_instance" "my_instance" {
  name             = var.db_instance_name
  database_version = var.db_version
  region           = var.region  
  deletion_protection = false
  

  depends_on = [var.connection]

  
}


------------
main.tf

module "cloudsql" {
  source = "./cloudSQL"
  depends_on = [module.network]
  custom_network = module.network.vpc_network_id
  connection = module.network.private_conn
  db_instance_name = var.db_name
  db_version = var.versions
  db_storage_size = var.dbsize
  db_machine_type = var.type
  query_insights_enabled = var.insights
  db_labels = var.labels
  dbname = var.name
  password = var.dbpassword
  dbuser = var.user
  address = local.my_ip_address
  region = var.region
  
}

I got success. I am able to create cloudSQL and custom network along with private Ip address for connectivity.