Provision a Restore Backup via Terraform
his section provides a step-by-step guide for automating the restore of a MongoDB node from a backup on 123cluster using Terraform and the Mastercard/restapi provider. You’ll learn how to parameterize the REST API call for repeatable, safe, and CI/CD-ready restore workflows.
Step 1: Copy the curl
Command from the UI
On the Restore Backup page for your MongoDB instance, 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 '{
"bu_id": "<BACKUP_ID>",
"autostart": true,
"restore_option": "this",
"target_id": "<MONGO_HOST_ADDRESS>:<MONGO_HOST_PORT>",
"restore_time": null,
"rest_api": true
}' \
<API_BASE_URL>/restore_mongo_node/
Step 2: Parse the curl Command
- Authorization header:
Extract the JWT token afterBearer
. - Content-Type & Accept:
Both must beapplication/json
. - Payload fields:
bu_id
(string, unique backup identifier)autostart
(boolean, whether to start node after restore)restore_option
(string, typically"this"
)target_id
(string,<host>:<port>
of MongoDB node to restore to)restore_time
(nullable/timestamp, point-in-time restore;null
for latest/full backup)rest_api
(boolean)
- Endpoint:
- Base URI:
<API_BASE_URL>
- Resource path:
/restore_mongo_node/
- Base URI:
Step 3: Translate into Terraform
- Create directory & file:
integration/
└── terraform/
└── restore_backup/
└── main.tf
- Provider block
// Terraform configuration for automating MongoDB node restore from backup 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 backup IDs, target nodes, and restore options to ensure flexibility and automation.
*/
variable "backup_id" {
description = "Unique identifier of the MongoDB backup to restore from"
type = string
default = "<BACKUP_ID>"
}
variable "mongo_host_address" {
description = "Target MongoDB node address (IP or FQDN)"
type = string
default = "<MONGO_HOST_ADDRESS>"
}
variable "mongo_host_port" {
description = "Target MongoDB node port"
type = string
default = "<MONGO_HOST_PORT>"
}
variable "autostart" {
description = "Whether to start the MongoDB node after restore"
type = bool
default = true
}
variable "restore_option" {
description = "Restore option (typically 'this')"
type = string
default = "this"
}
variable "restore_time" {
description = "Point-in-time restore timestamp (null for latest/full backup)"
type = string
default = null
}
- Resource definition
/*
Resource for restoring a MongoDB node from backup via REST API.
All fields are parameterized for portability and automation.
*/
resource "restapi_object" "restore_node" {
path = "/restore_mongo_node/"
data = jsonencode({
bu_id = var.backup_id
autostart = var.autostart
restore_option = var.restore_option
target_id = "${var.mongo_host_address}:${var.mongo_host_port}"
restore_time = var.restore_time
rest_api = true
})
}
- Output block
/*
Output the full API JSON response after triggering the restore.
Use for logs, CI/CD tracking, or downstream automation.
*/
output "restore_node_response" {
description = "Raw JSON response from restore_mongo_node"
value = restapi_object.restore_node.data
}
Step 4: Initialize & Apply
cd integration/terraform/restore_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 restore_node_response
Additional Guidance & Best Practices
- Parameterization: Use variables for all backup IDs, target node addresses, and restore options to ensure safe, portable, and environment-agnostic automation.
- Security: Always store JWT tokens and sensitive data securely—use environment variables, secret managers, or mark variables as
sensitive
in Terraform. - CI/CD Integration: Integrate this workflow into your CI/CD pipelines for reliable, auditable, and automated restore operations.
- API Versioning: Regularly check the 123cluster API documentation to ensure compatibility with any changes to restore endpoints or payloads.
- Logging & Outputs: Capture and log API responses as outputs for compliance, monitoring, and integration with follow-up jobs (such as health checks or notifications).
- Point-in-Time Recovery: For production, document and test the use of the
restore_time
parameter for point-in-time recovery scenarios. - Environment Isolation: Use Terraform workspaces to avoid conflicts between different environments (dev, staging, prod).
- Error Handling: Implement checks for API errors and ensure your automation triggers alerts or retries on failures.
- Confirmation: Add manual approvals or reviews for destructive or production restores to minimize risk.