Skip to content

Commit 7b0646a

Browse files
authored
doc: Add example for mongodbatlas_cloud_backup_schedule to create policies for multiple clusters (#1403)
1 parent 819be8e commit 7b0646a

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Example - MongoDB Atlas Cloud Backup Schedule for setting up policies for multiple clusters
2+
3+
This project aims to provide an example of using [Cloud Backup Schedule in Atlas](https://docs.atlas.mongodb.com/reference/api/cloud-backup/schedule/modify-one-schedule/).
4+
5+
6+
## Dependencies
7+
8+
* Terraform MongoDB Atlas Provider v1.10.0
9+
* A MongoDB Atlas account
10+
11+
```
12+
Terraform v1.10.0
13+
+ provider registry.terraform.io/terraform-providers/mongodbatlas v1.10.0
14+
```
15+
16+
## Usage
17+
18+
**1\. Ensure your MongoDB Atlas credentials are set up.**
19+
20+
This can be done using environment variables:
21+
22+
```bash
23+
export MONGODB_ATLAS_PUBLIC_KEY="xxxx"
24+
export MONGODB_ATLAS_PRIVATE_KEY="xxxx"
25+
```
26+
27+
... or follow as in the `variables.tf` file and create **terraform.tfvars** file with all the variable values, ex:
28+
```
29+
public_key = "<MONGODB_ATLAS_PUBLIC_KEY>"
30+
private_key = "<MONGODB_ATLAS_PRIVATE_KEY>"
31+
```
32+
**2\. Update required variables.**
33+
Now create/update **terraform.tfvars** file with all the variable values and make sure **not to commit it**. For this example, you just need to provide `org_id` and a `project_name`.
34+
35+
**3\. Review the Terraform plan.**
36+
37+
Execute the below command and ensure you are happy with the plan.
38+
39+
``` bash
40+
$ terraform plan
41+
```
42+
This project currently supports the below deployments:
43+
44+
- MongoDB Atlas Project
45+
- MongoDB Atlas Clusters (3 AWS M10 clusters in various regions)
46+
- MongoDB Cloud Backup Schedule(s) with various policies which is set up for each created cluster.
47+
48+
**5\. Execute the Terraform apply.**
49+
50+
Now execute the plan to provision the Federated settings resources.
51+
52+
``` bash
53+
$ terraform apply
54+
```
55+
56+
**6\. Destroy the resources.**
57+
58+
Once you are finished your testing, ensure you destroy the resources to avoid unnecessary Atlas charges.
59+
60+
``` bash
61+
$ terraform destroy
62+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
locals {
2+
atlas_clusters = {
3+
"cluster_1" = { name = "m10-aws-1e", region = "US_EAST_1" },
4+
"cluster_2" = { name = "m10-aws-2e", region = "US_EAST_2" },
5+
"cluster_3" = { name = "m10-aws-3w", region = "US_WEST_1" }
6+
}
7+
}
8+
9+
resource "mongodbatlas_project" "atlas-project" {
10+
org_id = var.org_id
11+
name = var.project_name
12+
}
13+
14+
resource "mongodbatlas_advanced_cluster" "automated_backup_test_cluster" {
15+
for_each = local.atlas_clusters
16+
project_id = mongodbatlas_project.atlas-project.id
17+
name = each.value.name
18+
cluster_type = "REPLICASET"
19+
20+
replication_specs {
21+
num_shards = 1
22+
23+
region_configs {
24+
electable_specs {
25+
instance_size = "M10"
26+
node_count = 3
27+
}
28+
analytics_specs {
29+
instance_size = "M10"
30+
node_count = 1
31+
}
32+
33+
provider_name = "AWS"
34+
region_name = each.value.region
35+
priority = 7
36+
}
37+
}
38+
39+
backup_enabled = true # enable cloud backup snapshots
40+
}
41+
42+
resource "mongodbatlas_cloud_backup_schedule" "test" {
43+
for_each = local.atlas_clusters
44+
project_id = mongodbatlas_project.atlas-project.id
45+
cluster_name = mongodbatlas_advanced_cluster.automated_backup_test_cluster[each.key].name
46+
47+
reference_hour_of_day = 3
48+
reference_minute_of_hour = 45
49+
restore_window_days = 4
50+
51+
policy_item_hourly {
52+
frequency_interval = 1 #accepted values = 1, 2, 4, 6, 8, 12 -> every n hours
53+
retention_unit = "days"
54+
retention_value = 1
55+
}
56+
policy_item_daily {
57+
frequency_interval = 1 #accepted values = 1 -> every 1 day
58+
retention_unit = "days"
59+
retention_value = 2
60+
}
61+
policy_item_weekly {
62+
frequency_interval = 4 # accepted values = 1 to 7 -> every 1=Monday,2=Tuesday,3=Wednesday,4=Thursday,5=Friday,6=Saturday,7=Sunday day of the week
63+
retention_unit = "weeks"
64+
retention_value = 3
65+
}
66+
policy_item_monthly {
67+
frequency_interval = 5 # accepted values = 1 to 28 -> 1 to 28 every nth day of the month
68+
# accepted values = 40 -> every last day of the month
69+
retention_unit = "months"
70+
retention_value = 4
71+
}
72+
73+
depends_on = [
74+
mongodbatlas_advanced_cluster.automated_backup_test_cluster
75+
]
76+
}
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
variable "public_key" {
2+
description = "Public API key to authenticate to Atlas"
3+
type = string
4+
}
5+
variable "private_key" {
6+
description = "Private API key to authenticate to Atlas"
7+
type = string
8+
}
9+
variable "project_name" {
10+
description = "Atlas project name"
11+
type = string
12+
}
13+
variable "org_id" {
14+
description = "Atlas organization ID"
15+
type = string
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_providers {
3+
mongodbatlas = {
4+
source = "mongodb/mongodbatlas"
5+
version = "~> 1.0"
6+
}
7+
}
8+
required_version = ">= 0.13"
9+
}

0 commit comments

Comments
 (0)