Provision Database DevOps
Learn how to provision Database DevOps using Terraform and the Harness API. This guide covers schema management, database instance provisioning, and schema change automation.
This guide walks you through provisioning Database DevOps using Terraform and the Harness API. The configuration enables seamless schema management, database instance provisioning, and automation of schema changes as part of your CI/CD workflows.
This guide walks you through provisioning Database DevOps using Terraform and the Harness Terraform Provider. The configuration enables seamless schema management, database instance provisioning, and automation of schema changes as part of your CI/CD workflows.
Before you begin
Before proceeding, ensure you have:
Terraform v1.3+ installed.
Access to a Harness account with Database DevOps Licence. Contact Harness Database DevOps Support if you need assistance.
Properly configured connectors for your databases (e.g., CockroachDB, PostgreSQL).
A valid Harness Platform API Key.
Step 1: Define a database schema
The harness_platform_db_schema resource allows you to define and manage a database schema.
resource "harness_platform_db_schema" "my_schema" {
identifier = "my_db_schema"
org_id = "default"
project_id = "default_project"
name = "My Database Schema"
tags = ["env:prod", "team:backend"]
schema_source {
connector = "cockroachDB" # database connector
location = "changelog.yaml" # schema changelog file
}
}In the above configuration:
schema_source.connector: Connector type (e.g., CockroachDB, PostgreSQL).schema_source.location: Location of the migration file (e.g., Liquibase/YAML changelog).tags: Useful for environment/team attribution.
This ensures schema migrations are version-controlled and can be applied consistently across environments.
Step 2: Provision a database instance
Next, use the harness_platform_db_instance resource to provision a database instance.
In this configuration, you specify:
schema: Binds the instance to the defined schema.branch: Ties schema changes to a Git branch (commonly main).connector: Database connector (PostgreSQL in this example).context: Logical environment (e.g., production).depends_on: Ensures schema creation before instance provisioning.
This setup allows you to manage database instances that automatically track schema changes.
Example Terraform configuration
The following example combines both resources into a complete, runnable configuration:
Step 3: Apply the configuration
To apply the configuration:
This will provision:
A database schema
my_db_schemawith versioned migrations.A database instance
my_db_instancebound to the schema.
This guide shows you how to create a database schema and then provision a database instance that tracks changes to the schema. This approach ensures your database objects are versioned and deployed consistently across environments.
Before you begin
Before you start, ensure you have:
A valid Harness account and API key. Go to Manage API keys to create an API key.
Organization (
orgIdentifier) and Project (projectIdentifier) IDs.A pre-configured DB Connector. You can create one via the Harness API as well or Harness UI. This connector allows Harness to connect to your database (e.g., PostgreSQL, MySQL, CockroachDB).
An HTTP client such as
curlor Postman.
In the cURL examples below, replace:
<org>with your organization identifier. You can find this in the URL when logged into Harness:https://app.harness.io/ng/org/<org>/...<project>with your project identifier. You can find this in the URL when logged into Harness:https://app.harness.io/ng/org/<org>/projects/<project>/...<account_id>with your Harness account ID. You can find this in the URL when logged into Harness:https://app.harness.io/ng/account/<account_id>/...<your_api_key>with your Harness API key. You can find this under Profile Overview.
Step 1: Create a database schema
Use the Harness Database DevOps API to define a schema that your instances will use.
Request
Endpoint:
Body Example
In the above request, we define a schema with the following parameters:
identifier: Unique identifier for the schema. Must be alphanumeric and can include_or$.name: Friendly display name of the schema.branch: Git branch where schema changes are tracked (e.g.,main).description: (Optional) Description for the schema.connector: The pre-configured database connector to use (e.g.,postgres-connector).tags: Optional key-value pairs to categorize schemas (e.g.,{"env":"prod"}).
Example curl request
Above command creates a new database schema, and you can now use it to provision instances. Below is the response:
Step 2: Create a database instance
After the schema is ready, let us provision an instance for it.
Request
Endpoint:
Body Example
In this request, we specify:
identifier: Unique identifier for the database instance.name: Friendly name for the instance.connector: The database connector to use (e.g.,pgfor PostgreSQL).branch: Git branch to track schema changes (e.g.,main).context: Logical environment label (e.g.,dev,prod).substituteProperties: Key-value pairs for any schema property substitutions (e.g., database name).tags: Optional key-value pairs to categorize instances (e.g.,{"env":"dev"}).
Example curl request
When you run the above command, you will get the following response:
Things to note
Schema first: The changelog is mandatory for Repository-type schemas.
Instance second: Instances reference an existing schema.
Branch: Both schema and instance must point to the correct Git branch.
Connector: Must match the DB type (Postgres, MySQL, etc.).
When you need to onboard hundreds or thousands of database instances, manual resource creation through the UI or individual Terraform configurations becomes impractical. The Database DevOps Bulk Onboarding Terraform module streamlines this process: it provisions JDBC connectors, database instances, and schemas directly from CSV files.
Before you begin
Before you begin, ensure you have:
Terraform v1.3 or later installed.
Harness provider version 0.30.0 or later. Go to Harness Terraform provider documentation to configure the provider.
A valid Harness Platform API Key.
Active Database DevOps license. Go to Subscription Overview and Management to view your subscriptions.
Prepared CSV files containing your database details (see format below).
Step 1: Prepare your CSV files
The module requires two CSV files: one for JDBC connectors and database instances, and another for database schemas.
JDBC connector and instance CSV
This CSV file creates both a JDBC connector and a database instance per row, sharing the same identifier and name.
Create a file named jdbc_connector_and_instance.csv with these columns:
identifier
yes
Unique identifier for both the connector and instance.
name
yes
Display name for both resources.
org_id
yes
Harness organization identifier.
project_id
yes
Harness project identifier.
url
yes
JDBC connection URL (for example, jdbc:postgresql://host:5432/db).
description
no
Description for both connector and instance.
delegate_selectors
no
Delegate tags, separated by pipes (tag1|tag2).
tags
no
Tags as comma-separated key:value pairs.
credentials_json
yes
JSON for the credentials block. Wrap in double quotes and escape internal quotes as "".
schema_identifier
yes
Must match the identifier of a row in the schema CSV.
branch
no
Git branch for the changelog.
context
no
Liquibase context (optional filter).
substitute_properties_json
no
JSON object for Liquibase property substitution or Flyway placeholder replacement.
Example row:
Database schema CSV
This CSV file creates database schema definitions that instances reference.
Create a file named db_schemas.csv with these columns:
identifier
yes
Unique identifier for the schema (referenced by schema_identifier in the connector CSV).
name
yes
Display name.
org_id
yes
Harness organization identifier.
project_id
yes
Harness project identifier.
description
no
Optional description.
service
no
Associated service identifier.
type
no
Schema type: Repository or Script (title case).
migration_type
no
Migration tool: Liquibase or Flyway.
use_percona
no
Set to true or false for Percona toolkit usage.
tags
no
Comma-separated key:value tags.
schema_source_json
conditional
JSON for repository-based schemas. Includes connector (Git connector ID), location, optional repo, archive_path, toml.
changelog_script_json
conditional
JSON for script-based schemas.
Example row for a repository-based schema:
Step 2: Configure the Terraform module
Create a new directory for your Terraform configuration:
Create a main.tf file that references the module:
Create a variables.tf file:
Create a terraform.tfvars file with your credentials:
Place your jdbc_connector_and_instance.csv and db_schemas.csv files in the same directory.
Step 3: Apply the Terraform configuration
Initialize Terraform to download the required providers and module:
Review the planned changes:
The output displays how many connectors, instances, and schemas will be created based on your CSV row counts.
Apply the configuration to create all resources:
Type yes when prompted to confirm the creation.
Step 4: Verify the resources
After Terraform completes, verify the resources were created:
In Harness, select Database DevOps from the module picker.
Navigate to DB Schemas to see your newly created schemas.
Navigate to DB Instances to see your database instances.
Navigate to Connectors under Project Setup to see your JDBC connectors.
Troubleshooting
Additional resources
The following resources provide further context for the bulk onboarding Terraform module.
Go to terraform-harness-database-devops-onboarding repository to view the module source code and examples.
Go to Set up connectors to understand JDBC connector configuration.
Go to Onboarding guide to learn about the Database DevOps workflow after onboarding.
Next steps
Go to Create a pipeline in Database DevOps to build the pipeline that runs your migrations.
Go to Secure database connectivity with SSL to enable encrypted connections for your database.
Go to Set up connectors to configure JDBC connectors for your database instances.
Last updated
Was this helpful?