Terraform destroy failing because Google SQL user owns databases

terraform

I'm using Terraform to provision a Google Cloud SQL PostgreSQL database using a google_sql_database_instance resource. I also create a user with a google_sql_user resource.

After applying, I deploy my application which creates databases owned by that user.

The problem is, when I terraform destroy, Terraform tries to delete the user before it deletes the database. It gets an error (which is expected; you can't delete Postgres users while they own databases). But Terraform doesn't need to delete the user, because the next step is to delete the whole database instance.

Is there a way to tell Terraform that deleting the google_sql_database_instance will have the effect of deleting the google_sql_user, so that Terraform doesn't try to explicity delete the user first and fail?

Best Answer

Use terraform's state rm to tell it to forget that the users and database exist so it won't actively try to delete them (and fail) at destroy time.

I use a destroy.sh script:

terraform state rm module.your_server_name.google_sql_user.users \ 
     module.your_server_name.google_sql_database.your_database_name
terraform destroy $@
Related Topic