tidbcloud_sql_user
リソースを使用する
このドキュメントでは、 tidbcloud_sql_user
リソースを使用してTiDB Cloud SQL ユーザーを管理する方法について説明します。
tidbcloud_sql_user
リソースの機能は次のとおりです。
- TiDB Cloud SQL ユーザーを作成します。
- TiDB Cloud SQL ユーザーを変更します。
- TiDB Cloud SQL ユーザーをインポートします。
- TiDB Cloud SQL ユーザーを削除します。
前提条件
- TiDB Cloud Terraform プロバイダーを入手する v0.4.0以降。
- TiDB Cloudクラスターを作成するには、次のいずれかのドキュメントを参照してください。
SQLユーザーを作成する
tidbcloud_sql_user
リソースを使用して SQL ユーザーを作成できます。
次の例は、TiDB Cloud SQL ユーザーを作成する方法を示しています。
SQL ユーザーのディレクトリを作成してそこに入ります。
sql_user.tf
ファイルを作成します。terraform { required_providers { tidbcloud = { source = "tidbcloud/tidbcloud" } } } provider "tidbcloud" { public_key = "your_public_key" private_key = "your_private_key" } resource "tidbcloud_sql_user" "example" { cluster_id = "your_cluster_id" user_name = "example_user" password = "example_password" builtin_role = "role_admin" }resource
ブロックを使用して、リソース タイプ、リソース名、リソースの詳細など、 TiDB Cloudのリソースを定義します。tidbcloud_sql_user
リソースを使用するには、リソース タイプをtidbcloud_sql_user
に設定します。- リソース名は必要に応じて定義できます。例:
example
。 - TiDB Cloud Starter またはTiDB Cloud Essential クラスターの SQL ユーザーの場合、
user_name
と組み込みロールrole_readonly
およびrole_readwrite
ユーザー プレフィックスで始まる必要があり、tidbcloud_serverless_cluster
データ ソースを実行することでユーザー プレフィックスを取得できます。 - SQL ユーザー指定情報を取得するには、
tidbcloud_sql_user
(リソース)参照してください。
terraform apply
コマンドを実行します。リソースを適用する場合はterraform apply --auto-approve
の使用は推奨されません。$ 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: # tidbcloud_sql_user.example will be created + resource "tidbcloud_sql_user" "example" { + auth_method = (known after apply) + builtin_role = "role_admin" + cluster_id = "10423692645600000000" + password = (sensitive value) + user_name = "example_user" } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:上記の結果では、Terraform によって実行プランが生成され、Terraform が実行するアクションが記述されます。
- 構成と状態の違いを確認できます。
apply
の結果も確認できます。新しいリソースが追加されますが、リソースは変更または破棄されません。known after apply
apply
後の対応する値が取得されることを示します。
計画の内容がすべて問題ない場合は、「
yes
と入力して続行します。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_sql_user.example: Creating... tidbcloud_sql_user.example: Creation complete after 2s Apply complete! Resources: 1 added, 0 changed, 0 destroyed.リソースの状態を確認するには、コマンド
terraform show
またはterraform state show tidbcloud_sql_user.${resource-name}
使用します。コマンド 1 は、すべてのリソースとデータソースの状態を表示します。$ terraform state show tidbcloud_sql_user.example # tidbcloud_sql_user.example: resource "tidbcloud_sql_user" "example" { builtin_role = "role_admin" cluster_id = "10423692645600000000" password = (sensitive value) user_name = "example_user" }
SQL ユーザーのパスワードまたはユーザー ロールを変更する
次のように、Terraform を使用して SQL ユーザーのパスワードまたはユーザー ロールを変更できます。
SQLユーザーを作成する実行するときに使用する
sql_user.tf
ファイルで、password
、builtin_role
、およびcustom_roles
(該当する場合) を変更します。例えば:
resource "tidbcloud_sql_user" "example" { cluster_id = 10423692645600000000 user_name = "example_user" password = "updated_example_password" builtin_role = "role_readonly" }terraform apply
コマンドを実行します。$ terraform apply tidbcloud_sql_user.example: Refreshing state... Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: ~ update in-place Terraform will perform the following actions: # tidbcloud_sql_user.example will be updated in-place ~ resource "tidbcloud_sql_user" "example" { + auth_method = (known after apply) ~ builtin_role = "role_admin" -> "role_readonly" ~ password = (sensitive value) # (2 unchanged attributes hidden) } Plan: 0 to add, 1 to change, 0 to destroy. 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上記の実行プランでは、パスワードと組み込みロールが変更されます。
計画の内容がすべて問題ない場合は、「
yes
と入力して続行します。Enter a value: yes tidbcloud_sql_user.example: Modifying... tidbcloud_sql_user.example: Modifications complete after 2s Apply complete! Resources: 0 added, 1 changed, 0 destroyed.terraform state show tidbcloud_sql_user.${resource-name}
使用して状態を確認します。$ terraform state show tidbcloud_sql_user.example # tidbcloud_sql_user.example: resource "tidbcloud_sql_user" "example" { builtin_role = "role_readonly" cluster_id = "10423692645600000000" password = (sensitive value) user_name = "example_user" }
builtin_role
role_readonly
に変更されます。5 password
センシティブな値であるため表示されません。
SQLユーザーをインポートする
Terraform で管理されていないTiDB Cloud SQL ユーザーの場合は、Terraform を使用してインポートすることで管理できます。
たとえば、Terraform によって作成されていない SQL ユーザーを次のようにインポートできます。
新しい
tidbcloud_sql_user
リソースのインポート ブロックを追加します。次のインポート ブロックを
.tf
ファイルに追加し、example
目的のリソース名に置き換え、${id}
cluster_id,user_name
の形式に置き換えます。import { to = tidbcloud_sql_user.example id = "${id}" }新しい構成ファイルを生成します。
インポート ブロックに従って、新しい
tidbcloud_sql_user
リソースの新しい構成ファイルを生成します。terraform plan -generate-config-out=generated.tf上記のコマンドでは、既存の
.tf
名を指定しないでください。指定した場合、Terraform はエラーを返します。その後、カレントディレクトリに
generated.tf
ファイルが作成され、そこにインポートされたリソースの設定が含まれます。ただし、必須の引数password
設定されていないため、プロバイダーはエラーをスローします。生成された設定ファイルで、引数password
の値をtidbcloud_sql_user
リソースに置き換えることができます。生成された構成を確認して適用します。
生成された構成ファイルを確認し、ニーズを満たしていることを確認してください。必要に応じて、このファイルの内容を任意の場所に移動することもできます。
次に、
terraform apply
を実行してインフラストラクチャをインポートします。適用後の出力例は次のとおりです。tidbcloud_sql_user.example: Importing... [id=10423692645600000000,example_user] tidbcloud_sql_user.example: Import complete [id=10423692645600000000,example_user] Apply complete! Resources: 1 imported, 0 added, 0 changed, 0 destroyed.
これで、インポートした SQL ユーザーを Terraform で管理できるようになりました。
SQLユーザーを削除する
SQL ユーザーを削除するには、 tidbcloud_sql_user
リソースの構成を削除し、 terraform apply
コマンドを使用してリソースを破棄します。
$ terraform apply
tidbcloud_sql_user.example: Refreshing state...
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# tidbcloud_sql_user.example will be destroyed
# (because tidbcloud_sql_user.example is not in configuration)
- resource "tidbcloud_sql_user" "example" {
- builtin_role = "role_readonly" -> null
- cluster_id = "10423692645600000000" -> null
- password = (sensitive value) -> null
- user_name = "example_user" -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.
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_sql_user.example: Destroying...
tidbcloud_sql_user.example: Destruction complete after 3s
Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
ここで、コマンドterraform show
実行すると、リソースがクリアされているため何も表示されません。
$ terraform show