Use Terraform to create a Tablestore instance

更新时间:
复制 MD 格式

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.

Note

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

Create a Tablestore instance

  1. Create a working directory, create a configuration file named main.tf in the working directory, and then copy the following code into main.tf.

    Note

    Terraform 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"
      }
    }
  2. Run the following command to initialize the Terraform runtime environment.

    terraform init

    The 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.
  3. Run the following command to apply the configuration.

    terraform apply

    When prompted, type yes and 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.
  4. 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

    image

    Tablestore console

    Log on to the Tablestore console. On the All Instances page, find and view the instance you created.

    image

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

Note

You can run the sample code in this tutorial with a single click. Run now

Sample code

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"
  }
}

For more complete examples, go to the folder for the corresponding cloud service in More Complete Examples.

FAQ

  1. 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