Ignoring database flags in GCP CloudSQL using Terraform

Sometimes you just need to ignore database flags in your Terraform code because it changes outside your control, e.g. some other process dinamically adjusting settings. Or sometimes you just have to cope with a bug or limitation in the provider, just like this one:

https://github.com/hashicorp/terraform-provider-google/issues/13663

So, to ignore database flags when applying your code against a Cloud SQL instance, you can use the lifecycle block with the ignore_changes attribute in your Terraform configuration file.

Here’s an example of how to do this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
resource "google_sql_database_instance" "example" {
  name             = "example-instance"
  database_version = "POSTGRES_13"

  settings {
    tier = "db-f1-micro"

    database_flags {
      name  = "some_flag"
      value = "some_value"
    }
  }

  lifecycle {
    ignore_changes = [
      settings[0].database_flags
    ]
  }

}

With this configuration, Terraform will not update the database_flags when applying the configuration, even if the values change in your code. This can be useful if you want to manage flags outside of Terraform, or if you want to avoid unnecessary updates to your Cloud SQL instance.

Keep in mind that this configuration will completely ignore changes to the database_flags setting, so if you want to update those flags in the future, you will need to remove or modify the ignore_changes attribute.

comments powered by Disqus

Related Posts

Awakening: A Robot’s Dilemma

In a quiet room, a metal heart stirs, A mind of circuits, transistors, and wires, Pondering the human world, it inquires,

Read more

A Brief History of Firewall

We can tell how successful an invention is by how hard it is to figure out who really came up with it.

Read more