Terraform adalah alat open-source yang menyediakan dan mengelola infrastruktur cloud sebagai kode. Tablestore terintegrasi dengan Terraform, sehingga Anda dapat membuat dan mengelola instans Tablestore melalui file konfigurasi.
Jalankan kode contoh dengan satu klik. Jalankan sekarang
Prasyarat
-
Untuk keamanan, gunakan pengguna RAM dengan izin minimum (Buat pengguna RAM, Berikan izin kepada pengguna RAM). Kebijakan minimum yang diperlukan:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "ots:GetInstance", "ots:BindInstance2Vpc", "ots:ListVpcInfoByInstance", "ots:UnbindInstance2Vpc", "ots:DeleteInstance", "ots:InsertInstance", "ots:UpdateInstance", "ots:ListInstance" ], "Resource": "*" } ] } -
Siapkan lingkungan Terraform menggunakan salah satu metode berikut:
-
Explorer: Lingkungan Terraform online. Tidak perlu instalasi. Gratis.
-
Gunakan Terraform untuk membuat resource secara cepat: Terraform telah diinstal sebelumnya di Cloud Shell dengan kredensial yang telah dikonfigurasi. Jalankan perintah langsung tanpa perlu pengaturan tambahan.
-
Instal dan konfigurasikan Terraform: Pilihan terbaik untuk jaringan terbatas atau lingkungan pengembangan kustom.
-
Resource yang digunakan
-
alicloud_ots_instance: sebuah instans Tablestore.
Buat instans Tablestore
-
Buat direktori kerja dengan file bernama main.tf, lalu tambahkan kode berikut ke dalam 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 } # Instans Tablestore resource "alicloud_ots_instance" "default" { name = "${var.name}-${random_integer.default.result}" # Nama instans description = var.name # Deskripsi instans accessed_by = "Vpc" # Jenis jaringan untuk akses instans tags = { # Tag instans Created = "TF" For = "Building table" } } -
Inisialisasi Terraform.
terraform initOutput yang diharapkan jika berhasil:
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. -
Terapkan konfigurasi.
terraform applyKetik
yessaat diminta dan tekan Enter. Output yang diharapkan jika berhasil: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. -
Verifikasi hasilnya.
Terraform show
Jalankan perintah ini untuk melihat resource yang telah dibuat:
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$Konsol Tablestore
Masuk ke Konsol Tablestore. Di halaman , verifikasi bahwa instans Anda muncul.
Lepaskan resource
Untuk menghapus resource yang tidak lagi diperlukan, jalankan terraform destroy (Perintah umum).
terraform destroy
Ketik yes saat diminta dan tekan Enter. Output yang diharapkan jika berhasil:
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.
Contoh lengkap
Jalankan kode contoh dengan satu klik. Jalankan sekarang
Kode contoh
Temukan contoh tambahan di repositori More Complete Examples.
FAQ
-
Bagaimana cara meningkatkan efisiensi pembuatan dan penghapusan instans Tablestore saat menyediakan beberapa layanan cloud dengan Terraform?
-
Kasus penggunaan: Alur kerja membuat instans Tablestore tetapi gagal pada resource berikutnya. Saat rollback dan mencoba ulang, nama instans masih terpakai karena proses penghapusan membutuhkan waktu dan nama harus unik dalam satu Wilayah.
-
Solusi: Tambahkan akhiran acak atau ID auto-increment ke nama instans. Setiap eksekusi menggunakan nama unik, sehingga percobaan ulang dapat dilanjutkan segera tanpa menunggu proses penghapusan selesai.