Failing to create Cloud SQL Instance through Terraform

1.6k views Asked by At

I want to create a Cloud SQL instance in a project, connected to a host VPC. However, when running terraform apply I get the same result every time:

google_sql_database_instance.default: Still creating... [1m20s elapsed]
google_sql_database_instance.default: Still creating... [1m30s elapsed]
google_sql_database_instance.default: Still creating... [1m40s elapsed]
google_sql_database_instance.default: Still creating... [1m50s elapsed]

Error: Error waiting for Create Instance: 



Error: Process completed with exit code 1.

I saw other answers here that claim that the problem goes away by configuring private service access, however this does not seem to fix my problem.

Here is my terraform file, with the relevant sections:

google_compute_global_address" "private_ip_address" {
  provider = google-beta

  project       = data.google_compute_subnetwork.subnet.project
  name          = "private-ip-address"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = data.google_compute_network.shared-vpc.id
}

resource "google_service_networking_connection" "private_vpc_connection" {
  provider = google-beta

  network                 = data.google_compute_network.shared-vpc.id
  service                 = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
}

resource "google_sql_database_instance" "default" {
  provider            = google-beta
  project             = module.base_shared_vpc_project.project_id
  name                = var.db_name
  database_version    = "MYSQL_5_7"
  region              = "us-east1"

  settings {
    tier                        = "db-f1-micro"
    availability_type           = "ZONAL"

    location_preference {
      zone = "us-east1-b"
    }

    ip_configuration {
      ipv4_enabled    = false
      private_network = data.google_compute_network.shared-vpc.id
    }
  }
  depends_on = [google_service_networking_connection.private_vpc_connection]
}

One final bit of information, setting TF_LOG=DEBUG provides only this additional message: Retry Transport: Stopping retries, last request failed with non-retryable error

1

There are 1 answers

1
ingernet On

A couple of possibilities:

  1. There is a chance that the Cloud SQL API timeout has been triggered but the process is still going. I ran into this when running a gcloud sql import sql command with a large data dump file. I got a "timeout exceeded" error but the process continued, as evidenced by the Disk Usage monitoring in the console.
  2. One thing GCP doesn't make a big deal about THAT IS ACTUALLY A BIG DEAL WITH IAC is that there is a 10-day moratorium on re-using the name of a Cloud SQL Instance that has been deleted. So if your IAC is attempting to recreate a Cloud SQL instance that has existed in the last 10 days, that may be part of the problem. That problem has made my team tableflip a couple of times. Try putting a datetimestamp as a variable in your "google_sql_database_instance " resource name.