Provision a Drop Database via Terraform
This section provides a step-by-step guide for automating the deletion (drop) of a MongoDB database on 123cluster using Terraform and the Mastercard/restapi provider. You’ll learn how to parameterize the REST API call for repeatable, secure, and auditable automation within your CI/CD pipeline.
Step 1: Copy the curl
Command from the UI
On the Delete Database page for your MongoDB database, 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:
db_id
(string, database name/ID)node_id
(string, format:<host>:<port>
)type
(string,"MONGO"
)rest_api
(boolean)
- Endpoint:
- Base URI:
<API_BASE_URL>
- Resource path:
/drop_mongo_database/
- Base URI:
Step 3: Translate into Terraform
- Create directory & file:
integration/
└── terraform/
└── drop_database/
└── main.tf
- Provider block
// Terraform configuration for automating MongoDB database 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 environment- and user-specific values.
*/
variable "database_name" {
description = "Name/ID of the MongoDB database to be dropped"
type = string
default = "<DATABASE_NAME>"
}
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>"
}
- Resource definition
/*
Resource for removing (dropping) a MongoDB database via REST API.
- All fields are parameterized for portability and automation.
*/
resource "restapi_object" "drop_database" {
path = "/drop_mongo_database/"
data = jsonencode({
db_id = var.database_name
node_id = "${var.mongo_host_address}:${var.mongo_host_port}"
type = "MONGO"
rest_api = true
})
}
- Output block
/*
Output the full API JSON response after database removal.
Use for logs, CI/CD tracking, or downstream automation.
*/
output "drop_database_response" {
description = "Raw JSON response from drop_mongo_database"
value = restapi_object.drop_database.data
}
Step 4: Initialize & Apply
cd integration/terraform/drop_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 drop_database_response
Additional Guidance & Best Practices
- Parameterization: Always use variables for database name, node address, and port to ensure safe, reusable automation across environments.
- Security: Store sensitive API tokens using environment variables, secret managers, or the
sensitive
attribute in Terraform. - CI/CD Integration: Include this workflow in your CI/CD pipeline to maintain a secure, auditable, and fully automated deletion process.
- API Versioning: Stay up to date with the 123cluster API documentation to ensure endpoints and payloads remain compatible.
- Logging & Outputs: Capture API responses as Terraform outputs for compliance, audit trails, and automation triggers.
- Environment Isolation: Use Terraform workspaces to prevent accidental deletion across different environments (dev, staging, prod).
- Error Handling: Validate and handle API errors in your automation, alerting on failures to protect against data loss.
- Confirmation: For production environments, implement manual or multi-step confirmation for deletion actions to avoid mistakes.