Terraform是一種開源工具,用於安全高效地預覽、配置和管理雲基礎架構和資源,產品Tablestore已接入Terraform。本文將為您介紹如何通過Terraform建立Table Store執行個體。
本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
為了降低資訊安全風險,建議使用最小許可權的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運行環境,您可以選擇以下任一方式來使用Terraform。
Explorer:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。
Cloud Shell:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。
在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。
使用的資源
alicloud_ots_instance:Table Store執行個體。
建立Table Store執行個體
建立一個工作目錄,並在該工作目錄中建立名為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 } # Table Store執行個體 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初始化成功。
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已建立資源的詳細資料:
terraform show
登入控制台查看
登入Tablestore控制台,進入頁面,查看已建立的執行個體。

清理資源
當您不再需要上述通過Terraform建立或管理的資源時,請運行以下命令以釋放資源。關於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建立多個雲產品時,如何提升Table Store建立/刪除執行個體的效率?
案例:客戶A使用Terraform連續建立一系列雲產品執行個體,其中Table Store執行個體建立成功而後續流程中有其它雲產品執行個體建立失敗,於是客戶回退工作流程,刪除已建立的執行個體,並準備重新執行。由於Table Store刪除執行個體需要較長時間,且執行個體命名在同地區內具有唯一性,因此在執行個體刪除完成前,客戶無法建立相同執行個體,只能等待執行個體刪除完成。
解決方案:可以通過給Table Store執行個體名稱加上隨機數尾碼或自增ID的方式保證每次工作流程執行都建立不同名稱的執行個體。這樣在工作流程某個節點執行失敗需要回退並且重新執行時,無需等待上一個Table Store執行個體刪除完成即可重新執行工作流程。