This tutorial is part of the Manage Bytebase with Terraform series:
📚 Complete examples: GitHub - Bytebase Terraform Provider
Learn how to automate database infrastructure management by combining Bytebase with Terraform using the Terraform Bytebase Provider.
In this tutorial, you’ll discover how to:
While Bytebase provides an intuitive GUI for database management, Terraform brings several key advantages for larger deployments:
Bytebase Terraform Provider handles control plane configuration such as settings, policies, access controls. It does not handle data plane operations such as database creation, schema migrations, DML execution, query.
Before starting this tutorial, ensure you have:
For macOS (using Homebrew):
For other platforms: Follow the official Terraform installation guide.
Start Bytebase:
Start MySQL instances for Test and Prod environments:
Set up Bytebase admin account:
Navigate to Bytebase (usually http://localhost:8080
) and register an admin account with Workspace Admin
role.
Before automating with Terraform, let’s set up a basic instance through the GUI to understand the manual process:
Add a database instance:
Test
environment with credentials root
/testpwd1
Create a project and database:
Test
demo
This manual process works fine for a few instances, but imagine doing this for dozens or hundreds of database instances across multiple environments!
You’ve added an instance for the Test
environment in Bytebase by clicking. What if you need to add hundreds of instances. In this section, you’ll witness the process simplification Terraform brings.
Create a new folder learn-terraform-bytebase
and create a blank file main.tf
in it.
Go to https://registry.terraform.io/providers/bytebase/bytebase/latest/docs. Click Use Provider, copy and paste the whole code block in the gray box into main.tf
. Pay attention to the version.
Copy the following provider part and paste it in main.tf
.
Go to IAM & Admin > Users & Groups, click + Add User.
Choose Service Account
as the Type, fill in the Email with tf@service.bytebase.com
, choose Workspace DBA
as Roles, and click Confirm.
Copy the Service Key for later use.
Paste the Service Key, Service Account Email, and URL into main.tf
.
Paste the following queries after the provider block and save the file. What it does is to list all existing environments and instances and print those out in the terminal.
Run terraform init
, terraform plan
and terraform apply
one by one in the terminal. You’ll see the output like this:
As we have two default environments in our Bytebase. Pay attention to resource_id
, they are test
and prod
.
As we can see, it’s the instance we just added. Follow "title" = "MySQL test"
, you’ll find "resource_id" = "mysql-test"
.
Before creating instances, let’s properly configure the environments. There are two approaches to achieve this - you typically only need one of them.
This approach uses environment settings and is usually sufficient for most use cases.
Remove the #List all environment
and #List all instances
blocks, and add the following environment configuration:
Alternatively, you can create environments using individual bytebase_environment
resources. This approach provides more granular control but requires careful dependency management.
Important: When using multiple bytebase_environment
resources, you must use depends_on
between environments. This ensures Terraform updates them in the correct order, as the Bytebase API only supports updating one environment list at a time.
Add the following environment resources after the settings configuration:
Finally, let’s add the database instances that will be associated with our environments. The configuration depends on which approach you chose in Step 4:
Key Points:
bytebase_setting.environments
and use environment references like "environments/${local.environment_id_test}"
bytebase_environment.test.name
Run terraform plan
and terraform apply
one by one in the terminal. You will see this in the terminal.
Go back to Bytebase, and click Environments. There is nothing changed with these two environments.
Click Instances on the left sidebar, and you will see the two instances we just added.
Click into one instance, scroll down and click Test Connection. It should be successful.
Now you have learned how to use Terraform to manage your MySQL database environments and instances in Bytebase, for PostgreSQL, you can futher declare database roles. Please check more example usage in GitHub.
Continue to the next tutorial: Manage Projects with Terraform where you’ll learn how to organize your databases into projects for better management.
If you encounter any problems while trying, welcome to our discord channel.
This tutorial is part of the Manage Bytebase with Terraform series:
📚 Complete examples: GitHub - Bytebase Terraform Provider
Learn how to automate database infrastructure management by combining Bytebase with Terraform using the Terraform Bytebase Provider.
In this tutorial, you’ll discover how to:
While Bytebase provides an intuitive GUI for database management, Terraform brings several key advantages for larger deployments:
Bytebase Terraform Provider handles control plane configuration such as settings, policies, access controls. It does not handle data plane operations such as database creation, schema migrations, DML execution, query.
Before starting this tutorial, ensure you have:
For macOS (using Homebrew):
For other platforms: Follow the official Terraform installation guide.
Start Bytebase:
Start MySQL instances for Test and Prod environments:
Set up Bytebase admin account:
Navigate to Bytebase (usually http://localhost:8080
) and register an admin account with Workspace Admin
role.
Before automating with Terraform, let’s set up a basic instance through the GUI to understand the manual process:
Add a database instance:
Test
environment with credentials root
/testpwd1
Create a project and database:
Test
demo
This manual process works fine for a few instances, but imagine doing this for dozens or hundreds of database instances across multiple environments!
You’ve added an instance for the Test
environment in Bytebase by clicking. What if you need to add hundreds of instances. In this section, you’ll witness the process simplification Terraform brings.
Create a new folder learn-terraform-bytebase
and create a blank file main.tf
in it.
Go to https://registry.terraform.io/providers/bytebase/bytebase/latest/docs. Click Use Provider, copy and paste the whole code block in the gray box into main.tf
. Pay attention to the version.
Copy the following provider part and paste it in main.tf
.
Go to IAM & Admin > Users & Groups, click + Add User.
Choose Service Account
as the Type, fill in the Email with tf@service.bytebase.com
, choose Workspace DBA
as Roles, and click Confirm.
Copy the Service Key for later use.
Paste the Service Key, Service Account Email, and URL into main.tf
.
Paste the following queries after the provider block and save the file. What it does is to list all existing environments and instances and print those out in the terminal.
Run terraform init
, terraform plan
and terraform apply
one by one in the terminal. You’ll see the output like this:
As we have two default environments in our Bytebase. Pay attention to resource_id
, they are test
and prod
.
As we can see, it’s the instance we just added. Follow "title" = "MySQL test"
, you’ll find "resource_id" = "mysql-test"
.
Before creating instances, let’s properly configure the environments. There are two approaches to achieve this - you typically only need one of them.
This approach uses environment settings and is usually sufficient for most use cases.
Remove the #List all environment
and #List all instances
blocks, and add the following environment configuration:
Alternatively, you can create environments using individual bytebase_environment
resources. This approach provides more granular control but requires careful dependency management.
Important: When using multiple bytebase_environment
resources, you must use depends_on
between environments. This ensures Terraform updates them in the correct order, as the Bytebase API only supports updating one environment list at a time.
Add the following environment resources after the settings configuration:
Finally, let’s add the database instances that will be associated with our environments. The configuration depends on which approach you chose in Step 4:
Key Points:
bytebase_setting.environments
and use environment references like "environments/${local.environment_id_test}"
bytebase_environment.test.name
Run terraform plan
and terraform apply
one by one in the terminal. You will see this in the terminal.
Go back to Bytebase, and click Environments. There is nothing changed with these two environments.
Click Instances on the left sidebar, and you will see the two instances we just added.
Click into one instance, scroll down and click Test Connection. It should be successful.
Now you have learned how to use Terraform to manage your MySQL database environments and instances in Bytebase, for PostgreSQL, you can futher declare database roles. Please check more example usage in GitHub.
Continue to the next tutorial: Manage Projects with Terraform where you’ll learn how to organize your databases into projects for better management.
If you encounter any problems while trying, welcome to our discord channel.