Skip to content

Commit 1922fed

Browse files
authored
doc: Adds example for mongodbatlas_backup_compliance_policy (#3391)
* doc: Initial example for mongodbatlas_backup_compliance_policy * doc: Add links to mongodbatlas_backup_compliance_policy example * chore: fix lint error and example * fix: improve v2 directory detection logic in tf-validate script * fix shell errors * address PR comments * doc: Adds module example * fix tf errors * chore: fix tf-validate script * address PR comments * chore: fix use v2 in the internal cluster_with_schedule module * docs feedback * doc: Simplify module example * doc: Apply docs feedback * pr suggestions * main.tf fixes * doc: Add important notes regarding Backup Compliance Policy deletion considerations * doc: Update support links * doc: Clarify note on using Terraform Module for deletion methods * add links from cluster docs to example * apply PR comments * doc: Add reference to Terraform issue for moved and removed blocks in backup compliance policy example
1 parent 2575215 commit 1922fed

File tree

18 files changed

+492
-34
lines changed

18 files changed

+492
-34
lines changed

docs/resources/advanced_cluster (preview provider 2.0.0).md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ Refer to the following for full privatelink endpoint connection string examples:
604604
[Flex Cluster Backups](https://www.mongodb.com/docs/atlas/backup/cloud-backup/flex-cluster-backup/) for flex clusters.
605605
If "`backup_enabled`" is `false` (default), the cluster doesn't use Atlas backups.
606606

607-
* `retain_backups_enabled` - (Optional) Set to true to retain backup snapshots for the deleted cluster. M10 and above only. This only applies to the `Delete` operation. If you see the `CANNOT_DELETE_SNAPSHOT_WITH_BACKUP_COMPLIANCE_POLICY` error code, set it to explicit `true`.
607+
- `retain_backups_enabled` - (Optional) Set to true to retain backup snapshots for the deleted cluster. M10 and above only. This only applies to the `Delete` operation. If you see the `CANNOT_DELETE_SNAPSHOT_WITH_BACKUP_COMPLIANCE_POLICY` error code, set it to explicit `true`. To learn more, see [this](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/master/examples/mongodbatlas_backup_compliance_policy/resource) example.
608608

609609
**NOTE** Prior version of provider had parameter as `bi_connector` state will migrate it to new value you only need to update parameter in your terraform file
610610

docs/resources/advanced_cluster.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ Refer to the following for full privatelink endpoint connection string examples:
431431
[Flex Cluster Backups](https://www.mongodb.com/docs/atlas/backup/cloud-backup/flex-cluster-backup/) for flex clusters.
432432
If "`backup_enabled`" is `false` (default), the cluster doesn't use Atlas backups.
433433

434-
* `retain_backups_enabled` - (Optional) Set to true to retain backup snapshots for the deleted cluster. M10 and above only. This only applies to the `Delete` operation. If you see the `CANNOT_DELETE_SNAPSHOT_WITH_BACKUP_COMPLIANCE_POLICY` error code, set it to explicit `true`.
434+
* `retain_backups_enabled` - (Optional) Set to true to retain backup snapshots for the deleted cluster. M10 and above only. This only applies to the `Delete` operation. If you see the `CANNOT_DELETE_SNAPSHOT_WITH_BACKUP_COMPLIANCE_POLICY` error code, set it to explicit `true`. To learn more, see [this](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/master/examples/mongodbatlas_backup_compliance_policy/resource) example.
435435

436436
**NOTE** Prior version of provider had parameter as `bi_connector` state will migrate it to new value you only need to update parameter in your terraform file
437437

docs/resources/backup_compliance_policy.md

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,9 @@ When enabled, the Backup Compliance Policy will be applied as the minimum backup
1010

1111
-> **NOTE:** With Backup Compliance Policy enabled, cluster backups are retained after a cluster is deleted and backups can be used normally until retention expiration. When the Backup Compliance Policy is not enabled, Atlas deletes the cluster's associated backup snapshots when a cluster is terminated. By default, a Backup Compliance Policy is not enabled. For more details see [Back Up, Restore, and Archive Data](https://www.mongodb.com/docs/atlas/backup-restore-cluster/).
1212

13-
-> **NOTE:** If you encounter errors when deleting an Atlas Cluster which has an associated `mongodbatlas_cloud_backup_schedule` resource, we suggest the following:
14-
* `mongodbatlas_cloud_backup_schedule` fails when you delete an Atlas cluster because an existing backup compliancy policy is enabled, which is expected.
15-
We first suggest disabling `mongodbatlas_backup_compliance_policy` resource, which requires contacting MongoDB Support and completing an extensive verification process.
16-
* Second, if the first suggestion is not possible, we suggest changing the order in which terraform deletes the resource. This might not always be possible since the `mongodbatlas_cloud_backup_schedule` resource is referencing attributes from the `mongodbatlas_advanced_cluster`, or `mongodbatlas_cluster resource`, and therefore has to delete the backup schedule before deleting the cluster resource. This dependency must be defined as stated in the `mongodbatlas_cloud_backup_schedule` per the [documentation](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/cloud_backup_schedule).
17-
* Third, another alternative is to modify the `mongodbatlas_cloud_backup_schedule` resource by replacing all the references to the `mongodbatlas_advanced_cluster`, or `mongodbatlas_cluster`, resource with the underlying values. This enables you to delete the cluster resource first, but you must obtain values from the state to do the appropriate replacements. Since there is no more reference to previous cluster resource, terraform will allow you to delete the cluster resource without having to delete the backup schedule previously.
18-
* For example, replace:
19-
```
20-
resource "mongodbatlas_cloud_backup_schedule" "test" {
21-
project_id = mongodbatlas_advanced_cluster.my_cluster.project_id
22-
cluster_name = mongodbatlas_advanced_cluster.my_cluster.name
23-
...
24-
}
25-
```
26-
with
27-
```
28-
resource "mongodbatlas_cloud_backup_schedule" "test" {
29-
project_id = "65251446ae5f3f6ec7968b13"
30-
cluster_name = "Cluster0"
31-
...
32-
}
33-
* Lastly, as a final resort taking into account the limitations, we recommend you to do the following:
34-
* Remove the `mongodbatlas_cloud_backup_schedule` resource from the state using `terraform state rm mongodbatlas_cloud_backup_schedule.backup_retention_policy`.
35-
* Remove the `mongodbatlas_cloud_backup_schedule` and `mongodbatlas_advanced_cluster`, or `mongodbatlas_cluster`, resource for the terraform configuration script and apply changes. Now only the cluster delete operation is called and runs successfully.
36-
13+
-> **NOTE:** If you encounter errors when deleting an Atlas Cluster that has an associated `mongodbatlas_cloud_backup_schedule` resource, follow the cleanup steps in the [`mongodbatlas_backup_compliance_policy`](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/examples/mongodbatlas_backup_compliance_policy/README.md#4-cleanup-extra-steps-when-a-backup-compliance-policy-is-enabled) example.
14+
15+
3716
## Example Usage
3817

3918
```terraform

docs/resources/cloud_backup_schedule.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
-> **NOTE** Groups and projects are synonymous terms. You may find `groupId` in the official documentation.
66

7-
-> **NOTE:** If Backup Compliance Policy is enabled for the project for which this backup schedule is defined, you cannot modify the backup schedule for an individual cluster below the minimum requirements set in the Backup Compliance Policy. See [Backup Compliance Policy Prohibited Actions and Considerations](https://www.mongodb.com/docs/atlas/backup/cloud-backup/backup-compliance-policy/#configure-a-backup-compliance-policy).
7+
-> **NOTE:** If Backup Compliance Policy is enabled for the project for which this backup schedule is defined, you cannot modify the backup schedule for an individual cluster below the minimum requirements set in the Backup Compliance Policy. See [Backup Compliance Policy Prohibited Actions and Considerations](https://www.mongodb.com/docs/atlas/backup/cloud-backup/backup-compliance-policy/#configure-a-backup-compliance-policy).
8+
9+
If you need to remove the `mongodbatlas_cloud_backup_schedule`, follow the cleanup steps in the [`mongodbatlas_backup_compliance_policy`](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/examples/mongodbatlas_backup_compliance_policy/README.md#4-cleanup-extra-steps-when-a-backup-compliance-policy-is-enabled) example.
810

911
-> **NOTE:** When creating a backup schedule you **must either** use the `depends_on` clause to indicate the cluster to which it refers **or** specify the values of `project_id` and `cluster_name` as reference of the cluster resource (e.g. `cluster_name = mongodbatlas_advanced_cluster.my_cluster.name` - see the example below). Failure in doing so will result in an error when executing the plan.
1012

docs/resources/cluster.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ But in order to explicitly change `provider_instance_size_name` comment the `lif
305305
```
306306
* The default value is false. M10 and above only.
307307

308-
* `retain_backups_enabled` - (Optional) Set to true to retain backup snapshots for the deleted cluster. M10 and above only. This only applies to the `Delete` operation. If you see the `CANNOT_DELETE_SNAPSHOT_WITH_BACKUP_COMPLIANCE_POLICY` error code, set it to explicit `true`.
308+
* `retain_backups_enabled` - (Optional) Set to true to retain backup snapshots for the deleted cluster. M10 and above only. This only applies to the `Delete` operation. If you see the `CANNOT_DELETE_SNAPSHOT_WITH_BACKUP_COMPLIANCE_POLICY` error code, set it to explicit `true`. To learn more, see [this](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/master/examples/mongodbatlas_backup_compliance_policy/resource) example.
309309
* `bi_connector_config` - (Optional) Specifies BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details.
310310
* `cluster_type` - (Required) Specifies the type of the cluster that you want to modify. You cannot convert a sharded cluster deployment to a replica set deployment.
311311

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Example - MongoDB Atlas Backup Compliance Policy with a module
2+
We recommend first to read the [resource example](../resource/README.md) as it contains all the relevant information. This example module is identical to it except that it is designed to serve as a reference for platform teams who have created their own module and make it available to internal teams for leveraging the MongoDB Atlas Terraform provider. Typically, these users do not have the ability to execute `terraform state` commands or modify the Terraform state manually.
3+
4+
As in the resource example, the attention is focused on how to avoid the [BACKUP_POLICIES_NOT_MEETING_BACKUP_COMPLIANCE_POLICY_REQUIREMENTS](../resource/README.md#4-cleanup-extra-steps-when-a-backup-compliance-policy-is-enabled) when running `terraform destroy` on a `mongodbatlas_advanced_cluster` with `mongodbatlas_cloud_backup_schedule` and enabled backup compliance policy.
5+
6+
To do that, we'll use:
7+
- an `add_schedule` variable provided by the module maintainer that controls the presence of the `mongodbatlas_cloud_backup_schedule` in the configuration
8+
- the `moved` and `removed` blocks used by the module user and added at the root level. For more info on why the two blocks are needed, please read this [Terraform issue](https://github.com/hashicorp/terraform/issues/34439).
9+
10+
-> **IMPORTANT NOTE:** Read the [Backup Compliance Policy Deletion Consideration](../resource/README.md#backup-compliance-policy-deletion-consideration) before running this example.
11+
12+
## How to delete the cluster and retain their backup snapshots
13+
14+
Let's begin by assuming you've created a module instance with the following configuration:
15+
16+
```terraform
17+
module "cluster_with_schedule" {
18+
source = "./modules/cluster_with_schedule"
19+
20+
project_id = var.project_id
21+
instance_size = var.instance_size
22+
cluster_name = var.cluster_name
23+
add_schedule = true
24+
}
25+
```
26+
Note: The `add_schedule` field is set to true, indicating that a `mongodbatlas_cloud_backup_schedule` resource has been defined, as reflected in the module's source code.
27+
28+
To proceed with the deletion, we'll update the configuration as follows:
29+
30+
```terraform
31+
module "cluster_with_schedule" {
32+
source = "./modules/cluster_with_schedule"
33+
34+
project_id = var.project_id
35+
instance_size = var.instance_size
36+
cluster_name = var.cluster_name
37+
add_schedule = false # changed
38+
}
39+
40+
moved {
41+
from = module.cluster_with_schedule.mongodbatlas_cloud_backup_schedule.this[0] # must be deleted with the `add_schedule` variable set to false
42+
to = mongodbatlas_cloud_backup_schedule.to_be_deleted # any resource name that doesn't exist works!
43+
}
44+
45+
removed {
46+
from = mongodbatlas_cloud_backup_schedule.to_be_deleted # any resource name that doesn't exist works!
47+
48+
lifecycle {
49+
destroy = false
50+
}
51+
}
52+
```
53+
54+
Then when you run `terraform apply`, you should see:
55+
56+
```bash
57+
[...]
58+
mongodbatlas_cloud_backup_schedule.to_be_deleted will no longer be managed by Terraform, but will not be destroyed
59+
(destroy = false is set in the configuration)
60+
(moved from module.cluster_with_schedule.mongodbatlas_cloud_backup_schedule.this[0])
61+
[...]
62+
Plan: 0 to add, 0 to change, 0 to destroy.
63+
```
64+
65+
Reply `yes` to confirm the state removal of `mongodbatlas_cloud_backup_schedule`.
66+
67+
Then run `terraform destroy` to destroy the cluster defined in `module.cluster_with_schedule.mongodbatlas_advanced_cluster.this`.
68+
See [Backup Compliance Policy Deletion Consideration](../resource/README.md#backup-compliance-policy-deletion-consideration) for details on the `mongodbatlas_backup_compliance_policy` deletion.
69+
70+
## FAQ
71+
I get a `Removed Resource still exists error` when running `terraform apply`, how do I fix it?
72+
73+
This error happens because the configuration still has the `mongodbatlas_cloud_backup_schedule` active.
74+
When using a module, together with the `removed` block, remember to add the `moved` block and set `add_schedule=false` on the `cluster_with_schedule` module.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
data "mongodbatlas_atlas_user" "this" {
2+
user_id = var.user_id
3+
}
4+
5+
# Be aware that deleting the `mongodbatlas_backup_compliance_policy` resource requires contacting MongoDB Support.
6+
resource "mongodbatlas_backup_compliance_policy" "this" {
7+
project_id = var.project_id
8+
authorized_email = data.mongodbatlas_atlas_user.this.email_address
9+
authorized_user_first_name = data.mongodbatlas_atlas_user.this.first_name
10+
authorized_user_last_name = data.mongodbatlas_atlas_user.this.last_name
11+
copy_protection_enabled = false
12+
pit_enabled = false
13+
encryption_at_rest_enabled = false
14+
15+
restore_window_days = 7
16+
on_demand_policy_item {
17+
frequency_interval = 0
18+
retention_unit = "days"
19+
retention_value = 1
20+
}
21+
policy_item_daily {
22+
frequency_interval = 0
23+
retention_unit = "days"
24+
retention_value = 1
25+
}
26+
}
27+
28+
module "cluster_with_schedule" {
29+
source = "./modules/cluster_with_schedule"
30+
31+
project_id = var.project_id
32+
instance_size = var.instance_size
33+
cluster_name = var.cluster_name
34+
add_schedule = true # change to false in Step 2
35+
}
36+
37+
# Step 2: For removing the `mongodbatlas_cloud_backup_schedule` resource
38+
# Rename the resource to avoid the `Removed Resource still exists error`
39+
# moved {
40+
# from = module.cluster_with_schedule.mongodbatlas_cloud_backup_schedule.this[0] # must be deleted with the `add_schedule` variable set to false
41+
# to = mongodbatlas_cloud_backup_schedule.to_be_deleted # any resource name that doesn't exist works!
42+
# }
43+
44+
# removed {
45+
# from = mongodbatlas_cloud_backup_schedule.to_be_deleted # any resource name that doesn't exist works!
46+
47+
# lifecycle {
48+
# destroy = false
49+
# }
50+
# }
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
terraform {
2+
required_providers {
3+
mongodbatlas = {
4+
source = "mongodb/mongodbatlas"
5+
version = "~> 1.34"
6+
}
7+
}
8+
required_version = ">= 1.0"
9+
}
10+
11+
resource "mongodbatlas_advanced_cluster" "this" {
12+
project_id = var.project_id
13+
name = var.cluster_name
14+
cluster_type = "REPLICASET"
15+
backup_enabled = true
16+
retain_backups_enabled = true
17+
18+
replication_specs = [
19+
{
20+
region_configs = [
21+
{
22+
provider_name = "AWS"
23+
region_name = "US_EAST_1"
24+
priority = 7
25+
electable_specs = {
26+
node_count = 3
27+
instance_size = var.instance_size
28+
disk_size_gb = 10
29+
}
30+
},
31+
]
32+
}
33+
]
34+
}
35+
resource "mongodbatlas_cloud_backup_schedule" "this" {
36+
count = var.add_schedule ? 1 : 0
37+
project_id = var.project_id
38+
cluster_name = mongodbatlas_advanced_cluster.this.name
39+
reference_hour_of_day = 19
40+
reference_minute_of_hour = 15
41+
restore_window_days = 7
42+
policy_item_daily {
43+
frequency_interval = 1
44+
retention_unit = "days"
45+
retention_value = 1
46+
}
47+
copy_settings {
48+
cloud_provider = "AWS"
49+
frequencies = ["HOURLY",
50+
"DAILY",
51+
"WEEKLY",
52+
"MONTHLY",
53+
"YEARLY",
54+
"ON_DEMAND"]
55+
region_name = "US_EAST_2"
56+
zone_id = mongodbatlas_advanced_cluster.this.replication_specs[0].zone_id
57+
should_copy_oplogs = false
58+
}
59+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "project_id" {
2+
type = string
3+
}
4+
5+
variable "cluster_name" {
6+
type = string
7+
}
8+
9+
variable "instance_size" {
10+
type = string
11+
}
12+
13+
variable "add_schedule" {
14+
type = bool
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "mongodbatlas" {
2+
public_key = var.public_key
3+
private_key = var.private_key
4+
}

0 commit comments

Comments
 (0)