Provision a Database via Terraform
A clear, step-by-step walkthrough for automating MongoDB database creation on 123cluster with Terraform and the Mastercard/restapi provider, demonstrating how to grab the REST API curl command from the UI, break down its headers and JSON body, translate each element into Terraform variables and resources, and embed the flow into a reusable CI/CD process.
Step 1: Copy the curl
Command from the UI
On the Create a new database (MongoDB) page, 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_name": "<DATABASE_NAME>",
"node_id": "<MONGO_HOST_ADDRESS>:<MONGO_HOST_PORT>",
"auto_delete": false,
"auto_delete_days": 1,
"rest_api": true
}' \
<API_BASE_URL>/add_mongo_database/
Step 2: Parse the curl
Command
- Authorization header
Extract the JWT afterBearer
. - Content-Type & Accept
Keep both asapplication/json
. - Payload fields
db_name
node_id
auto_delete
auto_delete_days
rest_api
- Endpoint
- Base URI:
<API_BASE_URL>
- Resource path:
/add_mongo_database/
- Base URI:
Step 3: Translate into Terraform
- Create directory & file
integration/
└── terraform/
└── create_database/
└── main.tf
- Provider block
// Terraform configuration for provisioning a MongoDB database on 123cluster
terraform {
required_providers {
restapi = {
source = "Mastercard/restapi"
version = "1.19.1" // Lock provider version for compatibility
}
}
}
/*
REST API provider configuration:
- uri: Base endpoint for 123cluster API.
- headers: Authentication and content type requirements.
- write_returns_object: Returns API responses as objects for easy access.
- debug: Enables verbose logging for troubleshooting.
- HTTP methods: POST for all resource operations.
*/
provider "restapi" {
uri = "<API_BASE_URL>"
write_returns_object = true
debug = true
headers = {
Authorization = "Bearer <YOUR_JWT_TOKEN>" // Use a secure, valid JWT token for API access
Content-Type = "application/json"
Accept = "application/json"
}
create_method = "POST"
update_method = "POST"
destroy_method = "POST"
}
- Variable declarations
/*
These variables make your Terraform code reusable and adaptable for any environment or pipeline.
Always use variables for host, port, and resource names to avoid hard-coding and simplify automation.
*/
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 "database_name" {
description = "Name of the new MongoDB database"
type = string
default = "<DATABASE_NAME>"
}
- Resource definition
/*
This resource sends a POST request to create a new MongoDB database.
- All fields are mapped to variables for portability and CI/CD use.
- node_id combines host address and port, as required by the API.
- Set auto_delete to false for safety; adjust as needed for your workflow.
*/
resource "restapi_object" "create_database" {
path = "/add_mongo_database/"
data = jsonencode({
db_name = var.database_name
node_id = "${var.mongo_host_address}:${var.mongo_host_port}" // API expects "host:port" format
auto_delete = false
auto_delete_days = 1
rest_api = true
})
}
- Output block
/*
Output the full API JSON response after database creation.
- Useful for CI/CD logs, debugging, or as an input for follow-up steps.
*/
output "database_response" {
description = "Raw JSON response from add_mongo_database"
value = restapi_object.create_database.data
}
Step 4: Initialize & Apply
cd integration/terraform/create_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 database_response
Additional Guidance & Best Practices
- Parameterization: By exposing all user-specific and environment-specific values as Terraform variables, the configuration is portable and reusable across multiple clusters and environments.
- Security: Store sensitive values (passwords, private keys, tokens) securely using environment variables, secret managers, or Terraform's sensitive variable types.
- CI/CD Integration: Integrate these steps into a CI/CD workflow for fully automated and consistent database provisioning.
- API Versioning: Always verify endpoint and payload requirements with the latest 123cluster API documentation to ensure compatibility.
- Logging & Outputs: Use Terraform outputs to capture and log API responses, enabling audit trails and integration with other automation steps.
- Environment Isolation: Utilize Terraform workspaces to separate deployments for development, staging, and production environments.
- Error Handling: Check and handle API response errors in your CI/CD process to ensure robust automation and alerting.