Can't figure out the Terraform configuration equivalent for creating address for VPC Peering and networking service for GCP

Viewed 384

I'd like to access my CloudSQL instance from my GKE Cluster pods. Since I am setting up I need to bring everything GCP related to the Terraform configuration files.

Following this tutorial: https://cloud.google.com/sql/docs/mysql/quickstart-kubernetes-engine#gcloud_2

I am stuck with two steps that describe the creating of a peering vpc connection.

So my question is what are the Terraform equivalents for the following two commands:

gcloud compute addresses create google-managed-services-default \
--global \
--purpose=VPC_PEERING \
--prefix-length=16 \
--description="peering range for Google" \
--network=default

and

gcloud services vpc-peerings connect \
--service=servicenetworking.googleapis.com \
--ranges=google-managed-services-default \
--network=default

I tried to find it in https://registry.terraform.io/providers/hashicorp/google/latest/docs but could not find the proper ones.

1 Answers

Creating the address would be like this:

resource "google_compute_global_address" "address_resource" {
  provider      = google-beta # from docs
  name          = "google-managed-services-default"
  address_type  = "INTERNAL"
  purpose       = "VPC_PEERING"
  prefix_length = "16"
  network       = google_compute_network.network_resource.id
  address       = "REPLACE_WITH_VALUE" # or remove it to automatically select an unused address range in your VPC network
}

and the service connection would be like this:

resource "google_service_networking_connection" "foobar" {
  network                 = google_compute_network.network_resource.id
  service                 = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.address_resource.name]
}

links:

Related