Terraform is an open-source tool that lets you safely and efficiently preview, provision, and manage cloud infrastructure and resources. Tablestore is integrated with Terraform. This topic describes how to use Terraform to create a Tablestore instance.
You can run the sample code in this tutorial with a single click. Run now
Prerequisites
-
To minimize security risks, use a RAM user with the minimum required permissions to complete this tutorial. For more information, see Create a RAM user and Grant permissions to a RAM user. The following policy grants the minimum permissions required for this tutorial:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "ots:GetInstance", "ots:BindInstance2Vpc", "ots:ListVpcInfoByInstance", "ots:UnbindInstance2Vpc", "ots:DeleteInstance", "ots:InsertInstance", "ots:UpdateInstance", "ots:ListInstance" ], "Resource": "*" } ] } -
Prepare a Terraform runtime environment. You can use one of the following methods:
-
Explorer: Alibaba Cloud provides an online runtime environment for Terraform. You can log on to use and experiment with Terraform without installing it. This is a quick, convenient, and free way to try out and debug Terraform.
-
Use Terraform to quickly create resources: Terraform is preinstalled in Cloud Shell and identity credentials are pre-configured. You can run Terraform commands directly in Cloud Shell. This method provides quick, convenient, and low-cost access to Terraform.
-
Install and configure Terraform: This method is suitable for scenarios with poor network connectivity or when a custom development environment is required.
-
Resources used
-
alicloud_ots_instance: a Tablestore instance.
Create a Tablestore instance
-
Create a working directory, create a configuration file named main.tf in the working directory, and then copy the following code into main.tf.
NoteTerraform supports creating instances only in Capacity Unit (CU) mode. It does not support creating instances in VCU mode.
variable "name" { default = "tf-example" } variable "region" { default = "cn-hangzhou" } provider "alicloud" { region = var.region } resource "random_integer" "default" { min = 10000 max = 99999 } # Tablestore instance resource "alicloud_ots_instance" "default" { name = "${var.name}-${random_integer.default.result}" # Instance name description = var.name # Instance description accessed_by = "Vpc" # Network type for instance access tags = { # Instance tags Created = "TF" For = "Building table" } } -
Run the following command to initialize the Terraform runtime environment.
terraform initThe following output indicates that Terraform was initialized successfully.
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. -
Run the following command to apply the configuration.
terraform applyWhen prompted, type
yesand press Enter. Wait for the command to complete. The following output indicates that the resources were applied successfully.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. -
Verify the result.
Terraform show
Run the following command in your working directory to view the details of the resources created by Terraform:
terraform show
Tablestore console
Log on to the Tablestore console. On the page, find and view the instance you created.

Release resources
When you no longer need the resources created or managed by Terraform, run the following command. For more information about the terraform destroy command, see Common commands.
terraform destroy
During execution, Terraform previews the resources to be destroyed. To confirm the deletion, type yes at the prompt and press Enter. Wait for the command to complete. The following output indicates that the resources were destroyed successfully.
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.
Complete example
You can run the sample code in this tutorial with a single click. Run now
Sample code
For more complete examples, go to the folder for the corresponding cloud service in More Complete Examples.
FAQ
-
How can I improve the efficiency of creating and deleting Tablestore instances when provisioning multiple cloud services with Terraform?
-
Use case: The workflow successfully creates the Tablestore instance, but fails to create a subsequent resource. The user rolls back the workflow, which attempts to delete the created instances, and then prepares to run the workflow again. However, deleting a Tablestore instance takes time, and instance names must be unique within a region. Therefore, the user cannot re-run the workflow to create an instance with the same name until the original instance is deleted.
-
Solution: Append a random number suffix or an auto-incrementing ID to the Tablestore instance name to ensure a unique name for each workflow execution. This way, if a workflow fails and needs to be rolled back and re-run, you can immediately rerun the workflow without waiting for the previous instance to be deleted.
References
-
To learn about Terraform on Alibaba Cloud, see What is Terraform?.
-
If
terraform inittimes out due to network latency or other issues that prevent the provider from being downloaded, see Configure Terraform Init acceleration.