1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

terraformによるTiDB Cloud Starterクラスタの作成

Last updated at Posted at 2025-08-18

はじめに

つい先日のTiDB Cloudのアップデートにより、TiDB CloudのAPIの新バージョン v1beta1 が利用できるようになり、terraformのTiDBプロバイダもそれに対応して更新されています。新バージョンでは、APIを利用する権限がより細かく設定できるようになりました。

この記事ではTiDB Cloud APIキーの取得方法と、それを使ってStarter(旧 Serverless)クラスタを作成する歩法についてご紹介します。本文中で紹介するterraformスクリプトの全体は下記のGithubリポジトリにあります。

また、@sgykfjsmさんによるより詳細なblogもあるのでこちらも見ていただくと、変更の背景も含め理解できるかと思います。

TiDB Cloud ServerlessはTiDB Cloud Starterへと名称変更されました

TiDB Cloudで情報の取得

terraformで作成する前に、必要な情報をTiDB Cloudコンソールを使って取得します。

APIキーの作成

まずはAPIキーを作成しましょう。

TiDB CloudのWebコンソールにログインし、組織を選択し、API Keysメニューを選択します。下の図で組織は PingCAP_Japanの部分です。
image.png

次にAPIキーを作成します。従来はAPIキーは組織で共通でしたが、新バージョンではプロジェクト単位のAPIキーが作成できるようになっています。今回はこちらを利用します。

先ほど開いた、API Keys画面で、下記の操作を行います。

  1. By Projectを選択
  2. プロジェクトを選択。下図では、default project
  3. 右上の Create Project API Keyボタンを押す

image.png

作成するAPIキーに説明とRoleを付与します。説明は何でも良いです。Roleはクラスタの作成が必要なため、Project Ownerを選択します。

image.png

APIキーが作成されます。このPublic KeyとPrivate Keyをメモしておいてください。
image.png

project idの取得

クラスタを作成するプロジェクトの project id を取得します。TiDB Cloudコンソールから今回クラスタを作成するプロジェクトを開き、ブラウザのURLバーから、projectId=以下の文字列を取得してください。これがproject idになります。

image.png

TiDBプロバイダにはproject情報を提供するprojectsデータソースもあり、名前で検索してidを取得することも可能です。
しかし、全プロジェクト情報を取得するため、今回利用しているプロジェクトレベルのAPIキーでは権限エラーとなって利用できません。そのため手動でproject_idを取得しています。

これで必要な情報は揃いました。

terraformの実行

それでは、terraformの実行に移りましょう。terraformの準備については今回省略します。
terraformのサンプルスクリプトを適当なディレクトリにgit cloneしておきます。

git clone https://github.com/bohnen/tidb-example-terraform.git
cd tidb-example-terraform

今回詳細に説明してはいませんが、terraformが呼び出すAPIのバージョンが変わったことにともない、TiDBプロバイダの提供するリソースも新しくなっています。従来は tidbcloud_clusterリソースでStarter(Serverless)クラスタも作成できましたが、今後は tidbcloud_serverless_clusterを利用します

必要な変数の設定

まずは必要な変数を設定していきます。terraform.tfvars.exampleをコピーしてterraform.tfvarsファイルを作成し、必要な情報を設定します。先に取得したAPIキー(public_key,private_key)と、project idを記載します。

また、必要に応じてプロジェクト名やリージョンも更新してください。

# TiDB Cloud API Keys
# Get these from TiDB Cloud Console: https://tidbcloud.com/console/clusters
tidbcloud_public_key  = "your_public_key_here"
tidbcloud_private_key = "your_private_key_here"

# Find this in TiDB Cloud console URL or use API to list projects
project_id = "your_project_id_here"

# Cluster Configuration (optional - defaults are provided in variables.tf)
cluster_name           = "tidb-demo-serverless"
# cloud_provider         = "AWS"

# Region format: {cloud_provider}-{region_code}
# AWS Examples: aws-us-west-2, aws-ap-northeast-1, aws-eu-central-1
# GCP Examples: gcp-us-central1, gcp-europe-west1, gcp-asia-southeast1
region                 = "aws-ap-northeast-1"

spending_limit_monthly = 0  # $10 in cents

TiDB Cloudの無料プランを利用している方は、spending_limit_monthly を0に設定してください。

初期設定

terraform.tfvarsの設定が終わったら、プロバイダのダウンロードを行います。terraform init コマンドを実行してください。

terraform init

Initializing the backend...
Initializing provider plugins...
- Finding tidbcloud/tidbcloud versions matching "~> 0.4.3"...
- Installing tidbcloud/tidbcloud v0.4.3...
...略...

Terraform has been successfully initialized!

確認と実行

初期化できたら、terraform plan で確認し、問題なければ terraform apply します。

terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:
...略...

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

tidbcloud_serverless_cluster.serverless_cluster: Creating...
tidbcloud_serverless_cluster.serverless_cluster: Still creating... [00m10s elapsed]
tidbcloud_serverless_cluster.serverless_cluster: Creation complete after 14s

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

cluster_endpoints = <sensitive>
cluster_id = "xxx"
cluster_name = "tidb-demo-serverless"
cluster_status = "ACTIVE"
project_id = "xxx"
region = {
  "cloud_provider" = "aws"
  "display_name" = "Tokyo (ap-northeast-1)"
  "name" = "regions/aws-ap-northeast-1"
  "region_id" = "ap-northeast-1"
}

このような感じで、10秒ちょっとでクラスタが起動すると思います。
これで、TiDB Cloud Starterクラスタが作成できました。

後片付け

無事クラスタが作成できたら、terraform destroyを使ってクラスタの削除ができます。

おわりに

新API用のAPIキーを利用して、terraformからTiDB Cloud Starterのクラスタを作成しました。
新APIではプロジェクト単位に管理者/ビューワーの権限制御が可能となり、より制限された自動化が可能となります。是非ご活用ください。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?