0

Planning to create the Postgres Instance using the Backstage, Terraform via pre-defined templates.

While running the Terraform plan, stuck with the below error:

Error: googleapi\*\*: Error 404: The Cloud SQL instance does not exist., instanceDoesNotExist\*\*
with module.postgres_instance.google_sql_database.databases\["default_db"\]
on .terraform/modules/postgres_instance/main.tf line 1, in resource "google_sql_database" "databases":
resource "google_sql_database" "databases" {

Error: googleapi: **Error 404: The Cloud SQL instance does not exist., instanceDoesNotExist**
with module.postgres_instance.google_sql_user.iam_service_account\[0\]
on .terraform/modules/postgres_instance/iam.tf line 10, in resource "google_sql_user" "iam_service_account":
resource "google_sql_user" "iam_service_account" {

Tried below ways to resolve the issue, Still stuck with the same error. Can someone please help me how to resolve the above issue?

  1. Verified the gcloud console not found the instances under my ProjectS C:\Windows\system32> gcloud sql instances list
  2. Added the resources under my terraform configuration:
resource "google_sql_user" "iam_service_account" {
instance = "ew2-wrk-dev-wrk01-svp-sqp-db1sq"
name     = "[email protected]"
project  = "XXXXXXX"
type     = "CLOUD_IAM_SERVICE_ACCOUNT"
}

resource "google_sql_database" "databases" {
name     = "default_db"
instance = "ew2-wrk-dev-wrk01-svp-sqp-db1sq"
project  = "XXXXXX"
}
2
  • simplify your goal ... are you able to spin up a virtual machine using backstage ? master that first ... understand what is needed to have a running postgres server ... I would ignore backstage until you can achieve what you want using terraform directly ... backstage is a layer on top which by definition abstracts out underlying details knowledge of which may well give you intuition of this current issue ... does terraform or backstage give you under the covers a VPC, subnet, VM on top of which it then installs the db server ... its good to chop up a question into stepping stones Commented Apr 23, 2025 at 13:35
  • Thanks for the quick Response @ScottStensland. No, I am unable to spin the VM it self, as Terraform plan is failed. We are using suggested curated Module. Removed the all Network, Service account, Just tried with the Terraform plan, stuck with the above error. Commented Apr 24, 2025 at 7:04

1 Answer 1

0

This error is the result of confusion between a Cloud SQL instance and a Cloud SQL database (two different things).

You must first create a Cloud SQL instance and then you can create a database attached to the instance.

The resource google_sql_database must be attached to a Cloud SQL instance via the instance parameter, which you have set to instance = "ew2-wrk-dev-wrk01-svp-sqp-db1sq", as your error is stating a Cloud SQL instance with the name ew2-wrk-dev-wrk01-svp-sqp-db1sq does not exist.

You must first create a resource of type google_sql_database_instance before you can create a database.

From example in Terraform doc:

# Cloud SQL instance
resource "google_sql_database_instance" "instance" {
  name             = "my-database-instance"
  region           = "us-central1"
  database_version = "MYSQL_8_0"
  settings {
    tier = "db-f1-micro"
  }

  deletion_protection  = true
}

# Cloud SQL database (on instance)
resource "google_sql_database" "database" {
  name     = "my-database"
  instance = google_sql_database_instance.instance.name
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.