Provision an Export Database via Terraform
This section provides a step-by-step guide to automating the export (dump) of a MongoDB node on 123cluster using Terraform and the Mastercard/restapi provider. You will learn how to convert a copied REST API command into a parameterized, reusable Terraform workflow suitable for integration with CI/CD pipelines.
Step 1: Copy the curl
Command from the UI
On the Export (Dump) Database page for your MongoDB node, 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 '{
"node_id": "<MONGO_HOST_ADDRESS>:<MONGO_HOST_PORT>",
"schedule": false,
"compress": false,
"backup_path": "<BACKUP_PATH>",
"keep_days": 3,
"rest_api": true
}' \
<API_BASE_URL>/backup_mongo_node/
Step 2: Parse the curl Command
- Authorization header:
Extract the JWT token afterBearer
. - Content-Type & Accept:
Both must beapplication/json
. - Payload fields:
node_id
(string, format:<host>:<port>
)type
(string, e.g.,"MONGO"
)cluster_id
(string, unique cluster identifier)schedule
(boolean, if export is scheduled)compress
(boolean, whether to compress export)include_data
(boolean, include actual data or only schema)export_path
(string, path to save export)keep_days
(integer, days to retain export files)rest_api
(boolean)
- Endpoint:
- Base URI:
<API_BASE_URL>
- Resource path:
/dump_mongo_node/
- Base URI:
Step 3: Translate into Terraform
- Create directory & file:
integration/
└── terraform/
└── export_database/
└── main.tf
- Provider block
// Terraform configuration for automating MongoDB node export (dump) 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 environment- and user-specific values.
*/
variable "mongo_host_address" {
description = "Address (IP or FQDN) of the MongoDB host"
type = string
default = "<MONGO_HOST_ADDRESS>"
}
variable "mongo_host_port" {
description = "Port on which MongoDB is listening"
type = string
default = "<MONGO_HOST_PORT>"
}
variable "cluster_id" {
description = "Cluster identifier for the MongoDB cluster"
type = string
default = "<CLUSTER_ID>"
}
variable "export_path" {
description = "Directory on server to store export (dump) files"
type = string
default = "/backups/mongo/exports"
}
variable "schedule" {
description = "Whether to schedule the export"
type = bool
default = false
}
variable "compress" {
description = "Whether to compress the export file"
type = bool
default = false
}
variable "include_data" {
description = "Include actual database data in export (not just schema)"
type = bool
default = true
}
variable "keep_days" {
description = "Number of days to keep the exported files"
type = number
default = 2
}
- Resource definition
/*
Resource for triggering MongoDB node export (dump) via REST API.
All fields are parameterized for portability and automation.
*/
resource "restapi_object" "export_database" {
path = "/dump_mongo_node/"
data = jsonencode({
node_id = "${var.mongo_host_address}:${var.mongo_host_port}"
type = "MONGO"
cluster_id = var.cluster_id
schedule = var.schedule
compress = var.compress
include_data = var.include_data
export_path = var.export_path
keep_days = var.keep_days
rest_api = true
})
}
- Output block
/*
Output the full API JSON response after triggering the export.
Use for logs, CI/CD visibility, or follow-up automation.
*/
output "export_response" {
description = "Raw JSON response from dump_mongo_node"
value = restapi_object.export_database.data
}
Step 4: Initialize & Apply
cd integration/terraform/export_database
# 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 export_response
Additional Guidance & Best Practices
- Parameterization: Use Terraform variables for all hostnames, cluster IDs, export paths, and schedule options to enable repeatable, environment-agnostic workflows.
- Security: Store JWT tokens and sensitive data securely (environment variables, secret managers, or Terraform’s sensitive type).
- CI/CD Integration: Automate database exports via your CI/CD pipeline for reliable, versioned, and auditable dumps.
- API Versioning: Check 123cluster API docs regularly to ensure you use the latest compatible endpoints and payloads.
- Logging & Outputs: Capture export job results with Terraform outputs for audit trails and automated follow-up actions (like backup integrity checks or notifications).
- Environment Isolation: Use workspaces for separate development, staging, and production export runs.
- Error Handling: Validate API responses in your automation process and implement robust alerting for export failures.