Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x-yuri/a4512ff4da5321269a6824e77962a571 to your computer and use it in GitHub Desktop.
Save x-yuri/a4512ff4da5321269a6824e77962a571 to your computer and use it in GitHub Desktop.
Cloud SQL and private IPs (Incorrect Service Networking config for instance: PROJECT_ID:test-sql:SERVICE_NETWORKING_NOT_ENABLED)

Cloud SQL and private IPs (Incorrect Service Networking config for instance: PROJECT_ID:test-sql:SERVICE_NETWORKING_NOT_ENABLED)

main.tf:

provider "google" {
  project = "PROJECT_ID"
}


resource "google_sql_database_instance" "test-sql" {
  deletion_protection = false
  name = "test-sql"
  database_version = "POSTGRES_15"
  region = "europe-central2"
  settings {
    tier = "db-f1-micro"
    ip_configuration {
      ipv4_enabled = false
      private_network = google_compute_network.test-sql.id
    }
  }
  # depends_on = [google_service_networking_connection.test-sql]
}


# resource "google_project_service" "test-sql" {
#   service = "servicenetworking.googleapis.com"
# }
# 
# resource "google_service_networking_connection" "test-sql" {
#   network = google_compute_network.test-sql.id
#   service = "servicenetworking.googleapis.com"
#   reserved_peering_ranges = [google_compute_global_address.test-sql.name]
# }
# 
# resource "google_compute_global_address" "test-sql" {
#   name = "test-sql"
#   purpose = "VPC_PEERING"
#   address_type = "INTERNAL"
#   prefix_length = 16
#   network = google_compute_network.test-sql.id
# }


resource "google_compute_network" "test-sql" {
  name = "test-sql"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "test-sql" {
  name = "test-sql"
  ip_cidr_range = "10.0.0.0/20"
  region = "europe-central2"
  network = google_compute_network.test-sql.self_link
}

Enable compute.googleapis.com if not already enabled:

https://console.developers.google.com/apis/api/compute.googleapis.com/overview?project=PROJECT_ID

// replace PROJECT_ID
$ docker run --rm -itv "$PWD:/app" -w /app google/cloud-sdk:457.0.0-alpine
/app # gcloud auth login --update-adc
/app # apk add terraform
/app # terraform init
/app # terraform apply; echo -e '\a'
...
╷
│ Error: Error, failed to create instance test-sql: googleapi: Error 400: Invalid request: Incorrect Service Networking config for instance: PROJECT_ID:test-sql:SERVICE_NETWORKING_NOT_ENABLED., invalid
│
│   with google_sql_database_instance.test-sql,
│   on main.tf line 6, in resource "google_sql_database_instance" "test-sql":
│    6: resource "google_sql_database_instance" "test-sql" {
│
╵

Uncomment google_project_service.test-sql.

/app # terraform apply; echo -e '\a'
...
╷
│ Error: Error, failed to create instance because the network doesn't have at least 1 private services connection. Please see https://cloud.google.com/sql/docs/mysql/private-ip#network_requirements for how to create this connection.
│
│   with google_sql_database_instance.test-sql,
│   on main.tf line 6, in resource "google_sql_database_instance" "test-sql":
│    6: resource "google_sql_database_instance" "test-sql" {
│
╵

Uncommend the rest.

/app # terraform apply; echo -e '\a'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment