Skip to content

Commit c9db0fc

Browse files
authored
Merge pull request #415 from cloudfoundry/improve_db_setup
Improve db deletion protection
2 parents 4c60789 + e6efb9b commit c9db0fc

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

docs/concourse/README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,26 @@ terragrunt run-all apply
111111
## Recommendations
112112
### Cloud SQL Instance deletion protection
113113

114-
Terraform hashicorp provider includes a deletion protection flag however in some cases it's misleading as it's not setting it on Google Cloud.
115-
To avoid confusion we do not set it in the code and recommend altering your production SQL Instance to protect from the deletion on the cloud side.
114+
The [database.tf](../../terraform-modules/concourse/infra/database.tf) configuration enables deletion protection on multiple levels. The Terraform hashicorp provider includes a deletion protection flag:
115+
```
116+
resource "google_sql_database_instance" "concourse" {
117+
118+
# This option prevents Terraform from deleting an instance
119+
deletion_protection = true
120+
```
121+
Note that if you really want to delete the database, Terraform will not allow this because `deletion_protection = true` is stored in the state. You first have to disable this flag, then run `apply` and then you can run a deletion operation.
122+
123+
In addition, we are setting a flag that enables the "Prevent instance deletion" option from the GCP console:
124+
```
125+
settings {
126+
deletion_protection_enabled = "true"
127+
}
128+
```
129+
130+
:warning: The option "Retain backups after instance deletion" should also be enabled. There is no Terraform configuration parameter,
131+
so you have to set it manually in the GCP console:
116132

117-
https://console.cloud.google.com/sql/instances/ -> select instance name -> edit -> Data Protection -> tick: Enable delete protection
133+
Cloud SQL -> Instances -> Edit configuration -> Data Protection -> Retain backups after instance deletion
118134

119135
### End-to-end testing
120136

docs/concourse/concourse_minor_version_upgrade.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ Please note the process should be also useful for upgrading major versions.
2020
```
2121
terragrunt run-all plan --terragrunt-source-update
2222
```
23+
Note: The Cloud SQL database has "automatic storage increases" enabled. So the disk could have grown larger than the initial value from the `config.yaml` file. In that case, Terraform would have to shrink the disk which is not possible. Instead, it tries to destroy the database and recreate it, losing all data including backups:
24+
```
25+
STDOUT [infra] tofu: -/+ resource "google_sql_database_instance" "concourse" {
26+
STDOUT [infra] tofu: ~ settings {
27+
STDOUT [infra] tofu: ~ disk_size = 44 -> 38 # forces replacement
28+
(...)
29+
STDOUT [infra] tofu: Plan: 1 to add, 1 to change, 1 to destroy.
30+
```
31+
Deletion protection is enabled on Terraform level, so this change could not be applied. To proceed, configure a bigger `sql_instance_disk_size` in the `config.yaml`.
2332

2433
4. Switch to `renovate's` pull request having bumped Concourse helm chart version
2534
```
@@ -32,7 +41,16 @@ Please note the process should be also useful for upgrading major versions.
3241
../terragrunt/scripts/concourse/create-sql-backup.sh
3342
```
3443

35-
6. Apply roll-out for new Concourse version
44+
6. Check Cloud SQL "Data Protection" settings
45+
46+
In the GCP console, navigate to:
47+
https://console.cloud.google.com/sql/instances/ -> select instance name -> edit -> Data Protection
48+
49+
Make sure the following flags are enabled:
50+
* "Prevent instance deletion"
51+
* "Retain backups after instance deletion"
52+
53+
7. Apply roll-out for new Concourse version
3654
```
3755
terragrunt run-all apply --terragrunt-source-update
3856
```

terraform-modules/concourse/infra/database.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ resource "google_sql_database_instance" "concourse" {
44
project = var.project
55
region = var.region
66

7-
# recommended protection via GCP SQL Instance settings
8-
# https://console.cloud.google.com/sql/instances/ -> select instance name -> edit
9-
# -> Data Protection -> tick: Enable delete protection
10-
deletion_protection = false
7+
# This option prevents Terraform from deleting an instance
8+
deletion_protection = true
119

1210
settings {
1311
activation_policy = "ALWAYS"
@@ -28,6 +26,8 @@ resource "google_sql_database_instance" "concourse" {
2826
transaction_log_retention_days = "7"
2927
}
3028

29+
deletion_protection_enabled = "true"
30+
3131
disk_autoresize = "true"
3232
disk_autoresize_limit = "0"
3333
disk_size = var.sql_instance_disk_size

0 commit comments

Comments
 (0)