Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - Creating Google Cloud SQL instance not working

I use the following Terraform configuration to try to create a subnet and a Cloud SQL MySQL 5.6 instance on Google Cloud Platform.

resource "google_compute_network" "default" {
  name = "my-default-network"
  auto_create_subnetworks = "true"
  project = "${google_project.project.project_id}"
}

resource "google_sql_database_instance" "wordpress" {
  region = "${var.region}"
  database_version = "MYSQL_5_6"
  project = "${google_project.project.project_id}"

  settings {
    tier = "db-n1-standard-1"

    ip_configuration {
      private_network = "${google_compute_network.default.self_link}"
    }
  }
}

But applying this plan gives me the following vague error. I also tried to destroy the entire project and tried to build it up again, but I get the same error.

google_sql_database_instance.wordpress: Still creating... (20s elapsed)
google_sql_database_instance.wordpress: Still creating... (30s elapsed)
google_sql_database_instance.wordpress: Still creating... (40s elapsed)

Error: Error applying plan:

1 error(s) occurred:

* google_sql_database_instance.wordpress: 1 error(s) occurred:

* google_sql_database_instance.wordpress: Error waiting for Create Instance:


Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

Can anyone see what I do wrong here?

Edit:

When adding TF_LOG=debug to the terraform apply-run, I get the following error.

"error": {
  "kind": "sql#operationErrors",
  "errors": [{
    "kind": "sql#operationError",
    "code": "INTERNAL_ERROR"
  }]
}

Edit 2: Simplified the network setup, but getting the exact same error.

like image 427
pkhamre Avatar asked Feb 25 '19 11:02

pkhamre


People also ask

Does Terraform work with Google Cloud?

Try Terraform on Google Cloud tutorials, courses, and self-paced training from Google Cloud Skills Boost . In this lab, you install Terraform and create a VM instance using Terraform. In this lab, you write infrastructure as code with Terraform.

How do I connect to Gcloud SQL instance?

In the Google Cloud console, go to the Cloud SQL Instances page. To open the Overview page of an instance, click the instance name. Select Connections from the SQL navigation menu. In the Authorized networks section, click Add network and enter the IP address of the machine where the client is installed.


1 Answers

A bit late to the party but I have just had and overcome this issue. In my case it was related to using the private_networking option. My suggestion is to read the documentation paying attention to the "Network Requirements" and check the following:

  • You have the servicenetworking.googleapis.com API enabled in your project
  • The ServiceAccount you are running with Terraform has the "Service Network Admin" role

I found that verifying private networking was the issue (by removing it and setting ipv4_enabled = "true") in a temporary instance helped focus my debugging efforts.

Good Luck!

like image 119
Ewan Avatar answered Nov 17 '22 01:11

Ewan