Terraform は、コードとしてのインフラストラクチャ (IaC) をプロビジョニングおよび管理するオープンソースツールです。Tablestore は Terraform と統合されており、設定ファイルを通じて Tablestore インスタンスを作成および管理できます。
サンプルコードをワンクリックで実行できます。今すぐ実行
前提条件
-
セキュリティ上、最小限の権限を持つ RAM ユーザーを使用します (RAM ユーザーの作成、RAM ユーザーへの権限付与)。必要な最小限のポリシー:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "ots:GetInstance", "ots:BindInstance2Vpc", "ots:ListVpcInfoByInstance", "ots:UnbindInstance2Vpc", "ots:DeleteInstance", "ots:InsertInstance", "ots:UpdateInstance", "ots:ListInstance" ], "Resource": "*" } ] } -
次のいずれかの方法で Terraform 環境を準備します。
-
Explorer:オンライン Terraform 環境。インストール不要。無料。
-
Terraform を使用したリソースの迅速な作成:Cloud Shell には Terraform が事前インストールされ、認証情報も事前設定済みです。セットアップなしで直接コマンドを実行できます。
-
Terraform のインストールと設定:制限されたネットワーク環境やカスタム開発環境に最適です。
-
使用するリソース
-
alicloud_ots_instance:Tablestore インスタンス。
Tablestore インスタンスの作成
-
main.tf という名前のファイルを含む作業ディレクトリを作成し、次のコードを main.tf に追加します。
variable "name" { default = "tf-example" } variable "region" { default = "cn-hangzhou" } provider "alicloud" { region = var.region } resource "random_integer" "default" { min = 10000 max = 99999 } # Tablestore インスタンス resource "alicloud_ots_instance" "default" { name = "${var.name}-${random_integer.default.result}" # インスタンス名 description = var.name # インスタンスの説明 accessed_by = "Vpc" # インスタンスアクセス用のネットワークタイプ tags = { # インスタンスタグ Created = "TF" For = "Building table" } } -
Terraform を初期化します。
terraform init成功時の出力例:
Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. -
設定を適用します。
terraform applyプロンプトが表示されたら
yesと入力し、Enter キーを押します。成功時の出力例:You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. 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 Apply complete! Resources: 2 added, 0 changed, 0 destroyed. -
結果を確認します。
Terraform show
次のコマンドを実行して、作成されたリソースを表示します。
terraform showshell@Alicloud:~/ots$ terraform show # alicloud_ots_instance.default: resource "alicloud_ots_instance" "default" { accessed_by = "Vpc" description = "tf-example" id = "tf-example-xxx" instance_type = "HighPerformance" name = "tf-example-xxx" network_source_acl = [] network_type_acl = [ "VPC", ] resource_group_id = "rg-xxx" tags = { "Created" = "TF" "For" = "Building table" } } # random_integer.default: resource "random_integer" "default" { id = "xxx" max = 99999 min = 10000 result = xxx } shell@Alicloud:~/ots$Tablestore コンソール
Tablestore コンソールにログインします。 ページで、インスタンスが表示されていることを確認します。
リソースのリリース
不要になったリソースを削除するには、terraform destroy を実行します (一般的なコマンド)。
terraform destroy
プロンプトが表示されたら yes と入力し、Enter キーを押します。成功時の出力例:
Plan: 0 to add, 0 to change, 2 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
...
Destroy complete! Resources: 2 destroyed.
完全な例
サンプルコードをワンクリックで実行できます。今すぐ実行
サンプルコード
その他の例については、その他の完全な例リポジトリをご参照ください。
よくある質問
-
Terraform で複数のクラウドサービスをプロビジョニングする際、Tablestore インスタンスの作成と削除の効率を向上させるにはどうすればよいですか?
-
ユースケース: ワークフローで Tablestore インスタンスを作成したが、後続のリソースで失敗した場合、ロールバックと再試行時に、削除に時間がかかり、リージョン内で名前が一意である必要があるため、インスタンス名がまだ使用中になっています。
-
解決策: インスタンス名にランダムな接尾辞または自動インクリメント ID を追加します。各実行で一意の名前が使用されるため、削除を待たずに再試行をすぐに実行できます。