Provision a Drop Backup via Terraform
This section provides a step-by-step guide for automating the deletion (drop) of a MongoDB backup on 123cluster using Terraform and the Mastercard/restapi provider. You’ll see how to turn a REST API command into a safe, reusable, and auditable workflow—ready for CI/CD automation and compliance
Step 1: Copy the curl
Command from the UI
On the Delete Backup page for your MongoDB backup, click { REST API }
. You’ll copy a command such as:
curl -v \
-H "Authorization: Bearer <YOUR_JWT_TOKEN>" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{
"db_id": "<DATABASE_NAME>",
"node_id": "<MONGO_HOST_ADDRESS>:<MONGO_HOST_PORT>",
"type": "MONGO",
"rest_api": true
}' \
<API_BASE_URL>/drop_mongo_database/
Step 2: Parse the curl Command
- Authorization header:
Extract the JWT token afterBearer
. - Content-Type & Accept:
Both must beapplication/json
. - Payload fields:
backup_id
(string, unique identifier of the backup to delete)rest_api
(boolean)
- Endpoint:
- Base URI:
<API_BASE_URL>
- Resource path:
/drop_backup/
- Base URI:
Step 3: Translate into Terraform
- Create directory & file:
integration/
└── terraform/
└── drop_backup/
└── main.tf
- Provider block
// Terraform configuration for automating MongoDB backup deletion on 123cluster
terraform {
required_providers {
restapi = {
source = "Mastercard/restapi"
version = "1.19.1"
}
}
}
/*
REST API provider configuration:
- uri: Base API endpoint for 123cluster.
- headers: JWT and content type.
- write_returns_object: Output as object.
- debug: Enables logging for troubleshooting.
- HTTP methods: POST for all actions.
*/
provider "restapi" {
uri = "<API_BASE_URL>"
write_returns_object = true
debug = true
headers = {
Authorization = "Bearer <YOUR_JWT_TOKEN>" // Use a valid, secure JWT token
Content-Type = "application/json"
Accept = "application/json"
}
create_method = "POST"
update_method = "POST"
destroy_method = "POST"
}
- Variable declarations
/*
Use variables for all resource identifiers to maximize reusability and automation.
*/
variable "backup_id" {
description = "Unique identifier of the MongoDB backup to be dropped"
type = string
default = "<BACKUP_ID>"
}
- Resource definition
/*
Resource for deleting (dropping) a MongoDB backup via REST API.
All fields are parameterized for portability and automation.
*/
resource "restapi_object" "drop_backup" {
path = "/drop_backup/"
data = jsonencode({
backup_id = var.backup_id
rest_api = true
})
}
- Output block
/*
Output the full API JSON response after backup deletion.
Use for logs, CI/CD tracking, or downstream automation.
*/
output "drop_backup_response" {
description = "Raw JSON response from drop_backup"
value = restapi_object.drop_backup.data
}
Step 4: Initialize & Apply
cd integration/terraform/drop_backup
# Initialize the Terraform working directory and download necessary providers
terraform init
# Apply the configuration, review the planned actions, and confirm execution
terraform apply
# Output the API response for logging or integration with other tools
terraform output drop_backup_response
Additional Guidance & Best Practices
- Parameterization: Always use variables for backup IDs to allow safe and repeatable automation.
- Security: Protect API tokens and sensitive data with environment variables, secret managers, or Terraform’s
sensitive
attribute. - CI/CD Integration: Incorporate this Terraform workflow into your CI/CD pipeline for automated, auditable, and consistent backup management.
- API Versioning: Keep your configuration aligned with the latest 123cluster API documentation to avoid incompatibility.
- Logging & Outputs: Capture and log the deletion response for compliance and as triggers for downstream cleanup or notification.
- Environment Isolation: Use Terraform workspaces to safely separate test, staging, and production backup deletions.
- Error Handling: Validate API responses and ensure errors in backup deletion are reported or trigger alerts as needed.
- Safety: For production backups, consider adding manual confirmation or review steps to avoid accidental deletion of critical backups.