GCP Cloud SQL failed to delete instance because `deletion_protection` is set to true - gcloud toggle?

Viewed 5246

Error message:

terraform destroy

module.application.google_sql_database_instance.sql-db-xxx: Destroying... [id=db-xxx]

Error: Error, failed to delete instance because deletion_protection is set to true. Set it to false to proceed with instance deletion

The terraform solution is here:

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance

On newer versions of the provider, you must explicitly set deletion_protection=false (and run terraform >apply to write the field to state) in order to destroy an instance. It is recommended to not set this >field (or set it to true) until you're ready to destroy the instance and its databases.

Question:

I do NOT want to make changes to the terraform script. I would rather toggle the deletion protection flag via gcloud, then destroy as per usual. For gcloud VMs there is a deletion protection flag I can toggle. However, I cannot find the corresponding flag for the database:

cloud sql instances describe db-xxx

  • Is this deletion_protection flag meta data within terraform itself?

  • If not, what is the gcloud command to toggle it?

  • If so, how can I override it via terraform without modifying the code; ie command line parameter?

I have insufficient 'points' to add to the existing thread of a similar title.

1 Answers

To answer your questions:

  1. From Terraform docs:

deletion_protection - Whether or not to allow Terraform to destroy the instance.

So yes, this is within Terraform itself. Deletion protection flag on GCP is currently only available on Compute Engine instances, not Cloud SQL instances.

  1. Currently, deletion protection can only be toggled on a Compute Engine Instance.

  2. You may consider using input variables like this:

terraform apply -var="deletion_protection=false"
terraform destroy

There are also other ways to use input variables. For more reference, here's the link.

Related