This tutorial is part of the Bytebase Terraform Provider series:

What You’ll Learn

Create projects to organize your databases into logical groups.

Prerequisites

Before starting this tutorial, ensure you have:

  • Completed Parts 1 & 2: Environments and instances configured
  • Built-in instances: Test Sample Instance and Prod Sample Instance from sample data
  • Terraform workspace: From previous tutorials

Step 1 - Understand Current Setup

From previous tutorials, you have:

  • Test instance with hr_test and postgres databases
  • Prod instance with hr_prod and postgres databases

Currently, these hr_test and hr_prod databases are in the default Sample Project, while postgres databases are unassigned. You can view them in Bytebase by clicking Databases on the left sidebar. Let’s organize them into new projects.

Step 2 - Create Projects

Terraform resourcebytebase_project
Sample file3-projects.tf

Create 3-projects.tf:

3-projects.tf
# Project One - All test databases
resource "bytebase_project" "project_one" {
  depends_on  = [bytebase_instance.test]
  resource_id = "project-one"
  title       = "Project One"

  # Automatically include all databases from test instance
  databases = bytebase_instance.test.databases
}

# Project Two - Specific production database
resource "bytebase_project" "project_two" {
  depends_on  = [bytebase_instance.prod]
  resource_id = "project-two"
  title       = "Project Two"

  # Explicitly specify which database to include
  databases = [
    "instances/prod-sample-instance/databases/hr_prod"
  ]
}

Two Assignment Patterns

  1. Dynamic: databases = bytebase_instance.test.databases

    • Automatically includes all databases from an instance
    • Good for development/test environments
  2. Explicit: databases = ["instances/.../databases/..."]

    • Manually specify each database
    • Better control for production

Step 3 - Apply Configuration

terraform plan
terraform apply

Step 4 - Verify Projects

  1. Go to Projects in Bytebase.

  2. You’ll see three projects:

    • Sample Project (original, now empty)

    • Project One (contains hr_test and postgres which are both from Test Sample Instance)

      project-1

    • Project Two (contains hr_prod which is from Prod Sample Instance)

      project-2

Summary and Next Steps

You’ve successfully organized databases into projects! Key takeaways:

  • Projects group related databases for better organization
  • Two assignment patterns: dynamic (all databases) or explicit (specific databases)
  • Projects span environments: can include both test and prod databases

Part 5: Manage General Settings with Terraform