4

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?

2 Answers2

3

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 $@
1

Not sure if it'll work but try to use depends_on for the instance resource like

depends_on = ["google_sql_database_instance.ResourceNameHere"]
Mike
  • 21,910
  • 7
  • 55
  • 79