How do I create a GCP SQL database sequentially in Terraform?

Viewed 749

For , I need to be able to provision multiple SQL DBs in GCP. These DBs also need to be on a VPC with a private IP address.

Config

resource "google_compute_network" "container_network" {
  name = "container-network"
}

resource "google_compute_global_address" "sql" {
  name = "sql-private-ip"
  purpose = "VPC_PEERING"
  address_type = "INTERNAL"
  prefix_length = 16
  network = google_compute_network.container_network.self_link
}

resource "google_service_networking_connection" "sql" {
  network = google_compute_network.container_network.self_link
  service = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.sql.name]
}

resource "google_sql_database_instance" "master" {
  provider = google-beta
  count = 1
  database_version = "MYSQL_5_7"

  depends_on = [google_service_networking_connection.sql]

  settings {
    tier = "db-f1-micro"
    ip_configuration {
      ipv4_enabled = false
      private_network = google_compute_network.container_network.id
    }
  }
}

When I use this config for a single database, this works fine. If I then change count = 2, that works fine as well. However, if I destroy the infrastructure and set count = 2, it fails.

Error: Error waiting for Create Instance: Failed to create subnetwork. <eye3 title='INVALID_ARGUMENT'/> generic::INVALID_ARGUMENT: New subnetwork overlaps with an IP range (10.28.0.0/24) in one of the active peers of an active peer (consumer-311061328248).

To me, this seems that the two databases are being created simultaneously and try to assign the same IP range.

Is there a way to create the databases sequentially instead or simultaneously? I suspect this will be a Terraform issue, rather than a GCP one although I'm open to ideas on this.

1 Answers

I was testing to see if something fancy like depends on to self:

resource "google_sql_database_instance" "master" {
  provider = google-beta
  count = 2
  database_version = "MYSQL_5_7"

  depends_on = [google_sql_database_instance.master[count.index-1]]

  ...
}

But nope, unfortunately not allowed, we get: Error: Invalid expression

A single static variable reference is required: only attribute access and indexing with constant keys. No calculations, function calls, template expressions, etc are allowed here.

Besides what I mentioned in the comments of using multiple resource "google_sql_database_instance" with dependencies between them (not a big fan of that option)...
the only other option I see is setting the terraform parallelism to 1 that will limit resource creation.
You can read more about it here:
https://www.terraform.io/docs/commands/apply.html#parallelism-n

Related