Tablestore integrates with Terraform, an open source Infrastructure as Code (IaC) tool. With Terraform, you can define and provision Tablestore instances programmatically instead of through the console.
This topic covers writing a Terraform configuration, applying it to create an instance, verifying the result, and cleaning up.
Run the sample code from this topic directly in Terraform Explorer.
Prerequisites
Before you begin, make sure that you have:
A RAM user with the minimum required permissions. To reduce the risk of leaking the AccessKey pair of your Alibaba Cloud account, use a RAM user instead of the root account. For more information, see Create a RAM user and Grant permissions to a RAM user. Attach the following policy to the RAM user:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "ots:GetInstance", "ots:BindInstance2Vpc", "ots:ListVpcInfoByInstance", "ots:UnbindInstance2Vpc", "ots:DeleteInstance", "ots:InsertInstance", "ots:UpdateInstance", "ots:ListInstance" ], "Resource": "*" } ] }A Terraform runtime environment set up through one of the following methods:
Terraform Explorer (recommended): An online runtime environment provided by Alibaba Cloud. No installation required. Best for quick experiments at no additional cost.
Cloud Shell: Terraform is preinstalled and credentials are preconfigured. Best for fast, low-cost debugging sessions.
Local installation: Install and configure Terraform on your on-premises machine. Best for custom development environments or restricted network conditions.
Required resources
This configuration uses the following Terraform resource:
alicloud_ots_instance -- A Tablestore instance.
Argument reference
The alicloud_ots_instance resource supports the following arguments:
| Argument | Required | Description | Example |
|---|---|---|---|
name | Yes | The instance name. Must be unique within the region. Appending a random suffix prevents name collisions during rollbacks. | tf-example-12345 |
description | No | A description for the instance. | tf-example |
accessed_by | No | The network types allowed to access the instance. Valid values: Any, Vpc, ConsoleOrVpc. | Vpc |
tags | No | A map of tags to assign to the instance. | { Created = "TF" } |
Procedure
Step 1: Write the configuration
Create a working directory and a file named main.tf. Add the following configuration:
variable "name" {
default = "tf-example"
}
variable "region" {
default = "cn-hangzhou"
}
provider "alicloud" {
region = var.region
}
resource "random_integer" "default" {
min = 10000
max = 99999
}
# Create a Tablestore instance with a random suffix to ensure name uniqueness
resource "alicloud_ots_instance" "default" {
name = "${var.name}-${random_integer.default.result}"
description = var.name
accessed_by = "Vpc"
tags = {
Created = "TF"
For = "Building table"
}
}The random_integer resource appends a random suffix to the instance name. This prevents name collisions if you need to tear down and recreate the instance, because Tablestore instance names must be unique within a region and deleted instances take time to fully release.
Step 2: Initialize Terraform
Run the following command to download the required provider plugins:
terraform initExpected output:
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.Step 3: Apply the configuration
Run the following command to create the instance:
terraform applyWhen prompted, type yes and press Enter to confirm. Expected output:
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.Step 4: Verify the result
Use either of the following methods to confirm that the instance was created:
Option A: Run terraform show
terraform showThe output displays the instance details, including its name, description, and network access type.
Option B: Check the Tablestore console
Log on to the Tablestore console. On the All Instances page, verify that the new instance appears in the list.
Clean up
To delete the instance and all associated resources managed by this configuration, run:
terraform destroyWhen prompted, type yes and press Enter to confirm. Expected output:
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.For more information about terraform destroy and other commands, see Common commands.
More examples
For additional Terraform examples across Alibaba Cloud services, see the landing-with-terraform quickstarts repository.
FAQ
How do I avoid name collisions in multi-service workflows?
Tablestore instance names must be unique within a region, and deleting an instance does not free the name immediately. If your workflow rolls back after creating a Tablestore instance, re-running with the same instance name fails because the previous instance may still be deleting.
Add a random suffix or auto-increment ID to each instance name so that every run produces a unique name. The sample code in this topic already does this with the random_integer resource.