Provision a Copy Database via Terraform

Provision a Copy Database via Terraform

This section provides a step-by-step guide for automating the copying (restoring, cloning) of a MongoDB database on 123cluster using Terraform and the Mastercard/restapi provider. You’ll learn how to transform a REST API command into a robust, reusable, and auditable workflow for your CI/CD process.

Step 1: Copy the curl Command from the UI
On the Copy Database 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 '{
    "db_id":            "<DATABASE_NAME>",
    "node_id":          "<MONGO_HOST_ADDRESS>:<MONGO_HOST_PORT>",
    "source_id":        "<SOURCE_MONGO_HOST_ADDRESS>:<SOURCE_MONGO_HOST_PORT>",
    "option":           "copy",
    "source_type":      "MONGO",
    "target_type":      "MONGO",
    "target_cluster_id":"<TARGET_CLUSTER_ID>",
    "source_cluster_id":"<SOURCE_CLUSTER_ID>",
    "copy_option":      "one_time",
    "copy_name":        "<COPY_NAME>",
    "db_exists":        true,
    "rest_api":         true
  }' \
  <API_BASE_URL>/copy_mongo_database/

Step 2: Parse the curl Command

  • Authorization header:
    Extract the JWT token after Bearer.
  • Content-Type & Accept:
    Both must be application/json.
  • Payload fields:
    • db_id (string, name/ID of the database to copy)
    • node_id (string, target node <host>:<port>)
    • source_id (string, source node <host>:<port>)
    • option (string, "copy")
    • source_type (string, "MONGO")
    • target_type (string, "MONGO")
    • target_cluster_id (string, target cluster ID)
    • source_cluster_id (string, source cluster ID)
    • copy_option (string, e.g., "one_time")
    • copy_name (string, custom name for the copy job)
    • db_exists (boolean, does the target DB already exist)
    • rest_api (boolean)
  • Endpoint:
    • Base URI: <API_BASE_URL>
    • Resource path: /copy_mongo_database/

Step 3: Translate into Terraform

  • Create directory & file:
integration/
└── terraform/
    └── copy_database/
        └── main.tf
  • Provider block
// Terraform configuration for automating MongoDB database copy 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 database, node, cluster, and copy job properties.
*/
variable "database_name" {
  description = "Name/ID of the MongoDB database to be copied"
  type        = string
  default     = "<DATABASE_NAME>"
}

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 "source_mongo_host_address" {
  description = "Source MongoDB node address (IP or FQDN)"
  type        = string
  default     = "<SOURCE_MONGO_HOST_ADDRESS>"
}

variable "source_mongo_host_port" {
  description = "Source MongoDB node port"
  type        = string
  default     = "<SOURCE_MONGO_HOST_PORT>"
}

variable "target_cluster_id" {
  description = "Target MongoDB cluster ID"
  type        = string
  default     = "<TARGET_CLUSTER_ID>"
}

variable "source_cluster_id" {
  description = "Source MongoDB cluster ID"
  type        = string
  default     = "<SOURCE_CLUSTER_ID>"
}

variable "copy_option" {
  description = "Copy mode (e.g., one_time, scheduled)"
  type        = string
  default     = "one_time"
}

variable "copy_name" {
  description = "Custom name for the copy job"
  type        = string
  default     = "<COPY_NAME>"
}

variable "db_exists" {
  description = "Does the target database already exist"
  type        = bool
  default     = true
}
  • Resource definition
/*
  Resource for copying (cloning/restoring) a MongoDB database via REST API.
  All fields are parameterized for flexibility and automation.
*/
resource "restapi_object" "copy_database" {
  path = "/copy_mongo_database/"
  data = jsonencode({
    db_id             = var.database_name
    node_id           = "${var.mongo_host_address}:${var.mongo_host_port}"
    source_id         = "${var.source_mongo_host_address}:${var.source_mongo_host_port}"
    option            = "copy"
    source_type       = "MONGO"
    target_type       = "MONGO"
    target_cluster_id = var.target_cluster_id
    source_cluster_id = var.source_cluster_id
    copy_option       = var.copy_option
    copy_name         = var.copy_name
    db_exists         = var.db_exists
    rest_api          = true
  })
}
  • Output block
/*
  Output the full API JSON response after copying the database.
  Use for CI/CD logs, further automation, or monitoring.
*/
output "copy_database_response" {
  description = "Raw JSON response from copy_mongo_database"
  value       = restapi_object.copy_database.data
}

Step 4: Initialize & Apply

cd integration/terraform/copy_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 copy_database_response

Additional Guidance & Best Practices

  • Parameterization: Always use variables for all identifiers, nodes, clusters, and copy options to ensure repeatable and maintainable code.
  • Security: Store API tokens securely using environment variables, secret managers, or mark variables as sensitive in Terraform.
  • CI/CD Integration: Embed this process into your CI/CD pipelines for automated and consistent MongoDB database copy/restore workflows.
  • API Versioning: Regularly review 123cluster API documentation to verify payloads, endpoints, and supported copy options.
  • Logging & Outputs: Capture and review outputs for audit trails, error handling, and downstream workflow integration (such as notifications or follow-up scripts).
  • Environment Isolation: Use workspaces and environment variables to avoid cross-environment conflicts and ensure controlled testing and production execution.
  • Error Handling: Validate API responses and alert on any copy job failures or unexpected results.
  • Confirmation: For production, implement additional checks before copying/overwriting important databases.