Manage Databases with Terraform
Originally posted in https://docs.bytebase.com/tutorials/manage-databases-with-terraform
Hi and welcome to Database DevOps Academy #98! We share Database DevOps insights and best practices for modern engineering organizations weekly. 🤹♀️
In Issue #98, we tackle a core step in Database DevOps: managing your database instances with Terraform. Bytebase makes it easy to register and control instances as code — setting the stage for a fully automated workflow.♟️
This tutorial is part of the Bytebase Terraform Provider series:
What You’ll Learn
Register database instances in Bytebase using Terraform.
Prerequisites
Before starting this tutorial, ensure you have Completed Part 1:
Step 1 - Explore Built-in Instances
Bytebase sample data includes two sample PostgreSQL instances. Let’s explore them:
Step 2 - Register Instances with Terraform
Now let’s import these instances into Terraform management.
Create 2-instances.tf with the following configuration:
2-instances.tf
# Test Sample Instance - PostgreSQL on port 8083
resource "bytebase_instance" "test" {
depends_on = [bytebase_setting.environments]
resource_id = "test-sample-instance"
environment = "environments/test"
title = "Test Sample Instance"
engine = "POSTGRES"
# Assign instance license
activation = true
# Connection settings for the built-in test database
data_sources {
id = "admin-test"
type = "ADMIN"
host = "/tmp" # Unix socket for local connection
port = "8083"
username = "bbsample"
password = "" # Empty for local auth
}
}
# Production Sample Instance - PostgreSQL on port 8084
resource "bytebase_instance" "prod" {
depends_on = [bytebase_setting.environments]
resource_id = "prod-sample-instance"
environment = "environments/prod"
title = "Prod Sample Instance"
engine = "POSTGRES"
activation = true
data_sources {
id = "admin-prod"
type = "ADMIN"
host = "/tmp"
port = "8084"
username = "bbsample"
password = ""
}
}
Understanding the Configuration
Step 3 - Apply Configuration
Now apply the configuration:
terraform plan
terraform apply
Step 4 - Verify Setup
You can now modify instance properties through Terraform. For example, to rename an instance:
resource "bytebase_instance" "test" {
# ... other config ...
title = "Development Database" # Changed title
}